Change Dir

          先知cd——熱愛生活是一切藝術(shù)的開始

          統(tǒng)計(jì)

          留言簿(18)

          積分與排名

          “牛”們的博客

          各個(gè)公司技術(shù)

          我的鏈接

          淘寶技術(shù)

          閱讀排行榜

          評(píng)論排行榜

          Commons Math學(xué)習(xí)筆記——分布

           

          看其他篇章到目錄選擇。

          概率分布是概率論的一個(gè)基礎(chǔ)。

          Commons Math包中也專門有一個(gè)子包對(duì)概率分布進(jìn)行了封裝實(shí)現(xiàn)。在distribution包中,定義了一個(gè)基本接口Distribution。該接口只有兩個(gè)方法,一個(gè)是double cumulativeProbability(double x),一個(gè)是double cumulativeProbability(double x0, double x1)。前者對(duì)于服從某種分布的隨機(jī)變量X,返回P(X<=x);后者則返回P(x0<=X<=x1)。正如其名所示,這樣也就得到了概率。

          具體distribution包中實(shí)現(xiàn)了基本所有的概率分布,分為連續(xù)型分布和離散型分布,連續(xù)型包括了像熟悉的指數(shù)分布、柯西分布等,離散型包括了泊松分布和二項(xiàng)分布等等。具體的類圖結(jié)構(gòu)見下圖:



           

          在連續(xù)型的ContinuousDistribution接口中,添加了一個(gè)double inverseCumulativeProbability(double p)的方法,這個(gè)方法返回P(X<x)=p中的x。也就是說通過已知概率,可以求得隨機(jī)變量Xx范圍。當(dāng)然看api文檔還應(yīng)注意一句,在3.0的版本中會(huì)加入public double density(double x)這個(gè)求概率密度函數(shù)的方法,敬請(qǐng)期待吧。離散型的接口DiscreteDistribution中則添加了double probability(double x)方法,用來計(jì)算P(X=x)的概率。

          具體的代碼我們以離散型的泊松分布和連續(xù)型的正態(tài)分布來講解。泊松分布的接口繼承了IntegerDistribution接口,在此基礎(chǔ)上加了getMean()方法和normalApproximateProbability()方法。正態(tài)分布NormalDistribution繼承了ContinuousDistribution,又添加了getMean()方法和double density(double x)方法以及getStandardDeviation()方法。

           

           1/**
           2 * 
           3 */
           4package algorithm.math;
           5
           6import org.apache.commons.math.MathException;
           7import org.apache.commons.math.distribution.NormalDistribution;
           8import org.apache.commons.math.distribution.NormalDistributionImpl;
           9import org.apache.commons.math.distribution.PoissonDistribution;
          10import org.apache.commons.math.distribution.PoissonDistributionImpl;
          11
          12/**
          13 * @author Jia Yu
          14 * @date   2010-11-28
          15 */
          16public class DistributionTest {
          17
          18    /**
          19     * @param args
          20     */
          21    public static void main(String[] args) {
          22        // TODO Auto-generated method stub
          23        poisson();
          24        System.out.println("------------------------------------------");
          25        normal();
          26        test();
          27    }
          28
          29    /**
          30     * test for example
          31     * 《飲料裝填量不足與超量的概率》
          32     * 某飲料公司裝瓶流程嚴(yán)謹(jǐn),每罐飲料裝填量符合平均600毫升,標(biāo)準(zhǔn)差3毫升的常態(tài)分配法則。隨機(jī)選取一罐,容量超過605毫升的概率?容量小于590毫升的概率
          33     * 容量超過605毫升的概率 = p ( X > 605)= p ( ((X-μ) /σ) > ( (605 – 600/ 3) )= p ( Z > 5/3= p( Z > 1.67= 0.0475
          34     * 容量小于590毫升的概率 = p (X < 590= p ( ((X-μ) /σ) < ( (590 – 600/ 3) )= p ( Z < -10/3= p( Z < -3.33= 0.0004
          35     */
          36    private static void test() {
          37        // TODO Auto-generated method stub
          38        NormalDistribution normal = new NormalDistributionImpl(600,3);
          39        try {
          40            System.out.println("P(X<590) = "+normal.cumulativeProbability(590));
          41            System.out.println("P(X>605) = "+(1-normal.cumulativeProbability(605)));
          42        } catch (MathException e) {
          43            // TODO Auto-generated catch block
          44            e.printStackTrace();
          45        }
          46    }
          47
          48    private static void poisson() {
          49        // TODO Auto-generated method stub
          50        PoissonDistribution dist = new PoissonDistributionImpl(4.0);
          51        try {
          52            System.out.println("P(X<=2.0) = "+dist.cumulativeProbability(2.0));
          53            System.out.println("mean value is "+dist.getMean());
          54            System.out.println("P(X=1.0) = "+dist.probability(1.0));
          55            System.out.println("P(X=x)=0.8 where x = "+dist.inverseCumulativeProbability(0.8));
          56        } catch (MathException e) {
          57            // TODO Auto-generated catch block
          58            e.printStackTrace();
          59        }
          60    }
          61
          62    private static void normal() {
          63        // TODO Auto-generated method stub
          64        NormalDistribution normal = new NormalDistributionImpl(0,1);
          65        try {
          66            System.out.println("P(X<2.0) = "+normal.cumulativeProbability(2.0));
          67            System.out.println("mean value is "+normal.getMean());
          68            System.out.println("standard deviation is "+normal.getStandardDeviation());
          69            System.out.println("P(X=1) = "+normal.density(1.0));
          70            System.out.println("P(X<x)=0.8 where x = "+normal.inverseCumulativeProbability(0.8));
          71        } catch (MathException e) {
          72            // TODO Auto-generated catch block
          73            e.printStackTrace();
          74        }
          75    }
          76
          77}
          78

           

           

          輸出:
          P(X<=2.0) = 0.23810330555354414
          mean value is 4.0
          P(X=1.0) = 0.07326255555493674
          P(X=x)=0.8 where x = 5
          ------------------------------------------
          P(X<2.0) = 0.9772498680518208
          mean value is 0.0
          standard deviation is 1.0
          P(X=1) = 0.24197072451914337
          P(X<x)=0.8 where x = 0.8416212335731417
          P(X<590) = 4.290603331968401E-4
          P(X>605) = 0.047790352272814696


          泊松分布只需要給定參數(shù)
          λ即可,而其期望就是λ。所以構(gòu)造方法一般就是new PoissonDistributionImpl(double mean)這樣的形式了。

          正態(tài)分布需要知道均值和方差,因此要在構(gòu)造函數(shù)時(shí)傳入,另外程序中以一個(gè)維基百科上的示例來驗(yàn)證正態(tài)分布的正確性。

          相關(guān)資料:

          概率分布:http://zh.wikipedia.org/zh-cn/%E6%A6%82%E7%8E%87%E5%88%86%E5%B8%83

          泊松分布:http://zh.wikipedia.org/zh-cn/%E6%B3%8A%E6%9D%BE%E5%88%86%E5%B8%83

          正態(tài)分布:http://zh.wikipedia.org/zh-cn/%E6%AD%A3%E6%80%81%E5%88%86%E5%B8%83

          Commons math包:http://commons.apache.org/math/index.html

          posted on 2010-12-23 20:03 changedi 閱讀(5140) 評(píng)論(0)  編輯  收藏 所屬分類: 數(shù)學(xué)

          主站蜘蛛池模板: 龙里县| 洛浦县| 庆云县| 盐边县| 乐清市| 澎湖县| 阳信县| 那曲县| 东港市| 商河县| 邵阳市| 凤冈县| 新津县| 繁峙县| 石家庄市| 通道| 甘洛县| 惠来县| 滦南县| 楚雄市| 宁国市| 抚顺县| 赤壁市| 射阳县| 武隆县| 林西县| 平顶山市| 富宁县| 诏安县| 习水县| 华阴市| 白沙| 同仁县| 吉木乃县| 通化市| 永登县| 西贡区| 崇州市| 海安县| 西青区| 三穗县|