如何在 Java 中的分布中移动数据

How to shift data in a distribution in Java

我在Java设计一个软件,其中一个功能是计算分布中某个值的累积分布。

例如:一个国家的平均结婚年龄为 28 岁(这是分布中的平均值),我使用的分布是 chi-square (class ChiSquaredDistribution),自由度为 (3),因为它类似于现实世界中的结婚年龄分布。

我的目标是:如果用户输入他们的年龄,输出将是他们根据该分布在该年龄(一年边界)结婚的大约百分比。类似于:input : 30 years >>> output : 5.1%input : 28 years>>> output :6%input : 56 years>>> output :0.8%。输入为 int,输出为 double

问题是,分布从 (0) 开始,默认情况下我相信均值为 (3),我写的以下代码显示 0 到 70 岁的结婚概率,我的问题是如何转移到 18 岁及以上,平均结婚年龄?

ChiSquaredDistribution x = new ChiSquaredDistribution(3); 
Random r = new Random();
for (int UserAtAge=0; UserAtAge<70; UserAtAge++) {
    System.out.println((x.cumulativeProbability(UserAtAge+1)-x.cumulativeProbability(UserAtAge))*100);  
}

为当前结果和预期结果附上两张图片。任何代码和帮助将不胜感激。

见current results and the desired results

通过从每个值中减去 18 来移动您的分布,因此 18 映射到 0,28 映射到 10,70 映射到 52,等等。未移动的卡方的平均值是它的自由度。使用卡方 (3) 会产生移位数据的平均值 21,因此您需要将其提高到卡方 (10) 以产生移位后的平均值 28。

经过一些清理(局部变量的小写开头,r 未使用),转换后的版本是:

ChiSquaredDistribution x = new ChiSquaredDistribution(10); 
for (int userAge=18; userAge<71; userAge++) {
    System.out.println((x.cumulativeProbability(userAge + 1 - 18) - x.cumulativeProbability(userAge - 18)) * 100);  
}