Statistics/Chi-squared distribution: Difference between revisions
Content added Content deleted
(Created Nim solution.) |
(New post.) |
||
Line 87: | Line 87: | ||
<br /><br /><br /> |
<br /><br /><br /> |
||
=={{header|Java}}== |
|||
<syntaxhighlight lang="java"> |
|||
import java.util.List; |
|||
public final class StatisticsChiSquaredDistribution { |
|||
public static void main(String[] aArgs) { |
|||
System.out.println(" Values of the Chi-squared probability distribution function"); |
|||
System.out.println(" x/k 1 2 3 4 5"); |
|||
for ( int x = 0; x <= 10; x++ ) { |
|||
System.out.print(String.format("%2d", x)); |
|||
for ( int k = 1; k <= 5; k++ ) { |
|||
System.out.print(String.format("%10.6f", pdf(x, k))); |
|||
} |
|||
System.out.println(); |
|||
} |
|||
System.out.println(); |
|||
System.out.println(" Values for the Chi-squared distribution with 3 degrees of freedom"); |
|||
System.out.println("Chi-squared cumulative pdf p-value"); |
|||
for ( int x : List.of( 1, 2, 4, 8, 16, 32 ) ) { |
|||
final double cdf = cdf(x, 3); |
|||
System.out.println(String.format("%5d%20.6f%20.6f", x, cdf, ( 1.0 - cdf ))); |
|||
} |
|||
final int[][] observed = { { 77, 23 }, { 88, 12 }, { 79, 21 }, { 81, 19 } }; |
|||
final double[][] expected = { { 81.25, 18.75 }, { 81.25, 18.75 }, { 81.25, 18.75 }, { 81.25, 18.75 } }; |
|||
double testStatistic = 0.0; |
|||
for ( int i = 0; i < observed.length; i++ ) { |
|||
for ( int j = 0; j < observed[0].length; j++ ) { |
|||
testStatistic += Math.pow(observed[i][j] - expected[i][j], 2.0) / expected[i][j]; |
|||
} |
|||
} |
|||
final int degreesFreedom = ( observed.length - 1 ) / ( observed[0].length - 1 ); |
|||
System.out.println(); |
|||
System.out.println("For the airport data:"); |
|||
System.out.println(" test statistic : " + String.format("%.6f", testStatistic)); |
|||
System.out.println(" degrees of freedom : " + degreesFreedom); |
|||
System.out.println(" Chi-squared : " + String.format("%.6f", pdf(testStatistic, degreesFreedom))); |
|||
System.out.println(" p-value : " + String.format("%.6f", cdf(testStatistic, degreesFreedom))); |
|||
} |
|||
// The gamma function. |
|||
private static double gamma(double aX) { |
|||
if ( aX < 0.5 ) { |
|||
return Math.PI / ( Math.sin(Math.PI * aX ) * gamma(1.0 - aX) ); |
|||
} |
|||
final double[] probabilities = new double[] { |
|||
0.99999999999980993, 676.5203681218851, -1259.1392167224028, 771.32342877765313, -176.61502916214059, |
|||
12.507343278686905, -0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7 }; |
|||
aX -= 1.0; |
|||
double t = probabilities[0]; |
|||
for ( int i = 1; i < 9; i++ ) { |
|||
t += probabilities[i] / ( aX + i ); |
|||
} |
|||
final double w = aX + 7.5; |
|||
return Math.sqrt(2.0 * Math.PI) * Math.pow(w, aX + 0.5) * Math.exp(-w) * t; |
|||
} |
|||
// The probability density function of the Chi-squared distribution. |
|||
private static double pdf(double aX, double aK) { |
|||
return ( aX > 0.0 ) ? |
|||
Math.pow(aX, aK / 2 - 1) * Math.exp(-aX / 2) / ( Math.pow(2, aK / 2) * gamma(aK / 2) ) : 0.0; |
|||
} |
|||
// The cumulative probability function of the Chi-squared distribution. |
|||
private static double cdf(double aX, double aK) { |
|||
return ( aX > 0.0 && aK > 0.0 ) ? gammaCDF(aX / 2, aK / 2) : 0.0; |
|||
} |
|||
// The normalised lower incomplete gamma function. |
|||
private static double gammaCDF(double aX, double aK) { |
|||
double result = 0.0; |
|||
for ( int m = 0; m <= 99; m++ ) { |
|||
result += Math.pow(aX, m) / gamma(aK + m + 1); |
|||
} |
|||
result *= Math.pow(aX, aK) * Math.exp(-aX); |
|||
return result; |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
{{ out }} |
|||
<pre> |
|||
Values of the Chi-squared probability distribution function |
|||
x/k 1 2 3 4 5 |
|||
0 0.000000 0.000000 0.000000 0.000000 0.000000 |
|||
1 0.241971 0.303265 0.241971 0.151633 0.080657 |
|||
2 0.103777 0.183940 0.207554 0.183940 0.138369 |
|||
3 0.051393 0.111565 0.154180 0.167348 0.154180 |
|||
4 0.026995 0.067668 0.107982 0.135335 0.143976 |
|||
5 0.014645 0.041042 0.073225 0.102606 0.122042 |
|||
6 0.008109 0.024894 0.048652 0.074681 0.097304 |
|||
7 0.004553 0.015099 0.031873 0.052845 0.074371 |
|||
8 0.002583 0.009158 0.020667 0.036631 0.055112 |
|||
9 0.001477 0.005554 0.013296 0.024995 0.039887 |
|||
10 0.000850 0.003369 0.008500 0.016845 0.028335 |
|||
Values for the Chi-squared distribution with 3 degrees of freedom |
|||
Chi-squared cumulative pdf p-value |
|||
1 0.198748 0.801252 |
|||
2 0.427593 0.572407 |
|||
4 0.738536 0.261464 |
|||
8 0.953988 0.046012 |
|||
16 0.998866 0.001134 |
|||
32 0.999999 0.000001 |
|||
For the airport data: |
|||
test statistic : 4.512821 |
|||
degrees of freedom : 3 |
|||
Chi-squared : 0.088754 |
|||
p-value : 0.788850 |
|||
</pre> |
|||
=={{header|jq}}== |
=={{header|jq}}== |