数学建模社区-数学中国

标题: 求高手指点写 ell,optim code [打印本页]

作者: HongYu_柳    时间: 2012-12-10 04:20
标题: 求高手指点写 ell,optim code
mu<-0$ g- e) u* V' K9 ]
sigma2<-1
8 S( G9 X9 Z& v9 ?. Z4 G: m  n<-20
- r' I' i0 d) W  L<--0.25" ?3 a; C+ ?% Q- A  n+ y) ^) U& p
  y<-rnorm(n,mu,sqrt(sigma2))+ d' H8 \1 T* U
  x<-y
! T. m" ^. [# N; J( @  w  c<-(y<=L)
# R2 g% [; _. P x[c]<-L1 l! ]: u0 ^" d
x) x9 J2 q! I" A, L6 [- s9 I
Q1:写一个ell function compute log-likelihood od x.+ |2 G  W' `" M1 m" f4 }
Q2用 R optim来maximize这个likelihood.
* |7 |, ]: L+ N( {  j* C$ O9 s
& Z, l0 s6 W+ e7 B5 O" p' H1 o我现在写了一个ell的试子了,但是怎么也求不出最大值。
( n/ G  U0 S( @' e* L4 Z. ?0 htheta<-c(mu,sigma2)
3 D2 y1 P$ n7 I" c ell<-function(theta,x,L=-0.25){1 F+ I: b5 e* c8 Y% T1 `
stopifnot(is.vector(theta),length(theta)==2)
( w9 q1 n7 `9 s0 x  p if(theta[2]<=0)
+ }" }& S. Q- [/ t1 z4 ^6 a! F# d1 x return(NA)
+ [, i! j1 B2 a( E5 `! G else(c<-(x==L))& R+ `) s  T% F0 ]# B
sum(dnorm(x[c],theta[1],sqrt(theta[2]),log=TRUE))
% W5 D, b" I- r1 n- j6 _ sum2(dnorm(x[!c],theta[1],sqrt(theta[2]),lop=true))}& @& C7 h6 n( S
8 F- K$ D6 V' Q+ d- D
我的求最大值试子是init<-c(mu=0,sigma2=1): ?3 Q* G9 ]' H# m1 v) |6 n/ E/ {
1 u/ ^, ]9 w* y/ K9 f# `( U
opt<-optim(par=init,fn=ell,method="L-BFGS-B",
. |9 x: N4 G+ a5 f) l& j  lower=c(-Inf,0.01),
" c3 Z  D& q4 u2 ~, v& ]  upper=c(Inf,Inf),hessian=TRUE,, M& e9 R$ `7 y' j
  control=list(fnscale=-1)); y, G6 F+ i. o9 ^& t9 x' f
谁能看看哪里错了。谢谢了。
作者: zxtttxxqm    时间: 2012-12-11 14:05
这个的话你去问版主吧




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5