[R] Calibrating the risk free interest rate using nlminb
Berend Hasselman
bhh at xs4all.nl
Tue Aug 16 18:56:36 CEST 2011
Newbie wrote:
>
> I used:
> marketdata <- read.csv(file="S&P 500 calls, jan-jun 2010.csv",
> header=TRUE, sep=";")
> after changing my directory to where the file is saved.
> The data imported should be correct.
> The spot is equal to S0, I typed it double in the post, sorry for that.
> So S0 = 1136.03 is the spot
>
1. it seems that your function difference contains an error: Superfluous
closing )
2. difference contains another possible error: it is not using the last
argument mid
It's using the global variable mid_bidask.
It should read
difference <- function (S0, K, T, r, sigma, q, mid)
{
return(BS_Call(S0, strike, T, r, sigma, q)- mid)
}
Your function returns a vector of length 460 as you correctly said.
But in the documentation of nlminb it clearly states that the objective
must return a scalar value.
(NB. It doesn't seem to check?)
So in function f you should at least reduce the vector to a scalar.
In this case I would minimize the sum of the differences squared to prevent
the optimization routine
from getting negative values for differences.
So define f as follows
f <- function(x) sum(difference(S0, strike, T, x, implvol, q, mid_bidask)^2)
and then
nlminb(start=r, f)
gives
> nlminb(start=r, f)
$par
[1] 0.01268534
$objective
[1] 3140.738
$convergence
[1] 0
$iterations
[1] 5
$evaluations
function gradient
8 5
$message
[1] "relative convergence (4)"
If you try nlm and optim (with method="BFGS) you'll get similar results.
It's up to you to decide if the outcome is plausible/acceptable.
Berend
--
View this message in context: http://r.789695.n4.nabble.com/Calibrating-the-risk-free-interest-rate-using-nlminb-tp3747509p3747758.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list