Chinaunix首页 | 论坛 | 博客
  • 博客访问: 100411
  • 博文数量: 35
  • 博客积分: 1845
  • 博客等级: 上尉
  • 技术积分: 394
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-17 17:06
文章分类

全部博文(35)

文章存档

2013年(2)

2012年(2)

2011年(24)

2010年(3)

2009年(4)

我的朋友

分类: C/C++

2011-09-25 11:53:39

機率與統計理論沒學好,這一次用C++來實現PDF和CDF,實做果然比較有趣!

C所提供得亂數產生函式,所產生出來的亂數為uniform distribution,我直接拿來做pdf和cdf,一旦找出pdf,即可求出cdf。

bin和sample數越多,準確度越高!若sample小,bin小依然可見pdf的曲線,至於cdf則影響小。

01 /**
02     Theme: PDF & CDF
03     Date: 100/06/18
04     compiler: Dev C++ 4.9.9.2
05     Author: ShengWen
06     Blog:
07 */
08 #include
09 #include
10 #define NR_SAMPLES 1000000
11 #define NR_BIN 10
12 using namespace std;
13 int main(){
14     int pdf[NR_BIN]={};//PDF桶子
15     int cdf[NR_BIN]={};//CDF桶子
16     srand(time(NULL));//隨機亂數種子
17     double x;//uniform distribution
18     //計算PDF
19     for(int i=0; i
20         x = rand() / (double)RAND_MAX;//RAND_MAX=32767
21         pdf[(int)(NR_BIN*x)]++;
22     }
23     //計算CDF
24     for(int i=0; i
25         for(int j=0; j<=i; j++){
26             cdf[i]+=pdf[j];
27         }
28     }
29     cout<<"PDF:"<
30     for(int i=0; i
31         cout<
32     }
33     cout<<"CDF:"<
34     for(int i=0; i
35         cout<
36     }
37     system("pause");
38     return EXIT_SUCCESS;
39 }

輸出結果:

PDF:
0 0.099913
1 0.100337
2 0.099838
3 0.099732
4 0.100106
5 0.099986
6 0.099889
7 0.100057
8 0.100026
9 0.100087
CDF:
0 0.099913
1 0.20025
2 0.300088
3 0.39982
4 0.499926
5 0.599912
6 0.699801
7 0.799858
8 0.899884
9 0.999971
請按任意鍵繼續 . . .

圖示如:

Probability Density Function

機率密度函數(Probability Density Function

Cumulative Distribution Function

累積分佈函數(Cumulative Distribution Function)

阅读(1518) | 评论(0) | 转发(0) |
0

上一篇:zzWeka入门教程(3)

下一篇:其实你不是GP

给主人留下些什么吧!~~