2

I am trying to re-implement some R code in Java and because it will be integrated in other code, not controled by me, it really needs to be in Java.

The critical part is fitting data to a non-standardized t-distribution using the moments of the data (mean, variance, skewness, kurtosis). I.e. finding the parameters of the non-standardized t-distribution which best fits the data.

I've already found a backup solution of making a Java wrapper of R code using JRI. But is there any way I could do this in pure Java code?

patapouf_ai
  • 17,605
  • 13
  • 92
  • 132

1 Answers1

3

So it is actually not very complicated at all:

 /**
 * Returns the parameters of the Student T distribution which is fitted using the moments given.
 *
 * @param avg: estimated average.
 * @param variance: estimated variance.
 * @param kurtosis: estimated kurtosis.

 * @return double[] {location, scale, degrees of freedom}.
 * @throws Nothing!! (for now)
 */
public static double[] get_Tdistribution_params_from_moments(double avg, double variance, double kurtosis){
    double mu = avg; // localization parameter
    double nu = (6-4*kurtosis)/(3-kurtosis); // degree of freedom
    double sigma = Math.pow(((nu-2)/nu)*variance,0.5); // scale parameter
    return new double[] {mu,sigma, nu};
}
patapouf_ai
  • 17,605
  • 13
  • 92
  • 132