Last active
January 4, 2018 21:11
-
-
Save mikelove/07539d012befa317476bd99d8293535d to your computer and use it in GitHub Desktop.
trying out RcppNumeric (R script)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f <- function(beta, coefs) coefs[1]*beta[1]^4 + coefs[2]*beta[1]^2 + | |
coefs[3]*beta[1] + coefs[4]*beta[2]^2 + coefs[5] | |
n <- 100000 | |
coefs <- matrix(c(0.1, -1, 1, 1, 10, | |
0.1, -1, .5, 1, 10), nrow=5, ncol=n) | |
s <- seq(-4, 4,length.out = 100) | |
plot(s, sapply(s, function(z) f(c(z,0),coefs[,1])), type="l") | |
par <- optim(c(0,0), f, coefs=coefs)$par | |
abline(v=par[1]) | |
system.time({ | |
pars <- matrix(nrow=2, ncol=n) | |
for (i in 1:n) { | |
pars[,i] <- optim(c(0,0), f, coefs=coefs[,i], method="L-BFGS-B")$par | |
} | |
}) | |
pars[,1:4] | |
library(Rcpp) | |
sourceCpp("numeric.cpp") | |
system.time({ | |
betas <- foo_fun(coefs) | |
}) | |
betas[,1:4] | |
abline(v=res[1], col="red") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment