Math functions
These functions are implemented in the MathFunctions class.
Factorial : x!
For a general explanation, see this article on factorial.
public static long MathFunctions.factorial(int n);
Normal Quantile function
For a general explanation, see these articles on the Quantile function and the normal distribution quantile function.
public static double MathFunctions.normInv(double p); // Normal distribution is normalised
public static double MathFunctions.normInv(double p, double mu, double sigma); // Normal distribution is not normalised
For example, it can be used to compute a VaR based on a centered normal distribution.
double scalarVarFormula(IVector pnl, double quantile, double timePeriod) {
double zValue = MathFunctions.normInv(quantile);
return Math.sqrt(Metric.AVGSQ.ofVector(pnl) * timePeriod) / zValue;
}
Statistical aggregators
These comprise a list of stream aggregators that pop up statistical measures.
Collector | Explanation | Formula (excel like) |
---|---|---|
MathFunctions.Metric.SUM | Sum of the stream | SUM(…) |
MathFunctions.Metric.AVERAGE | Mean of the stream | AVERAGE(…) |
MathFunctions.Metric.STDEV_S | Standard deviation of the stream representing a sample of the global population | STDEV.S(…) = SQRT(VAR.S(…)) |
MathFunctions.Metric.VAR_S | Variance of the stream representing a sample of the global population | VAR.S(…) = (SUMSQ(…) / COUNT(…) - (SUM(…) / COUNT(…)) ^ 2) * COUNT(…) / (COUNT(…) - 1) |
MathFunctions.Metric.STDEV_P | Standard deviation of the stream population | STDEV.P(…) = SQRT(VAR.P(…)) |
MathFunctions.Metric.VAR_P | Variance of the stream population | VAR.P(…) = (SUMSQ(…) / COUNT(…) - (SUM(…) / COUNT(…)) ^ 2) |
MathFunctions.Metric.SUMSQ | Sum of the square of the stream | SUMSQ(…) = SUM(…^2) |
MathFunctions.Metric.RMS | Root mean square of the stream | SQRT(SUMSQ(…) / COUNT(…)) |
MathFunctions.Metric.AVGSQ | Mean square of the stream | SUMSQ(…) / COUNT(…) |
Usage :
MathFunctions.Metric metric = MathFunctions.Metric.valueOf(properties.getProperty(METRIC));
// For a vector
IVector vector;
double value = metric.ofVector(vector);
// For a double stream
double[] array;
DoubleStream stream = Arrays.stream(array);
double value = getCollector().applyAsDouble(stream);