简体   繁体   中英

How to shift data in a distribution in Java

I am designing a software in Java, one of its functionalities is calculating the cumulative distribution of certain value in the distribution.

For example: The average marriage age in a country 28 old (which is the mean in the distribution), the distribution that i am using is chi-square (class ChiSquaredDistribution) with degree of freedom(3), since it resembles age at marriage distribution in the real world.

My goal is: if the user type their age, the output would be an approximate percentage of them getting married at that age (one year boundary) based on that distribution. something like: input : 30 years >>> output : 5.1% , input : 28 years>>> output :6% , input : 56 years>>> output :0.8% . The input is int, output is double

the problem is, the distribution starts at (0), and the mean is i believe (3) by default, the following code i wrote displays marriage probability from the age 0 to 70, my question is how to shift it to 18 and over, with the mean of the average age at marriage ?

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);  
}

Two images attached for current results, and the intended results. Any code and help would highly be appreciated.

See the current results and the desired results

Shift your distribution by subtracting 18 from each value, so 18 maps to 0, 28 maps to 10, 70 maps to 52, etc. The mean of an unshifted chi-square is its degrees of freedom. Using a chi-square(3) would yield a mean of 21 for the shifted data, so you'll want to bump that up to a chi-square(10) to yield a mean of 28 with the shift.

With some cleanup (lower-case start for local variables, r was unused), the shifted version is:

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);  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM