Professional Documents
Culture Documents
Part 2 金融資料前處理
Part 2 金融資料前處理
授課教師: 翁詠祿
日期: 10/18/2022、
11/01/2022
[1] Marcos Lopez de Prado, Advances in Financial Machine Learning, Wiely, 2018
[2] Sebastian Raschka and Vahid Mirjalili, Python Machine Learning: Machine Learning and Deep Learning with
Python, scikit-learn, and TensorFlow 2, 3rd Edition, 2019
[3] Stefan Jansen, Machine Learning for Algorithmic Trading - Second Edition,2020
q There are two “NaN” data, which mean the missing data.
Ø column C and D have one “NaN” respectively.
Original data
1. Remove rows that contain missing values:
Original data
q Decision trees and random forests are two for the very
few machine learning algorithms where we don’t need to
worry about feature scaling. (scale invariant)
Sum up to 1
q Characteristic equation:
Ø It’s solved to find a matrix's eigen vectors. (non-zero solution)
det(𝜆𝐼 − 𝐴) = 0
𝐴 − 𝐼 𝒙𝟏 = 0 → 𝐴𝒙𝟏 = 𝒙𝟏 → 𝒙𝟏 = (0.6,0.4)"
1 1
𝐴 − 𝐼 𝒙𝟐 = 0 → 𝐴𝒙𝟐 = 𝒙𝟐 → 𝒙𝟐 = (1, −1)"
2 2
q Check:
0.8 0.3 0.6
𝐴𝒙𝟏 = = 𝒙𝟏 → 𝜆 = 1 𝐴$%% 𝒙𝟏 = 𝐴&& 𝒙𝟏 = ⋯ = 𝒙𝟏
0.2 0.7 0.4
0.8 0.3 1 1 1
𝐴𝒙𝟐 = = 𝒙𝟐 → 𝜆 = 1 1 $%%
0.2 0.7 −1 2 2 𝐴$%% 𝒙𝟐 = 𝐴&& 𝒙 = ⋯ = ( ) 𝒙𝟐
2 𝟐 2
Small value
1 very
0.8 0.6
𝐴&& = 𝒙𝟏 + ( )&& 0.2 𝒙𝟐 = + small
0.2 2 0.4
vector
𝐵 = 𝑃3. 𝐴𝑃
𝑎. 𝒗. + 𝑎+ 𝒗+ + ⋯ + 𝑎4 𝒗4 = 0
Proof: 𝜆. ⋯ 0
𝐴𝑃 = 𝐴 𝒙𝟏 , … , 𝒙𝒏 = 𝜆. 𝒙𝟏 , … , 𝜆2 𝒙𝒏 = 𝒙𝟏 , … , 𝒙𝒏 ⋮ ⋱ ⋮ = 𝑃𝐷
0 ⋯ 𝜆2
q 𝐴 = 𝑃𝐷𝑃WJ
𝐴X = (𝑃𝐷𝑃WJ)X = 𝑃𝐷 X 𝑃WJ
x= x. , x+ , ⋯ , x* , x ∈ ℝ*
xW = 𝐳 , W ∈ ℝ*×8
𝐳 = z. , z+ , ⋯ , z8 , 𝐳 ∈ ℝ8
value − mean
𝑧=
standard deviation
Result Relationship
Positive Two variables increase or decrease together.
sign (correlated)
Negative One increases when the other decreases.
sign (Inversely correlated)
Zero Two variables are not related
(uncorrelated)
𝜆. = 1.284028 ; 𝜆+ = 0.04908323
𝜆$ > 𝜆' à eigenvector that corresponds to the first principal component (PC1)
is 𝒗𝟏 and the corresponds to the second component (PC2) is 𝒗𝟐 .
0.6778736 −0.7351785
𝒗𝟏 = 𝒗𝟐 =
0.7351785 0.6778736
𝜆. = 1.284028 ; 𝜆+ = 0.04908323
We can either form a feature vector with both of the eigen vectors or
discard the eigen vector 𝒗𝟐 , which is the one of lesser significance.
1.05594 −0.50494
∵ 𝜆9 > 𝜆+ > 𝜆. 𝑤 = 0.69108 −0.67548
1 1
𝒗, 𝒗'
5. Transform the samples onto new subspace:
• Use 3×2 dimensional matrix 𝑊: (𝑥 is data point)
• x= x$ , x' , ⋯ , x- , x ∈ ℝ-
𝑦 = 𝑊:x
Most information
(eigen value)
Dept. of Electrical Engineering
66 National Tsing Hua University, Taiwan
Feature transformation
q Using projection matrix onto the PCA subspace:
𝐱 ; = 𝐱𝐖 (2 − dimension)
1
𝐦" = I 𝐱 i ∈ class label
n"
𝐱∈@!
J
3+ 3K.M 𝑒𝑥𝑝
𝑓 𝒙 𝝁, 𝜮 = (2𝜋) 𝜮 −0.5 𝒙 − 𝝁 : Σ 3. (𝒙 − 𝝁)
Multivariate normal distribution
𝑥height
𝒙= 𝑥
weight
𝑝 𝒙 𝝁4G2 , 𝜮4G2
1
= 𝜮4G2 3K.M 𝑒𝑥𝑝 −0.5 𝒙 − 𝝁4G2 : Σ 3. (𝒙 − 𝝁4G2 )
2𝜋
Ø Woman:
𝑝 𝒙 𝝁HI4G2 , 𝜮HI4G2
1
= 𝜮HI4G2 3K.M 𝑒𝑥𝑝 −0.5 𝒙 − 𝝁HI4G2 : Σ 3. (𝒙 − 𝝁HI4G2 )
2𝜋
q Result:
𝑙𝑛 𝑝 𝒙 𝝁*+, , 𝜮*+,
= −𝑙𝑛 2𝜋 − 0.5 𝜮*+, − 0.5 𝒙 − 𝝁*+, 0 Σ1# (𝒙 − 𝝁*+, )
𝑙𝑛 𝑝 𝒙 𝝁./*+, , 𝜮./*+,
= −𝑙𝑛 2𝜋 − 0.5 𝜮./*+, − 0.5 𝒙 − 𝝁./*+, 0 Σ1# (𝒙 − 𝝁./*+, )
Which:
B
𝐒< = I 𝑛A (𝒎𝒊 − 𝐦 )(𝒎A − 𝐦 ):
A-.
𝑿; = 𝑿𝑾
2-d dataset.
𝜙
𝐱 = x$ , x' ;
𝑧 = x$ , x' , x$' + x'' ;
Project the data from a lower dimensional (2D) to a higher dimensional (3D) space.
Dept. of Electrical Engineering
90 National Tsing Hua University, Taiwan
Kernel functions and the kernel trick
q We can tackle nonlinear problems by projecting them
onto a new feature space of higher dimensionality where
the classes become linearly separable.
𝜙: ℝ* ⟶ ℝ8 (k ≫ d)
q For example :
O
𝐱 = x. , x+
↓𝜙
:
𝑧= 𝑥.+ , 2𝑥. 𝑥+ , 𝑥++
%
1 " "
σ,8 = I(x, − µ, )(x8 − µ8 )
n
"-.
%
1 " "
σ,8 = I x, x8 , Standardization(~N(0,1))
n
"-.
% %
1 (") (")<
1
Σ = 4𝒙 𝒙 ≈ 4 𝜙(𝒙(") )𝜙(𝒙 A )O
n n
"-. "-.
= > '
𝒙 −𝒙
κ 𝒙 = ,𝒙 > = exp −
2𝜎 '
' $
κ 𝒙 = ,𝒙 > = exp −𝛾 𝒙 = −𝒙 > , 𝑤ℎ𝑒𝑟𝑒 𝛾 =
'@ "
κ 𝒙 . ,𝒙 . κ 𝒙 . ,𝒙 + κ 𝒙 . ,𝒙 %
+ . + +
⋯ +
K= κ 𝒙 ,𝒙 κ 𝒙 ,𝒙 κ 𝒙 ,𝒙 %
⋮ ⋱ ⋮
κ 𝒙 % ,𝒙 . κ 𝒙 % ,𝒙 + ⋯ κ 𝒙 % ,𝒙 %
• Close price
• tickDirection: 每筆的價格走向
• minusTick: 價格向下
• zeroMinusTick: 價格不變但前一個 tick 價格向下
• plusTick: 價格向上
• zeroPlusTick: 價格不變但前一個 tick 價格向上
EK θO = EK T max{P b) = 1 ,1 − P b) = 1 }
T ∗ = arg min{ θO ≥ EK T max{P b) = 1 ,1 − P b) = 1 } }
, 𝑖𝑓 𝑟A#,%,A#,%CD < −𝜏
−1
𝑦) = ™ 0 , 𝑖𝑓 |𝑟A#,%,A#,%CD | ≤ 𝜏
1 , 𝑖𝑓 𝑟A#,%,A#,%CD > 𝜏
−𝜏
𝑦) = −1
𝑦) = 0