knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(mypkgr) library(devtools) devtools::install_github("Florian-40/microbenchmark") library(microbenchmark) library(ggplot2)
You can find microbenchmark package on my Git-Hub page : https://github.com/Florian-40/microbenchmark.git
mb <- microbenchmark(mvtnorm::dmvnorm(rep(1.96, 2)), mvnpdf(x=matrix(rep(1.96,2)), Log=FALSE), times=1000L) mb expression<-c('dmvnorm', 'mvnpdf') autoplot(mb, expression)
And with multidimensionnal, we get :
n <- 100 mb <- microbenchmark(mvtnorm::dmvnorm(matrix(1.96, nrow = n, ncol = 2)), mvnpdf(x=matrix(1.96, nrow = 2, ncol = n), Log=FALSE), times=100L) mb expression<-c('dmvnorm', 'mvnpdf') autoplot(mb, expression)
By using this lines in terminal, we can check profiling of the code :
profvis::profvis({ + n <- 10e4 + pdfval <- mvnpdf(x=matrix(1.96, nrow = 2, ncol = n), Log=FALSE) + })
mvnpdfsmart
function.We can use new function mvnpdfmart
and check profile with similar code :
profvis::profvis({ + n <- 10e4 + pdfval <- mvnpdfsmart(x=matrix(1.96, nrow = 2, ncol = n), Log=FALSE) + })
We can compare results with a microbenchmark
:
n <- 1000 mb <- microbenchmark(mvtnorm::dmvnorm(matrix(1.96, nrow = n, ncol = 2)), mvnpdf(x=matrix(1.96, nrow = 2, ncol = n), Log=FALSE), mvnpdfsmart(x=matrix(1.96, nrow = 2, ncol = n), Log=FALSE), times=100L) mb expression<-c('dmvnorm', 'mvnpdf', 'mvnpdfsmart') autoplot(mb, expression)
mvnpdfoptim
function.We can use new function mvnpdfmart
and check profile with similar code :
profvis::profvis({ + n <- 10e4 + pdfval <- mvnpdfoptim(x=matrix(1.96, nrow = 2, ncol = n), Log=FALSE) + })
We can compare all results with a microbenchmark
:
n <- 1000 mb <- microbenchmark(mvtnorm::dmvnorm(matrix(1.96, nrow = n, ncol = 2)), mvnpdf(x=matrix(1.96, nrow = 2, ncol = n), Log=FALSE), mvnpdfsmart(x=matrix(1.96, nrow = 2, ncol = n), Log=FALSE), mvnpdfoptim(x=matrix(1.96, nrow = 2, ncol = n), Log=FALSE), times=100L) mb expression<-c('dmvnorm', 'mvnpdf', 'mvnpdfsmart', 'mvnpdfoptim') autoplot(mb, expression)
We write a better inversion matrix with C++ language and we include it in mvnpdfsmart
. Thus, we get microbenchmark :
n <- 1000 mb <- microbenchmark(mvtnorm::dmvnorm(matrix(1.96, nrow = n, ncol = 2)), mvnpdf(x=matrix(1.96, nrow = 2, ncol = n), Log=FALSE), mvnpdfsmart(x=matrix(1.96, nrow = 2, ncol = n), Log=FALSE), mvnpdfoptim(x=matrix(1.96, nrow = 2, ncol = n), Log=FALSE), mvnpdf_invC(x=matrix(1.96, nrow = 2, ncol = n), Log=FALSE), times=100L) mb expression<-c('dmvnorm', 'mvnpdf', 'mvnpdfsmart', 'mvnpdfoptim', 'mvnpdf_invC') autoplot(mb, expression)
Now, use complete implementation of mvnpdfsmart
in C++.
Rcpp::sourceCpp("../src/mvnpdfC.cpp") n <- 1000 mb <- microbenchmark(mvtnorm::dmvnorm(matrix(1.96, nrow = n, ncol = 2)), mvnpdf(x=matrix(1.96, nrow = 2, ncol = n), Log=FALSE), mvnpdfsmart(x=matrix(1.96, nrow = 2, ncol = n), Log=FALSE), mvnpdfoptim(x=matrix(1.96, nrow = 2, ncol = n), Log=FALSE), mvnpdf_invC(x=matrix(1.96, nrow = 2, ncol = n), Log=FALSE), mvnpdfC(x=matrix(1.96, nrow = 2, ncol = n), mean = rep(0, 2), varcovM = diag(2), Log=FALSE), times=100L) mb expression<-c('dmvnorm', 'mvnpdf', 'mvnpdfsmart', 'mvnpdfoptim', 'mvnpdf_invC', 'mvnpdfC') autoplot(mb, expression)
Let's try to calculate the logarithm of $n$ numbers.
mb <- microbenchmark(log_par(1:100), log_seq(1:100), times=50) mb expression <- c('log_par', 'log_seq') autoplot(mb, expression)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.