概述:
正态分布是统计学中常用的一种概率分布。当数据量足够大时,很多分布近似于正态分布,因此它是常用的一种分布。这里主要是介绍,在Java中我们可以使用一些库和方法来实现正态分布。
1.正态分布在Java中的运行要求:
除了正常的Java运行环境外,需要下载以下工具包;
Numbers – Apache Commons Numbers;
2.具体的实现方法:
下面以一段代码来介绍:
package NormalDistribution;
import org.apache.commons.statistics.distribution.*;
public class NorDisExample {
public static void main(String[] args) {
normalDistribution( 0.0, 1.0);
}
/**
* @param mean 均值
* @param sd 标准差 Standard Deviation
* @param x 需要计算的点
*/
public static void normalDistribution(Double mean, Double sd) {
NormalDistribution normalDistribution = NormalDistribution.of (mean, sd);
// 设定需要计算的某点
double x= 1.0;
// 设定需要计算的概率区间
double x0= 0.0;
double x1= 3.0;
// 计算某点概率密度(PDF)
double pdf = normalDistribution.density(x);
System.out.println("PDF: " + pdf);
// 计算某区间累积概率(CDF)
double cdf = normalDistribution.probability( x0, x1) ;
System.out.println("CDF: " + pdf);
}
}
//验算方法:区间[0,3]的累积概率加上0.5应该等于0.9987左右。
引入类包:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.statistics.distribution;
import org.apache.commons.numbers.gamma.Erfc;
import org.apache.commons.numbers.gamma.InverseErfc;
import org.apache.commons.numbers.gamma.ErfDifference;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.sampling.distribution.GaussianSampler;
import org.apache.commons.rng.sampling.distribution.ZigguratSampler;
运算结果截图:
3.结语:
之前有人使用Apache Commons Math3的库来计算正态分布函数,在Math4时,采用Apache Commons Statistics来处理,用Numbers – Apache Commons Numbers和RNG – Commons RNG配合使用。
4.参考资料:
[Apache Commons 官方文档]
java正态分布函数_mob64ca12d06991的技术博客_51CTO博客 [原创] https://blog.51cto.com/u_16213300/6957923
正态分布和Java实现 小鱼干换酒钱 [原创] http://t.csdnimg.cn/gBzhg