Skip to content

Instantly share code, notes, and snippets.

View doryokujin's full-sized avatar

doryokujin doryokujin

View GitHub Profile
@doryokujin
doryokujin / comp.r
Created May 8, 2012 16:37
coupon collector's problem
as.prob <- function(weight){
return(weight/sum(weight))
}
expt.comp <- function(input){
n <- length(input)
g <- function(i,n,input){
(-1)**(n-1-i)*1/(1-apply(combn(input,i),2,sum))
}
tmp <- sapply(seq(0,n-1),function(i){
sapply(seq(1,50),function(n){ n*harmonic(n) })
[1] [2] [3] [4] [5]
[1] 1.000000 3.000000 5.500000 8.333333 11.416667
[2] 14.700000 18.150000 21.742857 25.460714 29.289683
[3] 33.218651 37.238528 41.341739 45.521873 49.773435
[4] 54.091664 58.472393 62.911945 67.407053 71.954793
[5] 76.552533 81.197892 85.888705 90.622996 95.398954
[6] 100.214913 105.069332 109.960789 114.887960 119.849614
[7] 124.844601 129.871846 134.930341 140.019140 145.137350
harmonic <- function(n){
sum(sapply(seq(1,n),function(n){ return(1/n) }))
}
res <- 44*harmonic(44) # res = 192.399
@doryokujin
doryokujin / test.r
Created May 20, 2012 13:44
prop.test
# hand calculation での注意:二項分布の同等性検定は2×2の分割表によるχ^2検定に等しいのでそちらの計算方法を採用。
# Q1.はサンプル数が少ないのでイェツの補正を行っている。
######################################
# Q1.
# Head Tail S
# A 4 6 10
# B 6 4 10
# S 10 10 20
#
> calc.p <- function(n) { rbinom(1,n,1/2)/n } # n回試行での表の出る確率
> x <- seq(1,1000)
> y <- sapply(x, calc.p)
> x
[1] 1 2 3 4 5 6 7 8 9 10
...
[995] 995 996 997 998 999 1000
> y
[1] 1.0000000 0.5000000 1.0000000 0.7500000 0.6000000
...
# 10回のコイン投げを100セット繰り返したときの,表の出た回数の分布。
> n <- 10; x <- seq(0,10); set <- 100;
> hist(rbinom(set,n,0.5), breaks=x, probability=TRUE, xlab='set=100,n=10')
> lines(x, dnorm(x, n/2, sqrt(n*0.5*0.5)), col="red")
# 20回のコイン投げを100セット繰り返したときの,表の出た回数の分布。
> n <- 20; x <- seq(0,20); set <- 100;
> hist(rbinom(set,n,0.5), breaks=x, probability=TRUE, xlab='set=100,n=10')
> lines(x, dnorm(x, n/2, sqrt(n*0.5*0.5)), col="red")
# Normal Distiribution
# Sequence between -4 and 4 with 0.1 steps
x <- seq(-4, 4, 0.1)
# Plot an empty chart with tight axis boundaries, and axis lines on bottom and left
plot(x, type="n", xaxs="i", yaxs="i", xlim=c(-4, 4), ylim=c(0, 0.4),
bty="l", xaxt="n", xlab="z-value", ylab="probability density")
# Function to plot each coloured portion of the curve, between "a" and "b" as a
# polygon; the function "dnorm" is the normal probability density function
> samples <- rnorm(100)
> table(round(samples))
-3 -2 -1 0 1 2 3
1 4 26 36 25 7 1
## harmonic number ##
harmonic <- function(n){
sum(sapply(seq(1,n),function(n){ return(1/n) }))
}
## expectation of partly complete case ##
expt.partly_comp1 <- function(m,n){
if (n < m) {
m * ( harmonic(m) - harmonic(m-n) )
}else{
m * harmonic(m)
as.prob <- function(weight){
return(weight/sum(weight))
}
expt.partly_comp2 <- function(vec.prob, n){
m <- length(vec.prob)
g <- function(m, n, i, vec.prob){
(-1)**(n-1-i)*choose(m-i-1, m-n)*1/(1-apply(combn(vec.prob, i), 2, sum))
}
tmp <- sapply(seq(0,n-1),function(i){
g( m, n, i, vec.prob)