確率分布

ベータ分布

http://itbc-world.com/home/rfm/r%E3%81%AE%E7%B5%B1%E8%A8%88%E9%96%A2%E6%95%B0/%E3%83%99%E3%83%BC%E3%82%BF%E5%88%86%E5%B8%83/

# ベータ分布
x <- seq( 0, 1, 0.1 )
curve( dbeta( x, 0.5, 0.5 ), col="blue", from=0, to=1 )
curve( dbeta( x, 2, 2 ), col="green", add=TRUE )
curve( dbeta( x, 5, 1 ), col="red", add=TRUE )
curve( dbeta( x, 1, 5 ), col="yellow", add=TRUE )

ガンマ分布

http://itbc-world.com/home/rfm/r%E3%81%AE%E7%B5%B1%E8%A8%88%E9%96%A2%E6%95%B0/%E3%82%AC%E3%83%B3%E3%83%9E%E5%88%86%E5%B8%83/

# ガンマ分布
x <- seq( 0, 10, 0.1 )
curve( dgamma( x, shape=0.5 ), col="blue", from=0, to=10 )
curve( dgamma( x, shape=0.5, scale=2 ), col="green", add=TRUE )
curve( dgamma( x, shape=1 ), col="yellow", add=TRUE )
curve( dgamma( x, shape=3 ), col="pink", add=TRUE )
curve( dgamma( x, shape=5 ), col="red", add=TRUE )

カイ二乗分布

http://itbc-world.com/home/rfm/r%E3%81%AE%E7%B5%B1%E8%A8%88%E9%96%A2%E6%95%B0/%E3%82%AC%E3%83%B3%E3%83%9E%E5%88%86%E5%B8%83/

x <- seq( 0, 10, 0.1 )
test_df <- 1
curve( dchisq( x, test_df ), col="red", lty="longdash", add=TRUE )
test_df <- 3
curve( dchisq( x, test_df ), col="blue", lty="longdash", add=TRUE )

指数分布

http://itbc-world.com/home/rfm/r%E3%81%AE%E7%B5%B1%E8%A8%88%E9%96%A2%E6%95%B0/%E3%82%AC%E3%83%B3%E3%83%9E%E5%88%86%E5%B8%83/

x <- seq( 0, 10, 0.1 )
curve( dexp( x ), col="red", lty="longdash", from=0,to=10)

二項分布(ベルヌーイ分布)

http://d.hatena.ne.jp/HiRos/20060514/1147596737

par(ann=F)                        #軸と全体のタイトルを描かない
#plot(1:50,dbinom(1:50, 5, p=0.5),type="l",ylim=c(0,0.3),col=1) 
#par(new=T)				#上書きの指定
plot(1:50,dbinom(1:50, 10, p=0.5),type="l",ylim=c(0,0.3),col=2)
par(new=T)				#上書きの指定
plot(1:50,dbinom(1:50, 20, p=0.5),type="l",ylim=c(0,0.3),col=3)
par(new=T)				#上書きの指定
plot(1:50,dbinom(1:50, 30, p=0.5),type="l",ylim=c(0,0.3),col=4)
par(new=T)				#上書きの指定
plot(1:50,dbinom(1:50, 40, p=0.5),type="l",ylim=c(0,0.3),col=5)
par(new=T)				#上書きの指定
plot(1:50,dbinom(1:50, 50, p=0.5),type="l",ylim=c(0,0.3),col=6)
grid()

ポアソン分布

http://itbc-world.com/home/rfm/r%E3%81%AE%E7%B5%B1%E8%A8%88%E9%96%A2%E6%95%B0/%E3%83%9D%E3%82%A2%E3%82%BD%E3%83%B3%E5%88%86%E5%B8%83/

# ポアソン分布
x <- seq( 0, 20, 1 )
plot( dpois( x, lambda=1 ), col="blue", type="b", ylim=c( 0, 0.5 ) )
par( new=TRUE )
plot( dpois( x, lambda=3 ), col="yellow", type="b", ylim=c( 0, 0.5 ) )
par( new=TRUE )
plot( dpois( x, lambda=5 ), col="red", type="b", ylim=c( 0, 0.5 ) )
par( new=TRUE )
plot( dpois( x, lambda=10 ), col="pink", type="b", ylim=c( 0, 0.5 ) )
par( new=TRUE )
plot( dpois( x, lambda=15 ), col="green", type="b", ylim=c( 0, 0.5 ) )

ガウス分布(正規分布)

http://qiita.com/HirofumiYashima/items/69c08eba285cc278a5b5

x<-0:60
curve(dnorm(x,30,5), 0, 60,col="green")
curve(dnorm(x,30,10), 0, 60,col="red",add=T)
curve(dnorm(x,15,10), 0, 60,col="blue",add=T)
curve(dnorm(x,30,5), 0, 60,col="green",add=T)

ディリクレ分布

http://www.singularpoint.org/blog/math/stat/dirichlet-distribution-3/
3次元

plot.dirichlet <- function(x){
  # x must be Nx3 matrix!!
  X <- x[,2]-x[,1]
  Y <- x[,3]
  par(xaxt="n")
  par(yaxt="n")
  par(bty="n")
  par(mar=rep(0,4))
  plot(X,Y,xlim=c(-1.2,1.2),ylim=c(-0.2,1.2),xlab="",ylab="",pch=20,cex=0.7)
  lines(c(-1,1,0,-1),c(0,0,1,0),col="gray")
  text(-1,0,"x1");  text(1,0,"x2");  text(0,1,"x3")
  alpha <- attr(x,"alpha")
  stralpha <- paste("a1=",alpha[1],"\na2=",alpha[2],"\na3=",alpha[3],sep="")
  text(-0.8,0.8,stralpha)
  print(alpha)
  symbols(-1,0,circles=0.08*sqrt(alpha[1]),inches=F,add=T)
  symbols(1,0,circles=0.08*sqrt(alpha[2]),inches=F,add=T)
  symbols(0,1,circles=0.08*sqrt(alpha[3]),inches=F,add=T)
}
 
rdirichlet <- function(N,alpha){
  d <- length(alpha)
  res <- matrix(0,N,d)
  for( i in 1:d ){
    res[,i] <- rgamma(N,alpha[i])
  }
  res <- res / rowSums(res)
  class(res) <- "dirichlet"
  attr(res,"alpha") <- alpha
  res
}
 
alphas <- list(c(1,1,1),c(3,2,1),c(6,2,1))
scale <- c(0.1,1,8)
 
par(mfrow=c(3,3))
for(alpha in alphas){
  for(s in scale){
    plot(rdirichlet(1000,s*alpha))
  }
}