Showing posts with label log-likelihood function. Show all posts
Showing posts with label log-likelihood function. Show all posts

## we create the log-likelihood function

>  LL = function(params , rets ) {alpha = params [ 1 ] ; sigsq = params [ 2 ]
+     logf = −log(sqrt (2*pi*sigsq))
+     − ( rets−alpha )^2/(2*sigsq )
+      LL = −sum( logf ) }
We then go ahead and do the MLE using the nlm (non-linear minimization) package in R. It uses a Newton-type algorithm.
# create starting guess for par ameters
params = c(0.001, 0.001 )
> res = nlm( LL,params, rets )
> params = c(0.001, 0.001 )
> res = nlm( LL,params, rets )
There were 50 or more warnings (use warnings() to see the first 50)
> res
\$minimum
[1] -6.591566

\$estimate
[1] 1.000000e-03 2.995424e-07

[1]      0.0 733755.7

\$code
[1] 2

\$iterations
[1] 7
We now pick off the results and manipulate them to get the annualized parameters {µ, σ}.

>  alpha = res\$estimate [ 1 ]
>  sigsq = res\$estimate [ 2 ]
>  sigma = sqrt( sigsq/h )
> sigma
[1] 0.008688192
> mu = alpha/h+0.5*sigma^2
> mu
[1] 0.2520377

### Black-Scholes formula-R

Black-Scholes formula-R > BlackScholes <- function(TypeFlag = c("c", "p"), S, X, Time, r, b, sigma) { TypeFla...