# [R] Loop and Solver with Black/Scholes-Formula

From: bstudent <marc.ruetten_at_gmx.de>
Date: Sat, 23 Apr 2011 11:57:49 -0700 (PDT)

Hello,

for my diploma thesis I need to program a solver for Merton´s respectively Black´s and Scholes´ Option pricing formula, which should be achieved for several dates.

What I want to do is to estimate the value of a firm´s assets "vA" (x denotes vA) and the option-implied volatility of firm´s assets "sigA" (x denotes sigA) by solving it simultaneous using the Black and Scholes formulas. This solution should be computed for several dates, at which the data input changes with the next date (it should be computed row by row of the data input matrix).

What I did so far is to program a for-loop in which I wrote the solver:

```># Dataset:
>
Q       vE           sigE           D             R
1  1  39.81095  0.08957312  51.64004  0.00930000
2  2  39.76028  0.09127646  54.98504  0.01072000
3  3  37.00382  0.08177820  60.29025  0.01489545
4  4  35.17477  0.09221447  60.41061  0.02017879
```
5 5 36.54418 0.07852334 55.00648 0.02553438 6 6 37.20026 0.07949604 62.59768 0.02908462
```>
>
>
># Loop with Solver for 6 dates:
>
> citinbs <- for (i in 1:6) {
```

+
+ BS <- function(x) {
+
+ vE <- citin[i,2]
+ sigE <- citin[i,3]
+ D <- citin[i,4]
+ R <- citin[i,5]
+
+ T = 1
+
+ f <- rep(NA, length(x))
+
+ f <- (x * pnorm(log (x/D) + (R + ( (x^2) / 2) ) * T ) / ( x
* sqrt(T)) - exp(-R*T) * D * pnorm(log (x/D) + (R - ( (x^2) / 2) ) * T ) / ( x * sqrt(T))) - vE
+
+ f <- ((x * exp(-T) * pnorm(log (x/D) + (R + ( (x^2) / 2) ) * T
) / ( x * sqrt(T)) * x) / vE) - sigE
+
+ f
+ }
+
+ p0 <- c( vE + D, sigE * (vE / (vE + D)) )
+
+ ans <- dfsane(par=p0, fn=BS)
+
+ print(as.matrix(ans\$par))
+
+ }

The problem is, that the results I get aren´t really plausible. The next thing is, that I need the relevant Values of Output (ans\$par, which includes two values - x=vA and x=sigA - per date) as matrix. This Matrix should look like this:

vA sigA

```a     x
b     y
c     z
.     .
.     .
.     .

```

The entry I wrote ("print(as.matrix(ans\$par))") doesn´t achieve that.

I hope you understood what I´m trying to do. I´m an absolute beginner in programming in R, so these are some of my first steps. Please be patient ;) Also I hope my English isn´t to bad.

Thank you very much for helping me out!!!

Kind regards,

bstudent.

```--
View this message in context: http://r.789695.n4.nabble.com/Loop-and-Solver-with-Black-Scholes-Formula-tp3470488p3470488.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help