zoukankan      html  css  js  c++  java
  • [Java] 数据分析--统计

    二项分布

    • 需求: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 }
    View Code

    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 }
    View Code

    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 }
    View Code

    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 }
    View Code

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

  • 相关阅读:
    sqlite3 增删改查
    Charles 修改接口返回值
    矫正Django 时间不正确
    unittest 使用 HTMLTestRunner 生成测试报告
    unittest 使用例子
    pyppeteer
    linux源码编译-安装timescaledb数据库(中标麒麟+龙芯CPU) (转载)
    不会用java api对kafka topic进行创建、查询和删除,你也太out了(建议收藏)(转载)
    django 学习(转载)
    Docker 启动镜像(转载)
  • 原文地址:https://www.cnblogs.com/cxc1357/p/14683652.html
Copyright © 2011-2022 走看看