Sunday, March 22, 2020

Black-Scholes formula-R

 Black-Scholes formula-R

> BlackScholes <- function(TypeFlag = c("c", "p"), S, X, Time, r, b, sigma) { TypeFlag = TypeFlag[1] d1 = (log(S/X) + (b + sigma * sigma/2) * Time)/(sigma * sqrt(Time)) d2 = d1 - sigma * sqrt(Time) if (TypeFlag == "c") price = S * exp((b - r) * Time) * pnorm(d1) - X * exp(-r * Time) * pnorm(d2) else if (TypeFlag == "p") price = X * exp(-r * Time) * pnorm(-d2) - S * exp((b - r) * Time) * pnorm(-d1) param <- list(TypeFlag = TypeFlag, S = S, X = X, Time = Time, r = r, b = b, sigma = sigma) ans <- list(parameters = param, price = price, option = "Black Scholes") class(ans) <- c("option", "list") ans }


> library("ggplot2", lib.loc="~/R/win-library/3.6")
> x <- seq(-4, 4, l = 50)
> y <- x
> myf <- function(x, y) {
+     sin(x) + cos(y)
+ }
> z <- outer(x, y, FUN = myf)
> persp(x, y, z, theta = 45, phi = 45, shade = 0.45)
> Cap = floor(Capitalization/1000)
> Cap
                2003  2004 2005  2006  2007 2008
Euronext US     1328 12707 3632 15421 15650 9208
TSX Group        888  1177 1482  1700  2186 1033
Australian SE    585   776  804  1095  1298  683
Bombay SE        278   386  553   818  1819  647
Hong Kong SE     714   861 1054  1714  2654 1328
NSE India        252   363  515   774  1660  600
Shanghai SE      360   314  286   917  3694 1425
Tokyo SE        2953  3557 4572  4614  4330 3115
BME Spanish SE   726   940  959  1322  1781  948
Deutsche Boerse 1079  1194 1221  1637  2105 1110
London SE       2460  2865 3058  3794  3851 1868
Euronext EU     2076  2441 2706  3712  4222 2101
SIX SE           727   826  935  1212  1271  857
> barplot(t(Cap)/1e+06, beside = TRUE, las = 2, ylab = "Capitalization [Mio USD]")
> title(main = "Major Stock Markets")
>
 function


> mtext(side = 3, "2003 - 2008")
> barplot(Cap/1e+06, beside = TRUE, ylab = "Capitalization [Mio USD]")
> palette(rainbow(13, s = 0.6, v = 0.75))
> stars(t(log(Cap)), draw.segments = TRUE, ncol = 3, nrow = 2,
+       key.loc = c(4.6, -0.5), mar = c(15, 0, 0, 0))
> mtext(side = 3, line = 2.2, text = "Growth and Decline of Major Stock Markets",
+       cex = 1.5, font = 2)
> abline(h = 0.9)

No comments:

Post a Comment

Black-Scholes formula-R

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