Machine learning in trading: theory, models, practice and algo-trading - page 3258

 
fxsaber #:

Python takes how long to compute the same size of the original matrix as in the example above?

import numpy as np
import time

def calc_corr_matrix():
    arr = np.random.rand(15000, 100).astype(np.float32)
    corr_matrix = np.corrcoef(arr)
    size_in_mb = corr_matrix.nbytes / 1024**2
    print("Array size:", size_in_mb, "MB")
    return corr_matrix

start_time = time.time()
corr_matrix = calc_corr_matrix()
end_time = time.time()

print("Time taken:", end_time - start_time, "seconds")
Array size: 1716.61376953125 MB
Time taken: 2.08686900138855 seconds

Time measurement considering matrix creation

 

throw away your stones )



 
Maxim Dmitrievsky #:

Time measurement taking into account matrix creation

Array size: 1716.61376953125 MB
Time taken: 4.784467697143555 seconds

This is on an old FX-8350.

 
fxsaber #:

On its basis I made a calculation of the correlation matrix.

Measured the performance.

For statistics, this is the result I got

2023.09.26 06:28:23.304 Test_Corr (USDJPY,H1)   EX5: 3981 AVX Release.
2023.09.26 06:28:23.304 Test_Corr (USDJPY,H1)   TerminalInfoString(TERMINAL_CPU_NAME) = AMD FX-8350 Eight-Core 
2023.09.26 06:28:23.304 Test_Corr (USDJPY,H1)   TerminalInfoInteger(TERMINAL_CPU_CORES) = 8 
2023.09.26 06:28:23.304 Test_Corr (USDJPY,H1)   TerminalInfoString(TERMINAL_CPU_ARCHITECTURE) = AVX 
2023.09.26 06:28:23.332 Test_Corr (USDJPY,H1)   inRows = 100 inCols = 15000 
2023.09.26 06:28:45.032 Test_Corr (USDJPY,H1)   matrix<double> Matrix1 = CorrMatrix(Matrix) - 21700095 mcs, 1717 MB
2023.09.26 06:29:48.495 Test_Corr (USDJPY,H1)   matrix<double> Matrix2 = Matrix.CorrCoef(false) - 63460976 mcs, 1717 MB
2023.09.26 06:29:50.225 Test_Corr (USDJPY,H1)   IsEqual(Matrix1, Matrix2) = true 

2023.09.26 06:34:21.572	Test_Corr (USDJPY,H1)	EX5: 3981 X64 Regular Release.
2023.09.26 06:34:21.572	Test_Corr (USDJPY,H1)	TerminalInfoString(TERMINAL_CPU_NAME) = AMD FX-8350 Eight-Core 
2023.09.26 06:34:21.572	Test_Corr (USDJPY,H1)	TerminalInfoInteger(TERMINAL_CPU_CORES) = 8 
2023.09.26 06:34:21.572	Test_Corr (USDJPY,H1)	TerminalInfoString(TERMINAL_CPU_ARCHITECTURE) = AVX 
2023.09.26 06:34:21.600	Test_Corr (USDJPY,H1)	inRows = 100 inCols = 15000 
2023.09.26 06:34:42.908	Test_Corr (USDJPY,H1)	matrix<double> Matrix1 = CorrMatrix(Matrix) - 21308403 mcs, 1717 MB
2023.09.26 06:35:46.736	Test_Corr (USDJPY,H1)	matrix<double> Matrix2 = Matrix.CorrCoef(false) - 63826475 mcs, 1717 MB
2023.09.26 06:35:48.481	Test_Corr (USDJPY,H1)	IsEqual(Matrix1, Matrix2) = true 
Clearly Python is much faster. It means that C will be faster too, so it turns out that MQ is not telling us something, promising comparable performance....

I should note that Python has a small parallelisation when running code - for half a second for about two cores, the rest is counted on one core.

 
NumPy library is written in C
 
Maxim Dmitrievsky #:
the NumPy library is written in C.

Well, yes, it makes sense. That's why I mentioned that the python wrapper is faster than a C compiler like MQL5.

 

Here on R ChatGPT offers

set.seed(123)  #  Задаем зерно для воспроизводимости результатов

calc_corr_matrix <- function() {
  #arr <- matrix(runif(15000 * 100), nrow = 15000, ncol = 100)
  arr <- matrix(runif(100 * 15000), nrow = 100, ncol = 15000)
  corr_matrix <- cor(arr)
  size_in_mb <- object.size(corr_matrix) / 1024^2
  cat("Array size:", size_in_mb, "MB\n")
  return(corr_matrix)
}

start_time <- Sys.time()
corr_matrix <- calc_corr_matrix()
end_time <- Sys.time()

cat("Time taken:", as.numeric(end_time - start_time), "seconds\n")

Result

> set.seed(123)  #  Задаем зерно для воспроизводимости результатов

> calc_corr_matrix <- function() {
+   #arr <- matrix(runif(15000 * 100), nrow = 15000, ncol = 100)
+   arr <- matrix(runif(100 * 15000), nrow = 100,  .... [TRUNCATED] 

> start_time <- Sys.time()

> corr_matrix <- calc_corr_matrix()
Array size: 1716.614 MB

> end_time <- Sys.time()

> cat("Time taken:", as.numeric(end_time - start_time), "seconds\n")
Time taken: 27.92359 seconds
> 
 

As I understand, python can work with an integer matrix and the speeds here are of a different order here

import numpy as np
import time

def calc_corr_matrix():
    arr = np.random.randint(1, 101, size=(15000, 100), dtype=np.int32)
    corr_matrix = np.corrcoef(arr, rowvar=False)
    size_in_mb = corr_matrix.nbytes / 1024**2
    print("Array size:", size_in_mb, "MB")
    return corr_matrix

np.random.seed(123)  #  Задаем зерно для воспроизводимости результатов

start_time = time.time()
corr_matrix = calc_corr_matrix()
end_time = time.time()

print("Time taken:", end_time - start_time, "seconds")

If the code is correct, the result is as follows

Array size: 0.0762939453125 MB
Time taken: 0.5172276496887207 seconds

The question of accuracy/comparability of the results of calculations itself should be checked.

 
fxsaber #:

This is simply a transition from CMatrixDouble to matrix<double>.

It takes 20% more execution time to convert formats both ways. But it is still much (> 3 times) slower than NumPy.

 
Maxim Dmitrievsky #:

Time measurement taking into account matrix creation

Save both matrices to files to reconcile the results.

Reason: