대학원 일기

연속 확률 분포 본문

School/Data Mining and Statistics

연속 확률 분포

대학원생(노예) 2023. 10. 16. 01:29

균등 분포

  • 유한한 실수 구간 [a, b]에서 동일한 확률로 관측되는 확률변수의 분포
library(ggplot2)

# uniform distribution plot (min=0, max=10), 균등분포 그래프
# 균등분포 : fun = dunif
ggplot(data.frame(x=c(-2,20)), aes(x=x)) +
  stat_function(fun=dunif, args=list(min = 0, max = 10), colour="black", size=1) +
  ggtitle("Uniform Distribution of (min=1, max=10)")

# (2) 누적균등분포 함수 그래프 (Cumulative Uniform distribution plot) : fun = punif
ggplot(data.frame(x=c(-2,20)), aes(x=x)) +
  stat_function(fun=punif, args=list(min = 0, max = 10), colour="black", size=1) +
  ggtitle("Cumulative Uniform Distribution of (min=0, max=10)")

# (3) 누적 균등분포함수(cumulative uniform distribution function) 확률 값 계산 : punif()
# : punif(q, min, max, lower.tail = TRUE/FALSE)
punif(3, min=0, max=10, lower.tail=TRUE)

# Uniform Distribution of (min=1, max=10), x from 0 to 3"
ggplot(data.frame(x=c(-2,20)), aes(x=x)) +
  stat_function(fun=dunif, args=list(min = 0, max = 10), colour="black", size=1) +
  annotate("rect", xmin=0, xmax=3, ymin=0, ymax=0.1, alpha=0.2, fill="yellow") +
  ggtitle("Uniform Distribution of (min=1, max=10), x from 0 to 3")

# (4) 분위수 함수 : qunif(p, min, max, lower.tail=TRUE/FALSE)
qunif(0.3, min=0, max=10, lower.tail = TRUE)

# 난수 발생
ru_100 <- runif(n=100, min=0, max = 10)
ru_100

# density plot of runif(n=100, min=0, max = 10) & adding line of 0.1 uniform probability
hist(ru_100, freq=FALSE, breaks=10, col="yellow")
abline(h=0.1, lty=3, lwd=3, col="red")

 

지수 분포

  • 확률밀도함수가 지수적으로 감소하는 확률 분포
# 지수 분포
library(ggplot2)
df <- data.frame(x=seq(0, 10, 0.1), pdf=dexp(x=seq(0, 10, 0.1), rate = 3))
ggplot(data=df, aes(x=x, y=pdf))+
  geom_line()+
  ylab("Probability Density")

# 지수 누적 확률 밀도 분포
# λ가 3일 때, P(1<X<2) 구하기
pexp(q=2, rate=3)-pexp(q=1, rate=3)

# 지수누적확률밀도분포 그리기
df <- data.frame(x=seq(0, 10, 0.1), cdf=pexp(q=seq(0, 10, 0.1), rate = 3))
ggplot(data=df, aes(x=x, y=cdf)) +
  geom_line()+
  ylab("cumulative density")

#지수확률분포의 95% 분위수 구하기
qexp(p=0.95, rate=3)

# 지수분포의 랜덤난수 구하기: 같은 코드 다른 난수(!주의)
rexp(3, rate=3) # [1] 0.9985774

rexp(3, rate=3) # [1] 0.9985774

# set.seed()을 이용한 초기값고정: 같은 코드 같은 난수
set.seed(100)
rexp(3, rate=3) # [1] 0.30807054 0.24127906 0.03488162

set.seed(100)
rexp(3, rate=3) # [1] 0.30807054 0.24127906 0.03488162

 

감마 분포

# 감마 분포
# ggplot 감마확률밀도함수 그리기
library(ggplot2)
df<- data.frame(x=seq(-2, 8, 0.01), pdf=dgamma(x=seq(-2, 8, 0.01), shape=1, scale=1.5))
ggplot(data=df, aes(x=x, y=pdf)) +
  geom_line() +
  ggtitle("shape = 1, scale = 1.5 gamma pdf")+
  ylab("Probability Density") #y축 이름설정

# Gam(1,1.5) P(-2<X<2) 구하기
pgamma(q=2, shape=1, scale=1.5) - pgamma(q=-2, shape=1, scale=1.5) # [1] 0.7364029

#감마누적확률분포 그리기
df <- data.frame(q=seq(-2, 10, 0.1), cdf=pgamma(q=seq(-2, 10, 0.1), shape=1, scale=1.5))
ggplot(data=df, aes(x=q, y=cdf)) +
    geom_line()+
    ggtitle("shape = 1, scale = 1.5 gamma cdf")+
    ylab("cumulative density")

# 감마확률분포의 분위수구하기
qgamma(p=0.5, shape = 1, scale =1.5) # [1] 1.039721

# 감마분포의 랜덤난수 구하기: 같은 코드 다른 난수(!주의)
rgamma(3, shape=1, scale=1.5) # [1] 0.4266229 0.1508929 0.1800380
rgamma(3, shape=1, scale=1.5) # [1] 0.06197817 1.89526666 0.77267735

#set.seed()을 이용한 초기값고정: 같은 코드 같은 난수
set.seed(100)
rgamma(3, shape=1, scale=1.5) # [1] 0.3119185 1.3561534 0.8791976
set.seed(100)
rgamma(3, shape=1, scale=1.5) # [1] 0.3119185 1.3561534 0.8791976

 

와이블 분포

# 와이블 분포 그리기
library(ggplot2)
df<- data.frame(x=seq(-5, 5, 0.1), pdf=dweibull(x=seq(-5, 5, 0.1), shape = 2, scale = 1)) 
ggplot(data=df, aes(x=x, y=pdf)) +
       geom_line(aes(x=x, y=pdf)) +
       ylab("Probability Density") # y축 이름설정

# 와이블 분포확률 구하기 및 누적확률밀도분포 그리기
pweibull(q=2, shape = 2, scale = 1) - pweibull(q=1, shape = 2, scale = 1) # [1] 0.3495638

#와이블 누적확률밀도분포 그리기
df<- data.frame(q=seq(-5, 5, 0.1), cdf=pweibull(q=seq(-5, 5, 0.1), shape = 2, scale = 1, lower.tail = TRUE)) 
ggplot(data=df, aes(x=q, y=cdf)) +
    geom_line()+
    ylab("cumulative density")

# 와이블 확률분포의 분위수(quartile) 구하기
#와이블 분포의 분위수구하기 0.5
qweibull(p=0.5, shape = 2, scale = 1, lower.tail = TRUE) # [1] 0.8325546

# 와이블분포의 랜덤난수 구하기: 같은 코드 다른 난수(!주의)
rweibull(3, shape = 2, scale = 1) # [1] 1.2347040 0.4527033 0.3246876
rweibull(3, shape = 2, scale = 1) # [1] 1.2027696 0.8395303 0.3058707

#set.seed()을 이용한 초기값고정: 같은 코드 같은 난수
set.seed(100)
rweibull(3, shape = 2, scale = 1) # [1] 1.0855483 1.1645024 0.7704695
set.seed(100)
rweibull(3, shape = 2, scale = 1) # [1] 1.0855483 1.1645024 0.7704695

 

베타 분포

# 베타 분포
x <- seq(0, 1, length.out = 51)
a <- 0.5
b <- 0.5
y <- (1/beta(a, b))*x^(a-1)*(1-x)^(b-1)
plot(x, y)

lines(x, dbeta(x, a, b), type = 'l', col = 'red')

par(mfrow = c(4,4))
plot(x, dbeta(x, a, b), col = 'red', type = 'l', ylim = c(0, 3), 
     main = paste('a = ', a, ', b = ', b))
beta_plot <- function(a, b){
  plot(x, dbeta(x, a, b), col = 'red', type = 'l', ylim = c(0, 3),
       main = paste('a = ', a, ', b = ', b))
}
beta_func <- function(){
  plot.new()
  par(mfrow = c(4, 4))
  for(i in 0:3){
    for(j in 0:3){
      beta_plot(0.5*2^i, 0.5*2^j)
    }
  }
}
beta_func()

'School > Data Mining and Statistics' 카테고리의 다른 글

9장: OPEN API  (0) 2023.10.16
8장: 웹 스크래핑 코드  (0) 2023.10.16
7장: 지도 활용 코드  (0) 2023.10.16
이산형 확률 분포  (0) 2023.10.16
데이터마이닝과 통계  (0) 2023.04.19
Comments