CorrCoef
Tính hệ số tương quan Pearson (hệ số tương quan tuyến tính).
cpp
matrix matrix::CorrCoef(
const bool rowvar=true // vector hàng hoặc cột của các quan sát
);
scalar vector::CorrCoef(
const vector& b // vector thứ hai
);
1
2
3
4
5
6
7
2
3
4
5
6
7
Giá trị trả về
Hệ số tương quan tích-lũy Pearson.
Ghi chú
Hệ số tương quan nằm trong khoảng [-1, 1].
Do làm tròn số thực, mảng kết quả có thể không phải là Hermitian, các phần tử trên đường chéo có thể không bằng 1, và các phần tử có thể không thỏa mãn bất đẳng thức abs(a) <= 1. Phần thực và phần ảo được giới hạn trong khoảng [-1, 1] để cố gắng cải thiện tình trạng này, nhưng không giúp ích nhiều trong trường hợp phức tạp.
Thuật toán đơn giản để tính hệ số tương quan của hai vector bằng MQL5:
cpp
double VectorCorrelation(const vector& vector_x, const vector& vector_y)
{
ulong n=vector_x.Size()<vector_y.Size() ? vector_x.Size() : vector_y.Size();
if(n<=1)
return(0);
ulong i;
double xmean=0;
double ymean=0;
for(i=0; i<n; i++)
{
if(!MathIsValidNumber(vector_x[i]))
return(0);
if(!MathIsValidNumber(vector_y[i]))
return(0);
xmean+=vector_x[i];
ymean+=vector_y[i];
}
xmean/=(double)n;
ymean/=(double)n;
double s=0;
double xv=0;
double yv=0;
double t1=0;
double t2=0;
//--- tính toán
s=0;
for(i=0; i<n; i++)
{
t1=vector_x[i]-xmean;
t2=vector_y[i]-ymean;
xv+=t1*t1;
yv+=t2*t2;
s+=t1*t2;
}
//--- kiểm tra
if(xv==0 || yv==0)
return(0);
//--- trả về kết quả
return(s/(MathSqrt(xv)*MathSqrt(yv)));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Ví dụ MQL5:
cpp
vectorf vector_a={1,2,3,4,5};
vectorf vector_b={0,1,0.5,2,2.5};
Print("vectors correlation ",vector_a.CorrCoef(vector_b));
//---
matrixf matrix_a={{1,2,3,4,5},
{0,1,0.5,2,2.5}};
Print("matrix rows correlation\n",matrix_a.CorrCoef());
matrixf matrix_a2=matrix_a.Transpose();
Print("transposed matrix cols correlation\n",matrix_a2.CorrCoef(false));
matrixf matrix_a3={{1.0f, 2.0f, 3.0f, 4.0f, 5.0f},
{0.0f, 1.0f, 0.5f, 2.0f, 2.5f},
{0.1f, 1.0f, 2.0f, 1.0f, 0.3f}};
Print("rows correlation\n",matrix_a3.CorrCoef());
Print("cols correlation\n",matrix_a3.CorrCoef(false));
/*
vectors correlation 0.9149913787841797
matrix rows correlation
[[1,0.91499138]
[0.91499138,1]]
transposed matrix cols correlation
[[1,0.91499138]
[0.91499138,1]]
rows correlation
[[1,0.91499138,0.08474271]
[0.91499138,1,-0.17123166]
[0.08474271,-0.17123166,1]]
cols correlation
[[1,0.99587059,0.85375023,0.91129309,0.83773589]
[0.99587059,1,0.80295509,0.94491106,0.88385159]
[0.85375023,0.80295509,1,0.56362146,0.43088508]
[0.91129309,0.94491106,0.56362146,1,0.98827404]
[0.83773589,0.88385159,0.43088508,0.98827404,1]]
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Ví dụ Python:
cpp
import numpy as np
va=[1,2,3,4,5]
vb=[0,1,0.5,2,2.5]
print("vectors correlation")
print(np.corrcoef(va,vb))
ma=np.zeros((2,5))
ma[0,:]=va
ma[1,:]=vb
print("matrix rows correlation")
print(np.corrcoef(ma))
print("transposed matrix cols correlation")
print(np.corrcoef(np.transpose(ma),rowvar=False))
print("")
ma1=[[1,2,3,4,5],[0,1,0.5,2,2.5],[0.1,1,0.2,1,0.3]]
print("rows correlation\n",np.corrcoef(ma1))
print("cols correlation\n",np.corrcoef(ma1,rowvar=False))
transposed matrix cols correlation
[[1. 0.91499142]
[0.91499142 1. ]]
rows correlation
[[1. 0.91499142 0.1424941 ]
[0.91499142 1. 0.39657517]
[0.1424941 0.39657517 1. ]]
cols correlation
[[1. 0.99587059 0.98226063 0.91129318 0.83773586]
[0.99587059 1. 0.99522839 0.94491118 0.88385151]
[0.98226063 0.99522839 1. 0.97234063 0.92527551]
[0.91129318 0.94491118 0.97234063 1. 0.98827406]
[0.83773586 0.88385151 0.92527551 0.98827406 1. ]]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33