-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExample3.java
More file actions
60 lines (42 loc) · 1.5 KB
/
Example3.java
File metadata and controls
60 lines (42 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package applications.statistics;
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
import utils.ListMaths;
import utils.ListUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/** Category: Statistics
* ID: Example1
* Description: Simulate the standard error for the mean is 1/sqrt(n)
* Taken From:
* Details:
* TODO
*/
public class Example3 {
public static List<Double> getNormalSample(double mu, double sd, int n){
List<Double> sample = new ArrayList<>(n);
Random rnd = new Random();
for(int i=0; i<n; ++i){
double value = rnd.nextGaussian()*sd + mu;
sample.add(value);
}
return sample;
}
public static void main(String[] args){
final double N_SIM = 1000;
final int N = 10;
final double MU = 0.0;
final double SIGMA = 1.0;
List<Double> means = new ArrayList<Double>();
for( int itr=0; itr < N_SIM; ++itr){
List<Double> sample = Example3.getNormalSample(MU, SIGMA, N);
double mean = ListMaths.sum(sample)/((double)sample.size());
//System.out.println(mean);
means.add(mean);
}
double[] vals = ListUtils.toDoubleArray(means);
DescriptiveStatistics stats = new DescriptiveStatistics(vals );
System.out.println("Standard deviation of means is: "+stats.getStandardDeviation());
System.out.println("1/sqrt(N) is: " + 1.0/Math.sqrt(N));
}
}