Java 中的 z 分数和 p 值(生存函数)

zScore and p-value in Java (survival function)

以下代码的 java 等价物是什么?

import scipy
from scipy.stats import zscore
zlist = [9967,11281,10752,10576,2366,11882,11798,]
z = zscore(zlist)
for e in z:
    print e,scipy.stats.norm.sf(abs(e))

答案是:

private void run() {
    double[] values = {9967,11281,10752,10576,2366,11882,11798};
    double variance = StatUtils.populationVariance(values);
    double sd = Math.sqrt(variance);
    double mean = StatUtils.mean(values);
    NormalDistribution nd = new NormalDistribution();
    for ( double value: values ) {
        double stdscore = (value-mean)/sd;
        double sf = 1.0 - nd.cumulativeProbability(Math.abs(stdscore));
        System.out.println("" + stdscore + " " + sf);
    }
}

这是使用The Apache Commons Mathematics Library

编辑:或者,甚至更好:

import java.util.function.BiConsumer;

import org.apache.commons.math3.distribution.NormalDistribution;
import org.apache.commons.math3.distribution.RealDistribution;
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;

public class ZScore {
    public static void main(String[] args) {
        ZScore program = new ZScore();
        double[] values = {9967,11281,10752,10576,2366,11882,11798};
        program.computeZScoreAndSurvivalFunctions(
            new DescriptiveStatistics(values), 
            new NormalDistribution(), 
            (zscore, sf)->System.out.println(""+zscore+" "+sf)
        );
    }

    private void computeZScoreAndSurvivalFunctions(
        DescriptiveStatistics ds, 
        RealDistribution dist, 
        BiConsumer<Double, Double> consumer
    ) {
        double variance = ds.getPopulationVariance();
        double sd = Math.sqrt(variance);
        double mean = ds.getMean();
        for ( int index = 0; index < ds.getN(); ++index) {
            double zscore = (ds.getElement(index)-mean)/sd;
            double sf = 1.0 - dist.cumulativeProbability(Math.abs(zscore));
            consumer.accept(zscore, sf);
        }
    }
}