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

 
Maxim Dmitrievsky #:
I thought there were some out-of-the-box tools in the library so you could output something without much effort

like Reshetov did a few dozen pages ago.

(and it's not about the library)

 
mytarmailS #:

I have a function that searches for a pattern/sequence in another large string , if found then ITINA otherwise False

illustration



I have a function in Rk but it is slow for large calculations, or rather there are even several functions written in different styles....

This is how the input data looks like, by the way, both strings can be of arbitrary length.

Here is the first s1 function written in the standard style, clear but cumbersome.

or something more elegant like s2.

both do the same thing

I have a question/request - can you write this simple function in rcpp for me?

I'm not ready to do it, because I don't have free time, and I don't like coding and debugging very much. And it will be more useful for you to figure it out yourself. I will answer some specific technical questions if I can.

 
Aleksey Nikolayev #:

I'm not ready to take it on, as I don't have free time, and I don't like coding and debugging very much. And it will be more useful for you to figure it out yourself. I will answer some specific technical questions if I can.

thanks for not helping me) I figured it out myself and that's great )

R code

yes_seq <- function(pat , dat){  
  lv <- rep(F,length(pat))
  k <- 1     
  for(i in 1:length(dat)){        
    if(dat[i] == pat[k]){
      lv[k] <- TRUE
      k <- k+1 
    }       
    if(k==length(pat)+1) break
  }
  return(  all(lv)   )
}

Rcpp code

src <-
  "bool yes_seq_cpp(NumericVector pat, NumericVector dat){

    LogicalVector lv (pat.length() ,false);
    int k=0;

    for(int i=0; i<dat.length(); ++i){
    
      if(dat[i]==pat[k]){
            lv[k] = true;
            k = k+1; 
          }
if(k==pat.length())  break;
}
    
return  is_true(all(lv == true));
}
"
Rcpp::cppFunction(src)


I have a question: how to make pat dat input variables accept both numeric vectors (as now) and string vectors ?




p.s. The function turned out to be only 6 times faster (( I thought it would be at least 100 times faster ((((

microbenchmark::microbenchmark( yes_seq(pat,dat), yes_seq_cpp(pat,dat))

Unit: microseconds
                  expr    min     lq     mean median      uq     max neval cld
     yes_seq(pat, dat) 42.193 43.333 44.78712 43.903 45.0435 106.052   100   b
 yes_seq_cpp(pat, dat)  5.701  5.987  6.61993  6.272  6.8425  22.807   100  a 
 
mytarmailS #:

thanks for not helping) figured it out myself and that's great )

R code

Rcpp code


There is a question: how to make pat dat input variables accept both numeric vectors (as now) and string vectors ?




p.s. The function turned out to be only 6 times faster (( I thought it would be at least 100 times faster ((((

You can compile the code with R (compile). I got almost 3-fold speedup of the code.

Instead of for, use foreach, loading all kernels.

 
СанСаныч Фоменко #:

You can compile R code (compile). I got almost 3-fold speedup of the code.

Use foreach instead of for, loading all kernels.

since R 3.4 cmpfun compilation is included by default in all R code

https://stackoverflow.com/questions/41502721/could-someone-explain-what-compiling-in-r-is-and-why-it-would-speed-up-this-c
 
mytarmailS #:

since R 3.4 cmpfun compilation is included by default in all R code

https:// stackoverflow.com/questions/41502721/could-someone-explain-what-compiling-in-r-is-and-why-it-would-speed-up-this-c

This post I understand that now it seems to always compile packages, which by the way can be seen when updating packages , line byte-compile and prepare package for lazy loading.

But I think that user code is NOT compiled. We load functions by source and I didn't see in the description of source that the function to be loaded is compiled

 
The articleWorking with Matrices and Vectors in MQL5 has been published:

 
СанСаныч Фоменко #:

This post I understand that now it seems to always compile packages, which by the way can be seen when updating packages , line byte-compile and prepare package for lazy loading.

But I believe that user code is NOT compiled. We load functions by source and I didn't see in the description of source that the function to be loaded is compiled

hmm, I'll try it

 
mytarmailS #:

There is a question: how to make pat dat input variables accept both numeric vectors (as now) and string vectors ?

The first idea to overload is to write another function with the same name but with different arguments. I've never done this in Rcpp, so I don't know if it will work. I'll probably have to make the C code in a separate file and use sourceCpp() instead of cppFunction().

 
Aleksey Nikolayev #:

The first idea to overload is to write another function with the same name but with different arguments. I've never done this in Rcpp, so I don't know if it will work. I'll probably have to make the C code in a separate file and use sourceCpp() instead of cppFunction().

Thanks

Reason: