11. [R] calculation of MLE for Normal (Gaussian) data (Jan24,26)

# 11. [R] calculation of MLE for Normal (Gaussian) data (Jan24,26)

This preview shows pages 1–2. Sign up to view the full content.

# January 7, 2011 # calculation of MLE for Normal (Gaussian) data # formula for negative likelihood # maximize log likelihood == minimize -1*loglikelihood # a = (mu, sig2) ; x = data of length n NLik = function(a,x){ mu = a[1] sig2 = a[2] n = length(x) L = n*log(sig2)/2 + sum((x - mu)^2)/(2*sig2) # .grad is a 1 by 2 matrix that is the derivatives of L .grad = array(0, c(1,2), list(NULL, c("mu", "sig2"))) .grad[,"mu"] = -sum(x-mu)/sig2 .grad[,"sig2"] = n/(2*sig2) - sum((x - mu)^2)/(2*sig2^2) attr(L, "gradient") = .grad L } # more efficient code for NLik NLik.1 = function(a,x){ mu = a[1] sig2 = a[2] n = length(x) b = sum((x - mu)^2) xbar = mean(x) L = n*log(sig2)/2 + b/(2*sig2) # .grad is a 1 by 2 matrix that is the derivatives of L .grad = array(0, c(1,2), list(NULL, c("mu", "sig2")))

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: .grad[,&quot;mu&quot;] = -(xbar-mu)/sig2 .grad[,&quot;sig2&quot;] = n/(2*sig2) - b/(2*sig2^2) attr(L, &quot;gradient&quot;) = .grad L } # simulate some data mu.true = 2 sig2.true = 4 n = 20 x.dat = rnorm(n, mean = mu.true, sd = sqrt(sig2.true) ) a.0 = c(0,1) x.fit = nlm(NLik, p=a.0 ,x=x.dat) x.fit #calculate MLE using analytic formula mu.hat = mean(x.dat) sig2.hat = sum( (x.dat - mu.hat)^2)/length(x.dat) mu.hat Page 1 of 2 25/01/2011 http://www.stats.uwo.ca/faculty/kulperger/Stat3858/Computing/RScripts/Normal-est-nlm. .. sig2.hat # also # n = length(x.dat) # sig2.hat = var(x.dat)*(n-1)/n Page 2 of 2 25/01/2011 http://www.stats.uwo.ca/faculty/kulperger/Stat3858/Computing/RScripts/Normal-est-nlm. .....
View Full Document

## This note was uploaded on 01/17/2012 for the course AM 1234 taught by Professor Qqqq during the Spring '11 term at UWO.

### Page1 / 2

11. [R] calculation of MLE for Normal (Gaussian) data (Jan24,26)

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online