Skip to content

Instantly share code, notes, and snippets.

@Gedevan-Aleksizde
Created November 18, 2018 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gedevan-Aleksizde/beb4185aa8967af20bed68b8d1c0c4b5 to your computer and use it in GitHub Desktop.
Save Gedevan-Aleksizde/beb4185aa8967af20bed68b8d1c0c4b5 to your computer and use it in GitHub Desktop.
---
title: "benchmark"
author: "ill-identified"
output:
pdf_document: default
html_document: default
---
## JIT 有効になってるとこれの結果が変わるかを検証
https://twitter.com/keito_oz/status/1062346765712580608
```{r setup}
require(tictoc)
require(tidyverse)
require(purrr)
require(skimr)
compiler::enableJIT(3)
set.seed(42)
S=100
K=100
r=5/100
q=0.0
sigma=0.3
T=1
Call_MC<-function(sample)
{
Call<-0
for(i in 1:sample)
{
Call<-Call+max(S*exp((r-q-sigma^2/2)*T+sigma*sqrt(T)*rnorm(1))-K,0)
}
Call<-exp(-r*T)*Call/sample
return(Call)
}
Call_MC2<-function(sample)
{
x<-rnorm(sample)
y<-S*exp((r-q-sigma^2/2)*T+sigma*sqrt(T)*x)-K
Call<-exp(-r*T)*sum(y[y>0])/sample #y(Call価値)の正の部分のみを足す
return(Call)
}
```
```{r}
result <- bind_rows(
map_dfr(rep(1e5, 100), function(x) system.time(Call_MC(x))%>% data.matrix %>% t %>% as.data.frame) %>%
mutate(n_iter=10e5, method='using loop'),
map_dfr(rep(1e5, 100), function(x) system.time(Call_MC2(x))%>% data.matrix %>% t %>% as.data.frame) %>%
mutate(n_iter=10e5, method='vectorization')
)
result <- result %>% bind_rows(
map_dfr(rep(1e6, 100), function(x) system.time(Call_MC(x))%>% data.matrix %>% t %>% as.data.frame) %>%
mutate(n_iter=10e6, method='using loop'),
map_dfr(rep(1e6, 100), function(x) system.time(Call_MC2(x))%>% data.matrix %>% t %>% as.data.frame) %>%
mutate(n_iter=10e6, method='vectorization')
)
result <- result %>% bind_rows(
map_dfr(rep(1e4, 1000), function(x) system.time(Call_MC(x))%>% data.matrix %>% t %>% as.data.frame) %>%
mutate(n_iter=10e4, method='using loop'),
map_dfr(rep(1e4, 1000), function(x) system.time(Call_MC2(x))%>% data.matrix %>% t %>% as.data.frame) %>%
mutate(n_iter=10e4, method='vectorization')
)
skim_with(numeric = list(hist = NULL))
group_by(result, method, n_iter) %>% skim(elapsed)
```
@Gedevan-Aleksizde
Copy link
Author

Skim summary statistics
n obs: 2400
n variables: 7
group variables: method, n_iter

─ Variable type:numeric ────────────────────────────────────────────────────────
method n_iter variable missing complete n mean sd p0 p25 p50 p75 p100
using loop 1e+05 elapsed 0 1000 1000 0.019 0.0014 0.017 0.018 0.018 0.019 0.038
using loop 1e+06 elapsed 0 100 100 0.23 0.25 0.19 0.2 0.2 0.2 2.65
using loop 1e+07 elapsed 0 100 100 2.08 0.083 1.98 2.03 2.07 2.11 2.58
vectorization 1e+05 elapsed 0 1000 1000 0.00074 0.00044 0 0 0.001 0.001 0.001
vectorization 1e+06 elapsed 0 100 100 0.0069 0.00091 0.006 0.007 0.007 0.007 0.015
vectorization 1e+07 elapsed 0 100 100 0.076 0.0016 0.073 0.075 0.076 0.077 0.08

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment