Question

In Apache commons Math there is a class Mean. I want to avoid instantiating a object every time I wanted to calculate a mean. Upon further examination I found a class StatUtils which has a static function that calculates a mean of a double[]. Now I want to evaluate the Gaussian function. I found a class Gaussian. But I still would rather not instantiate a object every time I want to evaluate the function. Is there a class similar to the StatUtils with static functions for evaluating the Gaussian function.

I considered instantiating a global object of type Mean to get access to the mean function without having to instantiate multiple Mean objects but when I found StatUtil I preferred that approach. For the Gaussian problem even a Global object is not acceptable since the constructor requires a mean and a sigma parameter. Therefore I would still have to instantiate a new object for each distribution.

What I am looking for is something like:

double y = gaussian(x, mean, sigma);

to summarize.

  1. Is there a static function to evaluate the Gaussian function.
  2. Is there a software design issue that is driving the Apache Math library design that I am missing. Is there something wrong with the StatUtils or Java.Math approach using static functions.
Was it helpful?

Solution

As long as you don't need the infrastructure with derivatives and parametrics that is associated with the original class, it should be possible to extract the relevant computation (which does not contain any real "state", but only depends on the three parameters that you mentioned) and place it into a single static method.

Based on the original Gaussian class, it should roughly look like this:

// Based on the Gaussian class from Apache Commons Math
public static double gaussian(double x, double mean, double sigma)
{
    double norm = 1 / (sigma * Math.sqrt(2 * Math.PI));
    double  is = 1 / sigma;
    double i2s2 = 0.5 * is * is;
    double xMinusMean = x - mean;
    return norm * Math.exp(-xMinusMean * xMinusMean * i2s2);
}

OTHER TIPS

Do you want the probability density function PDF or the cumulative density function CDF. The PDF is given by @Marco13 answer. For most statistical applications its the CDF which is most interesting finding the probability that x

What you can do is have one instance of org.apache.commons.math3.distribution.NormalDistribution which mean 0 and standard deviation 1. You can rescale the values to match

static NormalDistribution dist = new NormalDistribution();

static double density(double x, double mean, double sd) {
    double scaledx = (x-mean)/sd;
    return dist.density(scaledx);
}

static double cumulativeProbability(double x, double mean, double sd) {
    double scaledx = (x-mean)/sd;
    return dist.cumulativeProbability(scaledx);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top