# R-programming-statistics

R is a programming language possesses an extensive catalog of statistical and graphical methods. It includes machine learning algorithm, linear regression, time series, statistical inference to name a few. Most of the R libraries are written in R,but many large companies also use R programming language, including Uber, Google, Airbnb, Facebook and so on

## Email subscriptions

Delivered by FeedBurner

Showing posts with label fExoticOptions. Show all posts
Showing posts with label fExoticOptions. Show all posts

## How to get fExoticOptions price

bench <- function(N = 1e5, K = 10) {
+     x <- rnorm(N)
+     gc()
+     t <- c()
+     t <- system.time(for (k in 1:K) median(x))
+     t <- system.time(for (k in 1:K) weightedMedian(x))
+     t <- t / t
+     names(t) <- c("median", "weightedMedian")
+     t
+ }
> library(fOptions)
Warning messages:
1: package ‘fOptions’ was built under R version 3.5.2
2: package ‘timeDate’ was built under R version 3.5.1
3: package ‘timeSeries’ was built under R version 3.5.1
4: package ‘fBasics’ was built under R version 3.5.2
> library(fExoticOptions)
Warning message:
package ‘fExoticOptions’ was built under R version 3.5.1
> library(fExoticOptions)
> FEInDomesticFXOption(TypeFlag = "c", S = 100, E = 1.5,
+                      X = 160, Time = 0.5, r = 0.08, q = 0.05, sigmaS = 0.20,
+                      sigmaE = 0.12, rho = 0.45)

Title:
FE In Domestic FX Option

Call:
FEInDomesticFXOption(TypeFlag = "c", S = 100, E = 1.5, X = 160,
Time = 0.5, r = 0.08, q = 0.05, sigmaS = 0.2, sigmaE = 0.12,
rho = 0.45)

Parameters:
Value:
TypeFlag c
S        100
E        1.5
X        160
Time     0.5
r        0.08
q        0.05
sigmaS   0.2
sigmaE   0.12
rho      0.45

Option Price:
8.305561

Description:
Wed Mar 20 16:24:40 2019

> QuantoOption(TypeFlag = "c", S = 100, Ep = 1.5, X = 105,
+              Time = 0.5, r = 0.08, rf = 0.05, q = 0.04, sigmaS= 0.2,
+              sigmaE = 0.10, rho = 0.30)

Title:
Quanto Option

Call:
QuantoOption(TypeFlag = "c", S = 100, Ep = 1.5, X = 105, Time = 0.5,
r = 0.08, rf = 0.05, q = 0.04, sigmaS = 0.2, sigmaE = 0.1,
rho = 0.3)

Parameters:
Value:
TypeFlag c
S        100
Ep       1.5
X        105
Time     0.5
r        0.08
rf       0.05
q        0.04
sigmaS   0.2
sigmaE   0.1
rho      0.3

Option Price:
5.328035

Description:
Wed Mar 20 16:25:02 2019

> EquityLinkedFXOption(TypeFlag = "p", E = 1.5, S = 100,
+                      X = 1.52, Time = 0.25, r = 0.08, rf = 0.05, q = 0.04,
+                      sigmaS = 0.20, sigmaE = 0.12, rho = -0.40)

Title:

Call:
EquityLinkedFXOption(TypeFlag = "p", E = 1.5, S = 100, X = 1.52,
Time = 0.25, r = 0.08, rf = 0.05, q = 0.04, sigmaS = 0.2,
sigmaE = 0.12, rho = -0.4)

Parameters:
Value:
TypeFlag p
E        1.5
S        100
X        1.52
Time     0.25
r        0.08
rf       0.05
q        0.04
sigmaS   0.2
sigmaE   0.12
rho      -0.4

Option Price:
4.208856

Description:
Wed Mar 20 16:25:20 2019

> TakeoverFXOption(V = 100, B = 100, E = 1.5, X = 1.55, Time = 1,
+                  r = 0.08, rf = 0.06, sigmaV = 0.20, sigmaE = 0.25, rho = 0.1)

Title:
Takeover FX Option

Call:
TakeoverFXOption(V = 100, B = 100, E = 1.5, X = 1.55, Time = 1,
r = 0.08, rf = 0.06, sigmaV = 0.2, sigmaE = 0.25, rho = 0.1)

Parameters:
Value:
V      100
B      100
E      1.5
X      1.55
Time   1
r      0.08
rf     0.06
q      function (save = "default", status = 0, runLast = TRUE) \n.Internal(quit(save, status, runLast))
sigmaV 0.2
sigmaE 0.25
rho    0.1

Option Price:
4.977936

Description:
Wed Mar 20 16:25:35 2019

> vanilla <- GBSOption(TypeFlag = "c", S = 100, X = 90, Time = 1,
+                      r = 0.02, b = -0.02, sigma = 0.3)
> KO <- sapply(100:300, FUN = StandardBarrierOption, TypeFlag = "cuo",
+              S = 100, X = 90, K = 0, Time = 1, r = 0.02, b = -0.02, sigma = 0.30)
Warning messages:
1: In if (X >= H) { :
the condition has length > 1 and only the first element will be used
2: In if (X < H) { :
the condition has length > 1 and only the first element will be used
> plot(KO[]@price, type = "l",
+      xlab = "barrier distance from spot",
+      ylab = "price of option",
+      main = "Price of KO converges to plain vanilla")
> abline(h = vanilla@price, col = "red")
> library(plot3D)
Warning message:
package ‘plot3D’ was built under R version 3.5.2
> BS_surface <- function(S, Time, FUN, ...) {
+     require(plot3D)
+     n <- length(S)
+     k <- length(Time)
+     m <- matrix(0, n, k)
+     for (i in 1:n){
+         for (j in 1:k){
+             l <- list(S = S[i], Time = Time[j], ...)
+             m[i,j] <- max(do.call(FUN, l)@price, 0)
+         }
+     }
+ persp3D(z = m, xlab = "underlying", ylab = "Remaining time",
+         zlab = "option price", phi = 30, theta = 20, bty = "b2")
+ }
>
> BS_surface(seq(1, 200,length = 200), seq(0, 2, length = 200),
+            GBSOption, TypeFlag = "c", X = 90, r = 0.02, b = 0, sigma = 0.3)
> ExecutiveStockOption(TypeFlag = "c", S = 60, X = 64, Time = 2,
+                      r = 0.07, b = 0.07-0.03, sigma = 0.38, lambda = 0.15)

Title:
Executive Stock Option Valuation

Call:
ExecutiveStockOption(TypeFlag = "c", S = 60, X = 64, Time = 2,
r = 0.07, b = 0.07 - 0.03, sigma = 0.38, lambda = 0.15)

Parameters:
Value:
TypeFlag c
S        60
X        64
Time     2
r        0.07
b        0.04
sigma    0.38
lambda   0.15

Option Price:
9.124388

Description:
Wed Mar 20 16:51:26 2019

> ForwardStartOption(TypeFlag = "c", S = 60, alpha = 1.1,
+                    time1 = 1, Time2 = 1/4, r = 0.08, b = 0.08-0.04, sigma = 0.30)

Title:
Forward Start Option Valuation

Call:
ForwardStartOption(TypeFlag = "c", S = 60, alpha = 1.1, time1 = 1,
Time2 = 1/4, r = 0.08, b = 0.08 - 0.04, sigma = 0.3)

Parameters:
Value:
TypeFlag c
S        60
alpha    1.1
time1    1
Time2    0.25
r        0.08
b        0.04
sigma    0.3

Option Price:
4.406449

Description:
Wed Mar 20 16:52:06 2019

> RatchetOption(TypeFlag = "c", S = 60, alpha = 1.1, time1 = c(1.00, 0.75),
+               Time2 = c(0.75, 0.50), r = 0.08, b = 0.04, sigma = 0.30)

Title:
Ratchet Option Valuation

Call:
RatchetOption(TypeFlag = "c", S = 60, alpha = 1.1, time1 = c(1,
0.75), Time2 = c(0.75, 0.5), r = 0.08, b = 0.04, sigma = 0.3)

Parameters:
Value:
TypeFlag c
S        60
alpha    1.1
time11   1
time12   0.75
Time21   0.75
Time22   0.5
r        0.08
b        0.04
sigma    0.3

Option Price:
3.2132

Description:
Wed Mar 20 16:52:51 2019

> TimeSwitchOption(TypeFlag = "c", S = 100, X = 110, Time = 1,
+                  r = 0.06, b = 0.06, sigma = 0.26, A = 5, m = 0, dt = 1/365)

Title:
Time Switch Option Valuation

Call:
TimeSwitchOption(TypeFlag = "c", S = 100, X = 110, Time = 1,
r = 0.06, b = 0.06, sigma = 0.26, A = 5, m = 0, dt = 1/365)

Parameters:
Value:
TypeFlag c
S        100
X        110
Time     1
r        0.06
b        0.06
sigma    0.26
A        5
m        0
d        0.00273972602739726

Option Price:
1.375037

Description:
Wed Mar 20 16:53:19 2019

> SimpleChooserOption(S = 50, X = 50, time1 = 1/4, Time2 = 1/2,
+                     r = 0.08, b = 0.08, sigma = 0.25)

Title:
Simple Chooser Option Valuation

Call:
SimpleChooserOption(S = 50, X = 50, time1 = 1/4, Time2 = 1/2,
r = 0.08, b = 0.08, sigma = 0.25)

Parameters:
Value:
S     50
X     50
time1 0.25
Time2 0.5
r     0.08
b     0.08
sigma 0.25

Option Price:
6.107073

Description:
Wed Mar 20 16:53:35 2019

> ComplexChooserOption(S = 50, Xc = 55, Xp = 48, Time = 0.25,
+                      Timec = 0.50, Timep = 0.5833, r = 0.10, b = 0.1-0.05,
+                      sigma = 0.35, doprint = TRUE)

Critical Value:
 51.11561

Title:
Complex Chooser Option Valuation

Call:
ComplexChooserOption(S = 50, Xc = 55, Xp = 48, Time = 0.25, Timec = 0.5,
Timep = 0.5833, r = 0.1, b = 0.1 - 0.05, sigma = 0.35, doprint = TRUE)

Parameters:
Value:
S             50
Xc            55
Xp            48
time          new("standardGeneric", .Data = function (x, ...) \nstandardGeneric("time"), generic = "time", package = "stats", group = list(), valueClass = character(0), signature = "x", default = new("derivedDefaultMethod", .Data = function (x, ...) \nUseMethod("time"), target = new("signature", .Data = "ANY", names = "x", package = "methods"), defined = new("signature", .Data = "ANY", names = "x", package = "methods"), generic = "time"), skeleton = (new("derivedDefaultMethod", .Data = function (x, ...) \nUseMethod("time"), target = new("signature", .Data = "ANY", names = "x", package = "methods"), defined = new("signature", .Data = "ANY", names = "x", package = "methods"), generic = "time"))(x, ...))
Timec         0.5
Timep         0.5833
r             0.1
b             0.05
sigma         0.35
criticalValue 51.1156056231326

Option Price:
6.050727

Description:
Wed Mar 20 16:53:59 2019

> OptionOnOption(TypeFlag = "pc", S = 500, X1 = 520, X2 = 50,
+                time1 = 1/2, Time2 = 1/4, r = 0.08, b = 0.08-0.03, sigma = 0.35)

Title:
Option On Option Valuation

Call:
OptionOnOption(TypeFlag = "pc", S = 500, X1 = 520, X2 = 50, time1 = 1/2,
Time2 = 1/4, r = 0.08, b = 0.08 - 0.03, sigma = 0.35)

Parameters:
Value:
S             500
X1            520
X2            50
time1         0.5
Time2         0.25
r             0.08
b             0.05
sigma         0.35
criticalValue 538.316546560699

Option Price:
21.19647

Description:
Wed Mar 20 16:54:18 2019

> HolderExtendibleOption(TypeFlag = "c", S = 100, X1 = 100,
+                        X2 = 105, time1 = 0.50, Time2 = 0.75, r = 0.08, b = 0.08,
+                        sigma = 0.25, A = 1)

Title:
Holder Extendible Option Valuation

Call:
HolderExtendibleOption(TypeFlag = "c", S = 100, X1 = 100, X2 = 105,
time1 = 0.5, Time2 = 0.75, r = 0.08, b = 0.08, sigma = 0.25,
A = 1)

Parameters:
Value:
TypeFlag c
S        100
X1       100
X2       105
time1    0.5
Time2    0.75
r        0.08
b        0.08
sigma    0.25
A        1

Option Price:
2.729216

Description:
Wed Mar 20 16:54:38 2019

> WriterExtendibleOption(TypeFlag = "c", S = 80, X1 = 90, X2 = 82,
+                        time1 = 0.50, Time2 = 0.75, r = 0.10, b = 0.10, sigma = 0.30)

Title:
Writer Extendible Option Valuation

Call:
WriterExtendibleOption(TypeFlag = "c", S = 80, X1 = 90, X2 = 82,
time1 = 0.5, Time2 = 0.75, r = 0.1, b = 0.1, sigma = 0.3)

Parameters:
Value:
TypeFlag c
S        80
X1       90
X2       82
time1    0.5
Time2    0.75
r        0.1
b        0.1
sigma    0.3

Option Price:
6.823755

Description:
Wed Mar 20 16:54:55 2019

> install.packages("plot3Drgl")
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/plot3Drgl_1.0.1.zip'
Content type 'application/zip' length 248378 bytes (242 KB)

package ‘plot3Drgl’ successfully unpacked and MD5 sums checked

> library(plot3D-packages)
Error in library(plot3D - packages) : 'package' must be of length 1
> par(mfrow = c(2, 1))
> x <- rchisq(1000, df = 4)
> hs <- hist(x, breaks = 15)
> hist3D(x = hs\$mids, y = 1, z = matrix(ncol = 1, data = hs\$density),
+        bty = "g", ylim = c(0., 2.0), scale = FALSE, expand = 20,
+        border = "black", col = "white", shade = 0.3, space = 0.1,
+        theta = 20, phi = 20, main = "3-D perspective")
> plotdev(xlim = c(-0.2, 1.2), ylim = c(-0.2, 1.2), theta = 45)
> ribbon3D(z = VV, scale = FALSE, expand = 0.01, bty = "g", phi = 20,
+          col = "lightblue", border = "black", shade = 0.2, ltheta = 90,
+          space = 0.3, ticktype = "detailed", d = 2, curtain = TRUE)
> pm <- par("mfrow")
>
> par(mfrow = c(2, 2))
> persp3D(z = volcano, main = "volcano", clab = c("height", "m"),
+         breaks = seq(80,200, by = 10))
> window()
Error in hasTsp(x) : argument "x" is missing, with no default
> persp3D(z = volcano, main = "volcano", clab = c("height", "m"),
+         breaks = seq(80,200, by = 10))
> persp3D(z = volcano, x = 1: nrow(volcano), y = 1:ncol(volcano),
+         expand = 0.3, main = "volcano", facets = FALSE, scale = FALSE,
+         clab = "height, m", colkey = list(side = 1, length = 0.5))
> V <- volcano[, seq(1, ncol(volcano), by = 3)]  # lower resolution
> ribbon3D(z = V, colkey = list(width = 0.5, length = 0.5,
+                               cex.axis = 0.8, side = 2), clab = "m")
> Vy <- volcano[seq(1, nrow(volcano), by = 3), ]
> ribbon3D(z = Vy, expand = 0.3, space = 0.3, along = "y",
+          colkey = list(width = 0.5, length = 0.5, cex.axis = 0.8))
>
> x <- seq(-pi, pi, by = 0.2)
> y <- seq(-pi, pi, by = 0.3)
> grid <- mesh(x, y)
> z    <- with(grid, cos(x) * sin(y))
> par(mfrow = c(2,2))
> persp3D(z = z, x = x, y = y)
> persp3D(z = z, x = x, y = y, facets = FALSE, curtain = TRUE)
> ribbon3D(z = z, x = x, y = y, along = "xy", space = 0.3)
> hist3D(z = z, x = x, y = y, border = "black")
> par(mfrow = c(2, 2))
> x <- seq(1, nrow(volcano), by = 3)
> y <- seq(1, ncol(volcano), by = 3)
> Volcano <- volcano [x, y]
> ribbon3D(z = Volcano, contour = TRUE, zlim= c(-100, 200),
+          image = TRUE)
> persp3D(z = Volcano, contour = TRUE, zlim= c(-200, 200), image = FALSE)
> persp3D(z = Volcano, x = x, y = y, scale = FALSE,
+         contour = list(nlevels = 20, col = "red"),
+         zlim = c(-200, 200), expand = 0.2,
+         image = list(col = grey (seq(0, 1, length.out = 100))))
> persp3D(z = Volcano, contour = list(side = c("zmin", "z", "350")),
+         zlim = c(-100, 400), phi = 20, image = list(side = 350))