HongYu_柳 发表于 2012-12-10 04:20

求高手指点写 ell,optim code

mu<-0
sigma2<-1
  n<-20
  L<--0.25
  y<-rnorm(n,mu,sqrt(sigma2))
  x<-y
  c<-(y<=L)
x<-L
x
Q1:写一个ell function compute log-likelihood od x.
Q2用 R optim来maximize这个likelihood.

我现在写了一个ell的试子了,但是怎么也求不出最大值。
theta<-c(mu,sigma2)
ell<-function(theta,x,L=-0.25){
stopifnot(is.vector(theta),length(theta)==2)
if(theta<=0)
return(NA)
else(c<-(x==L))
sum(dnorm(x,theta,sqrt(theta),log=TRUE))
sum2(dnorm(x[!c],theta,sqrt(theta),lop=true))}

我的求最大值试子是init<-c(mu=0,sigma2=1)

opt<-optim(par=init,fn=ell,method="L-BFGS-B",
  lower=c(-Inf,0.01),
  upper=c(Inf,Inf),hessian=TRUE,
  control=list(fnscale=-1))
谁能看看哪里错了。谢谢了。

zxtttxxqm 发表于 2012-12-11 14:05

这个的话你去问版主吧
页: [1]
查看完整版本: 求高手指点写 ell,optim code