首页 技术 正文
技术 2022年11月8日
0 收藏 504 点赞 1,932 浏览 2959 个字

二项分布

  • 需求:5个四面体筛子,筛子三面绿色,一面红色,模拟1000000次,统计每次试验红色落地筛子个数的分布
  • 实现:用循环实现5个筛子和1000000次试验,定义函数numRedDown模拟5个筛子试验结果,redDown模拟单次试验结果

Simulation.java

 1 import java.util.Random;
2
3 public class Simulation{
4 static final Random RANDOM = new Random();
5 static final int n = 5;
6 static final int N = 1000000;
7
8 public static void main(String[] args){
9 double[] dist = new double[n+1];
10 for(int i = 0; i < N ; i++){
11 int x = numRedDown(n);
12 ++dist[x];
13 }
14 for(int i = 0; i <=n;i++){
15 System.out.printf("%4d%8.4f%n",i,dist[i]/N);
16 }
17 }
18
19 static boolean redDown(){
20 int m = RANDOM.nextInt(4);
21 return (m==0);
22 }
23
24 static int numRedDown(int n){
25 int numRed = 0;
26 for(int i = 0; i < n; i++){
27 if(redDown()){
28 ++numRed;
29 }
30 }
31 return numRed;
32 }
33 }

BinomialDistrabutionTester.java

 1 import org.apache.commons.math3.distribution.BinomialDistribution;
2
3 public class BinomialDistributionTester {
4 static final int n = 5;
5 static final double p = 0.25;
6
7 public static void main(String[] args) {
8 BinomialDistribution bd = new BinomialDistribution(n, p);
9 for (int x = 0; x <= n; x++) {
10 System.out.printf("%4d%8.4f%n", x, bd.probability(x));
11 }
12 System.out.printf("mean = %6.4f%n", bd.getNumericalMean());
13 double variance = bd.getNumericalVariance();
14 double stdv = Math.sqrt(variance);
15 System.out.printf("standard deviation = %6.4f%n", stdv);
16 }
17 }

0 0.2381
1 0.3954
2 0.2629
3 0.0880
4 0.0145
5 0.0010

协方差

  • 需求:生成1000个随机数对(x,y),并计算x和y的相关系数
  • 实现:Apache Commons Math library 中相应方法

 1 import java.util.Random;
2 import org.apache.commons.math3.stat.correlation.Covariance;
3 import org.apache.commons.math3.stat.descriptive.moment.Variance;
4
5 public class CorrelationExample {
6 static final Random RANDOM = new Random();
7 static double[][] data1 = random(1000);
8 static double[][] data2 = {{1, 2, 3, 4, 5}, {1, 3, 5, 7, 9}};
9 static double[][] data3 = {{1, 2, 3, 4, 5}, {9, 8, 7, 6, 5}};
10
11 public static void main(String[] args) {
12 System.out.printf("rho1 = %6.3f%n", rho(data1));
13 System.out.printf("rho2 = %6.3f%n", rho(data2));
14 System.out.printf("rho3 = %6.3f%n", rho(data3));
15 }
16
17 static double[][] random(int n) {
18 double[][] a = new double[2][n];
19 for (int i = 0; i < n; i++) {
20 a[0][i] = RANDOM.nextDouble();
21 a[1][i] = RANDOM.nextDouble();
22 }
23 return a;
24 }
25
26 static double rho(double[][] data) {
27 Variance v = new Variance();
28 double varX = v.evaluate(data[0]);
29 double sigX = Math.sqrt(varX);
30 double varY = v.evaluate(data[1]);
31 double sigY = Math.sqrt(varY);
32 Covariance c = new Covariance(data);
33 double sigXY = c.covariance(data[0], data[1]);
34 return sigXY/(sigX*sigY);
35 }
36 }

rho1 = -0.036
rho2 = 1.000
rho3 = -1.000

正态分布

  • 需求:模拟均值16,标准差2.82的正态分布
  • 实现:Apache Commons Math library 的 NomorDistribution类

 1 import org.apache.commons.math3.distribution.NormalDistribution;
2
3 public class NormalDistributionTester {
4 static int n = 32;
5 static double p = 0.5;
6 static double mu = n*p;
7 static double sigma = Math.sqrt(n*p*(1-p));
8
9 public static void main(String[] args) {
10 NormalDistribution nd = new NormalDistribution(mu, sigma);
11
12 double a = 17.5, b = 21.5;
13 double Fa = nd.cumulativeProbability(a);
14 System.out.printf("F(a) = %6.4f%n", Fa);
15 double Fb = nd.cumulativeProbability(b);
16 System.out.printf("F(b) = %6.4f%n", Fb);
17 System.out.printf("F(b) - F(a) = %6.4f%n", Fb - Fa);
18 }
19 }

F(a) = 0.7021
F(b) = 0.9741
F(b) – F(a) = 0.2720

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,028
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,367
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,858