2016年9月24日土曜日

三つの分布:Lognormal Distribution(対数正規分布)

Normal Distribution(正規分布)」からの続き。

次の図は、図 B.4 の点線(理論上の分布)を除いたもの。
> u <- rlnorm(1001)
> dframe <- data.frame(u=u)
> p01 <- ggplot(dframe, aes(x=u)) + geom_density()
> p02 <- ggplot(dframe, aes(x=log(u))) + geom_density()
> nplot(list(p01,p02))

nplot の定義は末尾の「R コード」参照。

上段は lognormal distribution「対数正規分布」の 1,001 個の乱数(meanlog = 0, sdlog = 1)の分布、下段はその乱数の対数値 log(u) の分布。下段は mean = 0, sd = 1 の正規分布のよう に見える。

そんな理論上の分布を重ねたのが次の図 B.4
# generate the lognormal with meanlog=0, sdlog=1
# 対数正規分布の理論値
> x  <- seq(from=0,to=25,length.out=500)
> f  <- dlnorm(x)
> lnormframe <- data.frame(x=x,y=f)

# generate the normal with mean=0, sd=1
# 正規分布の理論値
> x2 <- seq(from=-5,to=5,length.out=500)
> f2 <- dnorm(x2)
> normframe  <- data.frame(x=x2,y=f2)

# plot densityplots with theoretical curves superimposed
> p1 <- ggplot(dframe, aes(x=u)) + geom_density() +
    geom_line(data=lnormframe,aes(x=x,y=y),linetype=2)
> p2 <- ggplot(dframe, aes(x=log(u))) + geom_density() +
    geom_line(data=normframe,aes(x=x,y=y),linetype=2)
> nplot(list(p1,p2))

つまり
The lognormal distribution is the distribution of a random variable x whose natural log log(x) is normally distributed. 
対数正規分布の乱数 x の対数 log(x) は正規分布する。

B.4 上段のように、極端に偏っている「正の数」は対数正規分布でモデル化できる。例えば、年収、売上、株価、など。

対数正規分布の母数において、平均値は中央値より大きくなる。

> u <- rlnorm(1001)
> mean(u)
[1] 1.564995
> mean(u); median(u)
[1] 1.564995
[1] 1.006462

そして、対数の平均値と標準偏差は、以下の例では 0 と 1 に近似する。

> mean(log(u))
[1] -0.0159064
> sd(log(u))
[1] 0.9712445

つまり、1 になるのは「平均値」ではなく「対数の平均値」。

# the 50th percentille (or median) of the lognormal with meanlog=0 and sdlog=10
> qlnorm(0.5)
[1] 1

# the probability of seeing a value x less than 1
x が 1 以下である確率
> plnorm(1)
[1] 0.5

# the probability of observing a value x less than 10
が 10 以下である確率
> plnorm(10)
[1] 0.9893489


Intuitively, if variations in the data are expressed naturally as percentages or relative differences, rather than as absolute differences, then the data is a candidate to be modeled lognormally. 
データが「絶対値の差」よりも「割合や相対的な差」で表す方が自然な場合、対数正規分布によるモデル化は候補になる。

75% of the Area Under the Curve

meanlog=1, sdlog=0 の対数正規分布の「下」75% を塗りつぶしたもの(図 B.5)。
# use lnormframe from previous example: the theoretical lognormal curve
> line <- qlnorm(0.75)
> xstr <- sprintf("qlnorm(0,75) = %1.3f", line)
> lnormframe75 <- subset(lnormframe, lnormframe$x < line)
# The shaded area is 75% of the area under the lognormal curve
> ggplot(lnormframe, aes(x=x,y=y)) + geom_line() +
 geom_area(data=lnormframe75, aes(x=x,y=y), fill="gray") +
 geom_vline(aes(xintercept=line), linetype=2) +
 geom_text(data=data.frame(),
   aes(x=line,y=0,label=xstr),hjust=0,vjust=1)


Binomial Distribution(二項分布)」につづく。


R コード

> library(grid)
> nplot <- function(plist) {
 n <- length(plist)
 grid.newpage()
 pushViewport(viewport(layout=grid.layout(n,1)))
 vplayout <- function(x,y) {
 viewport(layout.pos.row=x, layout.pos.col=y)}
   for(i in 1:n) {
     print(plist[[i]],vp=vplayout(i,1))
   }
 }

0 件のコメント:

コメントを投稿