---
title: "Simulation"
author: "Tian Qin"
date: "2024-01-30"
output: html_document
---

## Simulation data

### Simulation

```{r,warning=FALSE}

library(RiemannLebesgueForest)
library(randomForest)
library(MASS)
library(caret)
set.seed(42)

# 
# X = data.frame(replicate(100,(runif(1500,min=0, max=1))))  #U[0,1]^100
# 
# 
fx <- function(x){

  10* prod( exp(-2*x[1:10]^2) )


}
# ysig <- apply(X,1,fx)
# 
# 
# yNoise = rnorm(1500,sd=1.3)
# y = ysig + yNoise

# train_index <- sample(1:nrow(X),size = 1000)
# xtrain <- X[train_index,]
# ytrain <- y[train_index]
# xtest <- X[-train_index,]
# ytest <- y[-train_index]
```

## Time efficiency (scality of RLF)

```{r}



n_samples <- c(1000,10000,20000,30000,40000,50000,60000,70000,80000,90000)

time_rlf <- c()
time_rf  <- c()
for(i in 1:length(n_samples)){
  
    xtrain = data.frame(replicate(10,sample.int(500, n_samples[i],replace=TRUE)))  #U[1,2,...,500]^10
    
    
    
    ysig <- apply(xtrain,1,fx)
    
    
    yNoise = rnorm(n_samples[i],sd=1)
    ytrain= ysig + yNoise
  
   start.time_rlf <- Sys.time()
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=1,replace=FALSE,Lp=0.8)

   end.time_rlf <- Sys.time()
   time.taken_rlf <-difftime(end.time_rlf, start.time_rlf, units = "secs")[[1]]
   print(time.taken_rlf)
   time_rlf[i] <-  time.taken_rlf

    
    start.time_rf <- Sys.time()
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   end.time_rf <- Sys.time()
      time.taken_rf <-difftime(end.time_rf, start.time_rf, units = "secs")[[1]]
   print(time.taken_rf)
   time_rf[i] <-  time.taken_rf

  
}


plot(time_rlf,type='l',col='red')
points(time_rf,type='l',col='blue')



```

