/*
This is a proprietary part of DataMelt package.
License: the Creative Commons Attribution-Share Alike License; either version 3.0 of the License,
or any later version.
http://creativecommons.org/licenses/by-sa/3.0/
Permission to use, copy and modify this software and its documentation for
NON-COMMERCIAL purposes is granted,
without fee, provided that an acknowledgement to the author, Dr Sergei Chekanov
appears in all copies and associated documentation or publications.
Redistributions of the source code, or parts of the source codes,
must retain the copyright notice, this list of conditions and the following disclaimer
(all at the top of each source code) and requires the written permission of Sergei Chekanov
Redistribution in binary form of all or parts of a class must reproduce the
copyright notice, this list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution and requires the written permission of Sergei Chekanov
Details about license agreement or commercial use are: http://jwork.org/scavis
*/
package jhpro.stat;
import jhplot.P1D;
import jhplot.gui.HelpBrowser;
import org.apache.commons.math3.util.FastMath;
/**
* Calculations of moments of a distribution. The moments are calculated as
* 1/Events sum^(events) (x^n), where n is the oder of the moment. Statistical
* errors are also evaluated.
*
* @author S.Chekanov
*
*/
public class Moments {
private int oder = 0;
private double[] IL, IE;
private int nev;
/**
* Initialization of the moments.
*
* @param order
* Max oder of the moment
*/
public Moments(int order) {
nev = 0;
this.oder = order;
IL = new double[this.oder];
IE = new double[this.oder];
for (int i = 0; i < this.oder; i++) {
IL[i] = 0;
IE[i] = 0;
}
}
/**
* Process current event for an integer random variable
*
* @param n
* input variable for the moment calculations
*/
public void process(int n) {
nev++;
for (int i = 0; i < this.oder; i++) {
IL[i] = IL[i] + FastMath.pow(n, (i + 1));
IE[i] = IE[i] + FastMath.pow(n, 2 * (i + 1));
};
}
/**
* Process current event for a double random variable
*
* @param x
* input variable for the moment calculations
*/
public void process(double n) {
nev++;
for (int i = 0; i < this.oder; i++) {
IL[i] = IL[i] + FastMath.pow(n, (i + 1));
IE[i] = IE[i] + FastMath.pow(n, 2 * (i + 1));
};
}
/**
* Get values of the moments
*
* @return
*/
public double[] getValues() {
double[] IM = new double[this.oder];
for (int i = 0; i < this.oder; i++)
IM[i] = IL[i] / (double) (nev - 1);
return IM;
}
/**
* Get errors on the moment calculations
*
* @return
*/
public double[] getErrors() {
double[] IEE = new double[this.oder];
for (int i = 0; i < this.oder; i++) {
double X1 = IL[i] / (double) (nev - 1);
double X2 = IE[i] / (double) (nev - 1);
double VAR = X2 - X1 * X1;
IEE[i] = 0;
if (VAR > 0)
IEE[i] = FastMath.sqrt(VAR / (double) (nev - 1));
}
return IEE;
}
/**
* Get results in form of P1D.
*
*
* @return p1d with values and errors
*/
public P1D getResults() {
P1D tmp = new P1D("Moments");
double[] val=getValues();
double[] err=getErrors();
for (int i = 0; i < this.oder; i++) {
tmp.add(i+1,val[i], err[i]);
}
return tmp;
}
/**
* Show online documentation.
*/
public void doc() {
String a = this.getClass().getName();
a = a.replace(".", "/") + ".html";
new HelpBrowser(HelpBrowser.JHPLOT_HTTP + a);
}
}
Ads help maintain this website.