||
本文的目的,是直观表达极大似然估计。给出一组数,若用正态分布拟合,则其平均值和标准差最可能的取值是什么?
极大似然面的顶点,就是最优估计。实际情况下,是对对数似然函数Log Likelihood取极值。这里只按照似然(Likelihood)的原始定义,直接求Likelihood 。
likelihood即为各种概率密度的乘积
注意R code 中的 prod(dnorm(dat, mean_val[i], sd_val[j]))
以下是绘制拟合正态分布的似然面。
Draw Likelihood Surface using R
### 绘制似然面: Likelihood Surface
## Author:jinlongzhang01@gmail.com
LikSurf <- function(dat, precision = 0.05){
mean_val <- seq(min(dat), sqrt(max(dat)), by = precision)
sd_val <- seq(0, exp(sd(dat)), by = precision)
mat <- rep(NA, length(mean_val)*length(sd_val))
dim(mat) <- c(length(mean_val), length(sd_val))
for(i in 1:length(mean_val)){
for(j in 1:length(sd_val)){
mat[i, j] <- prod(dnorm(dat, mean_val[i], sd_val[j]))
}
}
#contour(mat)
res <- list(xaxis = axisTicks(range(mean_val), log = FALSE),
yaxis = axisTicks(range(sd_val), log = FALSE),
mean_val = mean_val, sd_val = sd_val,
mat = mat, pre = precision)
class(res) <- "LikSurf"
return(invisible(res))
}
print.LikSurf <- function(x){
cat("This is a likelihood surface with", "nx_range: ",
range(x$xaxis), "ny_range:", range(x$yaxis),
"nprecision", x$pre, "n")
}
contour.LikSurf <- function(x, ...){
contour(x = x$mean_val, y = x$sd_val, z = x$mat, axes = FALSE, xlab = "Mean", ylab = "sd", ...)
axis(1, x$xaxis); axis(2, x$yaxis); box()
}
image.LikSurf <- function(x, ...){
image(x = x$mean_val, y = x$sd_val, z = x$mat, axes = FALSE, xlab = "Mean", ylab = "sd", ...)
axis(1, x$xaxis); axis(2, x$yaxis); box()
}
### 以上函数拷贝到R中即可
### 应用举例
dat = c(-0.77, 6.50, 2.17, -0.51, 1.1, 2.52, 0.17, -0.01, 1.02, 0.11, -0.31, -0.12, 0.25, 0.84)
#dat = rnorm(10)
lks <- LikSurf(dat, precision = 0.05)
image(lks, col = cm.colors(20))
contour(lks, drawlabels = TRUE, add = TRUE)
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-12-22 19:33
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社