-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.d
60 lines (52 loc) · 1.34 KB
/
stats.d
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
import std.math, std.stdio, std.algorithm, std.range, std.array;
version ( none){
pure ulong nCr(ulong n, ulong r){
/+ factorial 0 = 1 +/
c= c > n - c ? c: n - c;
int result = 1;
//return reduce!("a * b")( cast(ulong)1, iota(c, n + 1));
foreach( ulong i ; c+1..n+1 ){
result *= i;
result /= i-c;
}
return result;
}
}
ulong nCr( ulong n, ulong r){
if ( n < r){
/+screw you!+/
return 0;
}
ulong[][] pascal_rectangle = minimallyInitializedArray!(ulong[][])(n+1-r, r+1);
foreach( ref ulong it ; pascal_rectangle[0]){
it = 1;
}
foreach( ref ulong it[] ; pascal_rectangle ){
it[0] = 1;
}
for( ulong i = 1 ; i < pascal_rectangle.length ; i++){
for ( ulong j = 1 ; j < pascal_rectangle[i].length ; j++){
pascal_rectangle[i][j] = pascal_rectangle[i-1][j] + pascal_rectangle[i][j-1] ;
}
}
return pascal_rectangle[n-r][r];
}
unittest{
foreach ( ulong i ; 1..7){
writeln(nCr(7,i));
}
assert( 20 == nCr(6, 3));
}
double binomial(ulong n, ulong k, double p){
return nCr(n,k)*pow(p,k)*pow(1-p, n-k);
}
double cumulative_binomial(ulong n, ulong k, double p){
return reduce!( "a + b")(0.0, map!( a => binomial(n, a, p) )( iota( 0, k+1) ));
}
unittest{
double a = 0;
for( int i = 0 ; i < 21 ; ++i){
writeln(i , ": ", a += binomial(20,i,.5));
writeln(i , ": ", cumulative_binomial(20,i,.5));
}
}