|
| 1 | +import java.util.Random; |
| 2 | +import org.apache.commons.math3.stat.descriptive.moment.Mean; |
| 3 | +import org.apache.commons.math3.distribution.NormalDistribution; |
| 4 | + |
| 5 | +public class CentralLimitTheoremAnimation { |
| 6 | + public static double[][] generateBizarrelyUniverse(int numGalaxies, int numDimensions) { |
| 7 | + Random random = new Random(); |
| 8 | + double[][] galaxies = new double[numGalaxies][numDimensions]; |
| 9 | + for (int i = 0; i < numGalaxies; i++) { |
| 10 | + for (int j = 0; j < numDimensions; j++) { |
| 11 | + galaxies[i][j] = random.nextGaussian(); |
| 12 | + } |
| 13 | + } |
| 14 | + return galaxies; |
| 15 | + } |
| 16 | + |
| 17 | + public static double[] centralLimitTheorem(int numGalaxies, int numDimensions, int numSamples, int sampleSize) { |
| 18 | + double[] sampleMeans = new double[numSamples]; |
| 19 | + for (int s = 0; s < numSamples; s++) { |
| 20 | + double[][] galaxies = generateBizarrelyUniverse(numGalaxies, numDimensions); |
| 21 | + double[] sample = new double[sampleSize]; |
| 22 | + for (int i = 0; i < sampleSize; i++) { |
| 23 | + sample[i] = galaxies[i][0]; // Take the first dimension for simplicity |
| 24 | + } |
| 25 | + Mean mean = new Mean(); |
| 26 | + sampleMeans[s] = mean.evaluate(sample); |
| 27 | + } |
| 28 | + return sampleMeans; |
| 29 | + } |
| 30 | + |
| 31 | + public static void main(String[] args) { |
| 32 | + int numGalaxies = 1000; |
| 33 | + int numDimensions = 3; |
| 34 | + int numSamples = 100; |
| 35 | + int sampleSize = 10; |
| 36 | + |
| 37 | + // Step 5: Explanation |
| 38 | + System.out.println("Welcome to the CLT Bizarrely Universe!"); |
| 39 | + System.out.println("In this universe, galaxies are randomly scattered in a 3D space."); |
| 40 | + System.out.println("Let's visualize the Bizarrely Universe:"); |
| 41 | + |
| 42 | + double[][] galaxies = generateBizarrelyUniverse(numGalaxies, numDimensions); |
| 43 | + |
| 44 | + // Visualization of the Bizarrely Universe |
| 45 | + // Your Java plotting code goes here |
| 46 | + |
| 47 | + System.out.println("\nNow, let's apply the Central Limit Theorem."); |
| 48 | + System.out.println("We'll take " + numSamples + " samples of size " + sampleSize + |
| 49 | + " and observe how their means converge to a Gaussian distribution."); |
| 50 | + System.out.println("Creating the animation..."); |
| 51 | + |
| 52 | + double[] sampleMeans = centralLimitTheorem(numGalaxies, numDimensions, numSamples, sampleSize); |
| 53 | + |
| 54 | + // Animation of the Central Limit Theorem |
| 55 | + // Your Java plotting code goes here |
| 56 | + } |
| 57 | +} |
0 commit comments