2

I am working on a C++ project that often requires the computation of Gaussian pdf given a data point x and an existing Gaussian distribution G.

This is expensive since the exponential function exp is involved. Even if I take log, the log function is costly as well. Any suggestions about how I can do it?

wanderer
  • 1,219
  • 1
  • 10
  • 10

2 Answers2

3

Vectorize it, that is, compute the exponents or logs in parallel using SIMD, you can also use optimized approximating SSE based exp and log if you don't need extreme accuracy, a simple lib for that can be found here.

However, when it comes to optimizing, profile first, that way you fix the problem, not what you think is the problem.

Necrolis
  • 25,836
  • 3
  • 63
  • 101
0

The log pdf isn't expensive, if you use the following shortcut:

Starting with

 log_pdf = log (1.0/ (sigma * 2.0 * pi))  - 0.5 * square(x-mean) / ( sigma*sigma );

you can see that the part of the term containing the log can be pre-calculated for any particular PDF, as can part of the rest. So for any given values for the standard deviation and the mean:

log_k = log (1.0/ (sigma * 2.0 * pi));
half_over_sigma_sq= 0.5 / (sigma*sigma)

Then when evaluating for lots of different values of x, you can calculate just

log_pdf = log_k - half_over_sigma_sq * square(x-mean);

This trick is used all the time in statistical modelling.

Ian
  • 336
  • 1
  • 4