Blog Archives

Munkres’ Assignment Algorithm with RcppArmadillo

September 24, 2013
By
Munkres’ Assignment Algorithm with RcppArmadillo

Munkres’ Assignment Algorithm (Munkres (1957), also known as hungarian algorithm) is a well known algorithm in Operations Research solving the problem to optimally assign N jobs to N workers. I needed to solve the Minimal Assignment Problem for a relabeling algorithm in MCMC sampling for finite mixture distributions, where I use a random permutation Gibbs sampler. For each sample...

Read more »

Getting indices of top elements from a vector using a priority queue

September 12, 2013
By
Getting indices of top elements from a vector using a priority queue

This post is based on a question on Stack Overflow and more precisely on Martin Morgan’s answer. The problem is to find the indices of top n elements from a vector. An inefficient way of doing this is to run order on the vector and then only keep the last n values: top <- function(x, n){ tail( order(x),...

Read more »

First Derivative of the Multivariate Normal Densities with RcppArmadillo

September 9, 2013
By
First Derivative of the Multivariate Normal Densities with RcppArmadillo

There is a great RcppArmadillo implementation of multivariate normal densities. But I was looking for the first derivative of the multivariate normal densities. Good implementations are surprisingly hard to come by. I wasn’t able to find any online and my first solutions in R were pretty slow. RcppArmadillo might be a great alternative particularly because I am not...

Read more »

Creating as and wrap for sparse matrices

August 5, 2013
By
Creating as and wrap for sparse matrices

An earlier article discussed sparse matrix conversion but stopped short of showing how to create custom as<>() and wrap() methods or functions. This post starts to close this gap. We will again look at sparse matrices from the Matrix package for R, as well as the SpMat class from Armadillo.At least for now we will limit outselves...

Read more »

Gibbs Sampler in C++

August 3, 2013
By
Gibbs Sampler in C++

Markov Chain Monte Carlo (MCMC) is a popular simulation method. As it is somewhat demanding, it is also frequently used to benchmark different implementations or algorithms. One particular algorithm has been compared a number of times, starting with an article by Darren Wilkinson, and Darren’s follow–up article which in turns responded in part to our article. This post...

Read more »

Faster Multivariate Normal densities with RcppArmadillo and OpenMP

July 13, 2013
By
Faster Multivariate Normal densities with RcppArmadillo and OpenMP

The Multivariate Normal density function is used frequently in a number of problems. Especially for MCMC problems, fast evaluation is important. Multivariate Normal Likelihoods, Priors and mixtures of Multivariate Normals require numerous evaluations, thus speed of computation is vital. We show a twofold increase in speed by using RcppArmadillo, and some extra gain by using OpenMP. This project is based...

Read more »

Sobol Sensitivity Analysis

June 10, 2013
By
Sobol Sensitivity Analysis

Sensitivity analysis is the task of evaluating the sensitivity of a model output Y to input variables (X1,…,Xp). Quite often, it is assumed that this output is related to the input through a known function f :Y= f(X1,…,Xp). Sobol indices are generalizing the coefficient of the coefficient of determination in regression. The ith first order indice is the proportion of...

Read more »

Using RcppProgress to control the long computations in C++

May 16, 2013
By
Using RcppProgress to control the long computations in C++

Usually you write c++ code with R when you want to speedup some calculations. Depending on the parameters, and especially during the development, it is difficult to anticipate the execution time of your computation, so that you do not know if you have to wait for 1 minute or hours. RcppProgress is a tool to help you monitor the...

Read more »

An accept-reject sampler using RcppArmadillo::sample()

May 8, 2013
By
An accept-reject sampler using RcppArmadillo::sample()

The recently added RcppArmadillo::sample() functionality provides the same algorithm used in R’s sample() to Rcpp-level code. Because R’s own sample() is written in C with minimal work done in R, writing a wrapper around RcppArmadillo::sample() to then call in R won’t get you much of a performance boost. However, if you need to repeatedly call sample(), then calling a...

Read more »

An accept-reject sampler using RcppArmadillo::sample()

May 8, 2013
By
An accept-reject sampler using RcppArmadillo::sample()

The recently added RcppArmadillo::sample() functionality provides the same algorithm used in R’s sample() to Rcpp-level code. Because R’s own sample() is written in C with minimal work done in R, writing a wrapper around RcppArmadillo::sample() to then call in R won’t get you much of a performance boost. However, if you need to repeatedly call sample(), then calling a...

Read more »