Portfolio

 Portfolio
library("fPortfolio", lib.loc="~/R/win-library/3.5")
Loading required package: timeDate
Loading required package: timeSeries
Loading required package: fBasics
Loading required package: fAssets
Warning messages:
1: package ‘fPortfolio’ 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.1
5: package ‘fAssets’ was built under R version 3.5.2
> library(fPortfolio)
> donlp2NLPControl(
+     iterma=4000, nstep=20, fnscale=1, report=FALSE, rep.freq=1,
+     tau0=1, tau=0.1, del0=1, epsx=1e-05, delmin=0.1 * del0,
+     epsdif=1e-08, nreset.multiplier=1, difftype=3, epsfcn=1e-16,
+     taubnd=1, hessian=FALSE, te0=TRUE, te1=FALSE, te2=FALSE,
+     te3=FALSE, silent=TRUE, intakt=TRUE)
Error in donlp2NLPControl(iterma = 4000, nstep = 20, fnscale = 1, report = FALSE,  :
  object 'del0' not found
> rdonlp2NLP(start, objective,
+            lower=0, upper=1, linCons, funCons, control=list())
Error in rdonlp2NLP(start, objective, lower = 0, upper = 1, linCons, funCons,  :
  object 'linCons' not found
> donlp2NLP(start, objective,
+           par.lower=NULL, par.upper=NULL,
+           eqA=NULL, eqA.bound=NULL,
+           ineqA=NULL, ineqA.lower=NULL, ineqA.upper=NULL,
+           eqFun=list(), eqFun.bound=NULL,
+           ineqFun=list(), ineqFun.lower=NULL, ineqFun.upper=NULL,
+           control=list())
Error in loadNamespace(name) : there is no package called ‘Rdonlp2’
> nlminb2NLPControl(
+     eval.max=500, iter.max=400, trace=0, abs.tol=1e-20, rel.tol=1e-10,
+     x.tol=1.5e-08, step.min=2.2e-14, scale=1, R=1, beta.tol=1e-20) 
$`eval.max`
[1] 500

$iter.max
[1] 400

$trace
[1] 0

$abs.tol
[1] 1e-20

$rel.tol
[1] 1e-10

$x.tol
[1] 1.5e-08

$step.min
[1] 2.2e-14

$scale
[1] 1

$R
[1] 1

$beta.tol
[1] 1e-20

> rnlminb2
function (...)
{
    Rnlminb2::nlminb2(...)
}
<bytecode: 0x000000001d797450>
<environment: namespace:fPortfolio>
> ramplNLP(start, objective,
+          lower=0, upper=1, amplCons, control=list(), ...) 
Error: '...' used in an incorrect context
> amplNLP()   
[1] NA
> amplNLPControl(
+     solver="minos", project="ampl", trace=FALSE)
$`solver`
[1] "minos"

$project
[1] "ampl"

$trace
[1] FALSE

> rdonlp2NLP(start, objective,
+            lower=0, upper=1, linCons, funCons, control=list()) 
Error in rdonlp2NLP(start, objective, lower = 0, upper = 1, linCons, funCons,  :
  object 'linCons' not found
> donlp2NLP(start, objective,
+           par.lower=NULL, par.upper=NULL,
+           eqA=NULL, eqA.bound=NULL,
+           ineqA=NULL, ineqA.lower=NULL, ineqA.upper=NULL,
+           eqFun=list(), eqFun.bound=NULL,
+           ineqFun=list(), ineqFun.lower=NULL, ineqFun.upper=NULL,
+           control=list())   
Error in loadNamespace(name) : there is no package called ‘Rdonlp2’
> donlp2NLPControl(
+     iterma=4000, nstep=20, fnscale=1, report=FALSE, rep.freq=1,
+     tau0=1, tau=0.1, del0=1, epsx=1e-05, delmin=0.1 * del0,
+     epsdif=1e-08, nreset.multiplier=1, difftype=3, epsfcn=1e-16,
+     taubnd=1, hessian=FALSE, te0=TRUE, te1=FALSE, te2=FALSE,
+     te3=FALSE, silent=TRUE, intakt=TRUE)
Error in donlp2NLPControl(iterma = 4000, nstep = 20, fnscale = 1, report = FALSE,  :
  object 'del0' not found
> rdonlp2
function (...)
{
    Rdonlp2::donlp2(...)
}
<bytecode: 0x000000001b0c9470>
<environment: namespace:fPortfolio>
>
> rsolnpNLP(start, objective,
+           lower=0, upper=1, linCons, funCons, control=list()) 
Error in rsolnpNLP(start, objective, lower = 0, upper = 1, linCons, funCons,  :
  object 'linCons' not found
> solnpNLP(start, objective,
+          par.lower=NULL, par.upper=NULL,
+          eqA=NULL, eqA.bound=NULL,
+          ineqA=NULL, ineqA.lower=NULL, ineqA.upper=NULL,
+          eqFun=list(), eqFun.bound=NULL,
+          ineqFun=list(), ineqFun.lower=NULL, ineqFun.upper=NULL,
+          control=list())   
Error in solnpNLP(start, objective, par.lower = NULL, par.upper = NULL,  :
  object 'objective' not found
> solnpNLPControl(
+     rho=1, outer.iter=400, inner.iter=800, delta=1e-07, tol=1e-08, trace=0)
$`rho`
[1] 1

$outer.iter
[1] 400

$inner.iter
[1] 800

$delta
[1] 1e-07

$tol
[1] 1e-08

$trace
[1] 0

> rsolnp
Error: object 'rsolnp' not found
>
> rnlminb2NLP(start, objective,
+             lower=0, upper=1, linCons, funCons, control=list()) 
Error in rnlminb2NLP(start, objective, lower = 0, upper = 1, linCons,  :
  object 'linCons' not found
> nlminb2NLP(start, objective,
+            par.lower=NULL, par.upper=NULL,
+            eqA=NULL, eqA.bound=NULL,
+            ineqA=NULL, ineqA.lower=NULL, ineqA.upper=NULL,
+            eqFun=list(), eqFun.bound=NULL,
+            ineqFun=list(), ineqFun.lower=NULL, ineqFun.upper=NULL,
+            control=list())   
Error in loadNamespace(name) : there is no package called ‘Rnlminb2’
> nlminb2NLPControl(
+     eval.max=500, iter.max=400, trace=0, abs.tol=1e-20, rel.tol=1e-10,
+     x.tol=1.5e-08, step.min=2.2e-14, scale=1, R=1, beta.tol=1e-20) 
$`eval.max`
[1] 500

$iter.max
[1] 400

$trace
[1] 0

$abs.tol
[1] 1e-20

$rel.tol
[1] 1e-10

$x.tol
[1] 1.5e-08

$step.min
[1] 2.2e-14

$scale
[1] 1

$R
[1] 1

$beta.tol
[1] 1e-20

> rnlminb2
function (...)
{
    Rnlminb2::nlminb2(...)
}
<bytecode: 0x000000001d797450>
<environment: namespace:fPortfolio>
>
> ramplNLP(start, objective,
+          lower=0, upper=1, amplCons, control=list(), ...) 
Error: '...' used in an incorrect context
> amplNLP()   
[1] NA
> amplNLPControl(
+     solver="minos", project="ampl", trace=FALSE)
$`solver`
[1] "minos"

$project
[1] "ampl"

$trace
[1] FALSE

> rdonlp2NLP(start, objective,
+            lower=0, upper=1, linCons, funCons, control=list()) 
Error in rdonlp2NLP(start, objective, lower = 0, upper = 1, linCons, funCons,  :
  object 'linCons' not found
> donlp2NLP(start, objective,
+           par.lower=NULL, par.upper=NULL,
+           eqA=NULL, eqA.bound=NULL,
+           ineqA=NULL, ineqA.lower=NULL, ineqA.upper=NULL,
+           eqFun=list(), eqFun.bound=NULL,
+           ineqFun=list(), ineqFun.lower=NULL, ineqFun.upper=NULL,
+           control=list())   
Error in loadNamespace(name) : there is no package called ‘Rdonlp2’
> donlp2NLPControl(
+     iterma=4000, nstep=20, fnscale=1, report=FALSE, rep.freq=1,
+     tau0=1, tau=0.1, del0=1, epsx=1e-05, delmin=0.1 * del0,
+     epsdif=1e-08, nreset.multiplier=1, difftype=3, epsfcn=1e-16,
+     taubnd=1, hessian=FALSE, te0=TRUE, te1=FALSE, te2=FALSE,
+     te3=FALSE, silent=TRUE, intakt=TRUE)
Error in donlp2NLPControl(iterma = 4000, nstep = 20, fnscale = 1, report = FALSE,  :
  object 'del0' not found
> rdonlp2
function (...)
{
    Rdonlp2::donlp2(...)
}
<bytecode: 0x000000001b0c9470>
<environment: namespace:fPortfolio>
> nlminb2NLPControl(
+     eval.max=500, iter.max=400, trace=0, abs.tol=1e-20, rel.tol=1e-10,
+     x.tol=1.5e-08, step.min=2.2e-14, scale=1, R=1, beta.tol=1e-20) 
$`eval.max`
[1] 500

$iter.max
[1] 400

$trace
[1] 0

$abs.tol
[1] 1e-20

$rel.tol
[1] 1e-10

$x.tol
[1] 1.5e-08

$step.min
[1] 2.2e-14

$scale
[1] 1

$R
[1] 1

$beta.tol
[1] 1e-20

> rnlminb2
function (...)
{
    Rnlminb2::nlminb2(...)
}
<bytecode: 0x000000001d797450>
<environment: namespace:fPortfolio>
> library("tseries", lib.loc="~/R/win-library/3.5")

    ‘tseries’ version: 0.10-46

    ‘tseries’ is a package for time series
    analysis and computational finance.

    See ‘library(help="tseries")’ for
    details.

Warning message:
package ‘tseries’ was built under R version 3.5.2
> library(tseries)
> stabilityAnalytics(index, method=c("turns", "drawdowns", "garch",
+                                    "riskmetrics", "bcp", "pcout"), ...)
Error: '...' used in an incorrect context
> backtestPlot(object, which="all", labels=TRUE, legend=TRUE, at=NULL,
+              format=NULL, cex=0.6, font=1, family="mono")
Error in backtestAssetsPlot(object, labels, legend, at, format) :
  object 'object' not found
>
> backtestAssetsPlot(object, labels=TRUE, legend=TRUE, at=NULL, format=NULL)
Error in backtestAssetsPlot(object, labels = TRUE, legend = TRUE, at = NULL,  :
  object 'object' not found
> backtestWeightsPlot(object, labels=TRUE, legend=TRUE, at=NULL, format=NULL)
Error in backtestWeightsPlot(object, labels = TRUE, legend = TRUE, at = NULL,  :
  object 'object' not found
> backtestRebalancePlot(object, labels=TRUE, legend=TRUE, at=NULL, format=NULL)
Error in backtestRebalancePlot(object, labels = TRUE, legend = TRUE, at = NULL,  :
  object 'object' not found
> backtestPortfolioPlot(object, labels=TRUE, legend=TRUE, at=NULL, format=NULL)
Error in backtestPortfolioPlot(object, labels = TRUE, legend = TRUE, at = NULL,  :
  object 'object' not found
> backtestDrawdownPlot(object, labels=TRUE, legend=TRUE, at=NULL, format=NULL)
Error in backtestDrawdownPlot(object, labels = TRUE, legend = TRUE, at = NULL,  :
  object 'object' not found
> backtestReportPlot(object, cex=0.6, font=1, family="mono")
Error in getStrategyFun(object$backtest) : object 'object' not found
> backtestStats(100, FUN = "rollingSigma", ...)
Error: '...' used in an incorrect context
> library("PortfolioAnalytics", lib.loc="~/R/win-library/3.5")
Loading required package: zoo

Attaching package: ‘zoo’

The following object is masked from ‘package:timeSeries’:

    time<-

The following objects are masked from ‘package:base’:

    as.Date, as.Date.numeric

Loading required package: xts
Loading required package: foreach
Loading required package: PerformanceAnalytics

Package PerformanceAnalytics (1.5.2) loaded.
Copyright (c) 2004-2018 Peter Carl and Brian G. Peterson, GPL-2 | GPL-3
https://github.com/braverock/PerformanceAnalytics


Attaching package: ‘PerformanceAnalytics’

The following objects are masked from ‘package:timeDate’:

    kurtosis, skewness

The following object is masked from ‘package:graphics’:

    legend

Warning messages:
1: package ‘PortfolioAnalytics’ was built under R version 3.5.2
2: package ‘zoo’ was built under R version 3.5.1
3: package ‘xts’ was built under R version 3.5.1
4: package ‘foreach’ was built under R version 3.5.2
5: package ‘PerformanceAnalytics’ was built under R version 3.5.2
> library(PortfolioAnalytics)
> data(indexes)
> head(indexes)
           US Bonds US Equities Int'l Equities
1980-01-31  -0.0272      0.0610         0.0462
1980-02-29  -0.0669      0.0031        -0.0040
1980-03-31   0.0053     -0.0987        -0.1188
1980-04-30   0.0992      0.0429         0.0864
1980-05-31   0.0000      0.0562         0.0446
1980-06-30   0.0605      0.0296         0.0600
           Commodities US Tbill Inflation
1980-01-31      0.0568   0.0104    0.0149
1980-02-29     -0.0093   0.0106    0.0146
1980-03-31     -0.1625   0.0121    0.0120
1980-04-30      0.0357   0.0137    0.0095
1980-05-31      0.0573   0.0106    0.0095
1980-06-30      0.0533   0.0066    0.0000
Warning message:
index class is Date, which does not support timezones.
Expected 'UTC' timezone, but indexTZ is ''
> LPP <- LPP2005REC[1:12, 1:4]
> colnames(LPP) <- abbreviate(colnames(LPP), 2)
> finCenter(LPP) <- "GMT"
> plot(LPP[, 1], type = "o", col = "steelblue",
+      main = "LPP", xlab = "2005", ylab = "Return")
> plot(LPP[, 1], at="auto", type = "o", col = "steelblue",
+      main = "LPP", xlab = "2005", ylab = "Return")
> plot(LPP[, 1:2], type = "o", col = "steelblue",
+      main = "LPP", xlab = "2005", ylab = "Return"
+ )
> plot(LPP[, 1], LPP[, 2], type = "p", col = "steelblue",
+      main = "LPP", xlab = "Return 1", ylab = "Return 2")
> LPP <- as.timeSeries(data(LPP2005REC))
> ZRH <- as.timeSeries(LPP[,"SPI"], zone = "Zurich", FinCenter = "Zurich")
> NYC <- as.timeSeries(LPP[,"LMI"], zone = "NewYork", FinCenter = "NewYork")
> finCenter(ZRH)
[1] "Zurich"
> finCenter(NYC)
[1] "NewYork"
> plot(ZRH, at="auto", type = "p", pch = 19, col = "blue")
> points(NYC, pch = 19, col = "red")
> finCenter(ZRH) <- "Zurich"
> finCenter(NYC) <- "NewYork"
> at <- unique(round(time(ZRH)))
> plot(ZRH, type = "p", pch = 19, col = "blue", format = "%b %d", at = at,
+      FinCenter = "GMT", xlab = "GMT", main = "ZRH - GMT")
> points(NYC, FinCenter = "GMT", pch = 19, col = "red")
> data(edhec)
> ret <- edhec[, 1:4]
> pspec <- portfolio.spec(assets=colnames(ret))
> pspec <- add.constraint(pspec, type="box")
> pspec <- add.constraint(pspec, type="box", min=0.05, max=0.45)
> pspec <- add.constraint(pspec,
+                         type="box",
+                         min=c(0.05, 0.10, 0.08, 0.06),
+                         max=c(0.45, 0.55, 0.35, 0.65))
> centroid.buckets {PortfolioAnalytics}
Error: unexpected '{' in "centroid.buckets {"
> centroid.buckets(buckets, simulations = 1000)
Error in centroid.buckets(buckets, simulations = 1000) :
  object 'buckets' not found
> chart.EfficientFrontier(object, ...,
+                         match.col = "ES", n.portfolios = 25, xlim = NULL, ylim = NULL,
+                         cex.axis = 0.8, element.color = "darkgray", main = "Efficient Frontier",
+                         RAR.text = "SR", rf = 0, tangent.line = TRUE, cex.legend = 0.8,
+                         chart.assets = TRUE, labels.assets = TRUE, pch.assets = 21,
+                         cex.assets = 0.8)
Error: '...' used in an incorrect context
> var.portfolio(R, weights)
Error in as.vector(x, mode) :
  cannot coerce type 'closure' to vector of type 'any'
> Data
[1] NA
> library("FRAPO", lib.loc="~/R/win-library/3.5")
Loading required package: cccp
Loading required package: Rglpk
Loading required package: slam
Using the GLPK callable library version 4.47
Financial Risk Modelling and Portfolio Optimisation with R (version 0.4-1)

Warning messages:
1: package ‘FRAPO’ was built under R version 3.5.2
2: package ‘cccp’ was built under R version 3.5.2
3: package ‘Rglpk’ was built under R version 3.5.2
4: package ‘slam’ was built under R version 3.5.2
> library(FRAPO)
> data(StockIndex)
> y <- StockIndex[, "SP500"]
> cs <- capser(y, min = 100, max = 200)
> head(cs)
[1] 200 200 200 200 200 200
> yret <- diff(log(y))
> bilson <- trdbilson(yret, exponent = 2)
> head(bilson)
[1]  0.01948730 -0.01935785  0.01177193 -0.04517835
[5]  0.10349955 -0.01545405
> es <- trdes(yret, lambda = 0.95)
> head(es)
[1]  0.018485339 -0.017438597  0.010304526
[4] -0.042136563  0.093941299 -0.009969699
> cs <- capser(y, min = 100, max = 200)
> yret <- diff(log(y))
> binary <- trdbinary(yret)
> head(binary)
[1]  0.02477189 -0.02460780  0.01497858 -0.05712579
[5]  0.12829276 -0.01965560
> hp <- trdhp(y, lambda = 1600)
> head(hp)
[1] 387.1268 389.8778 392.6293 395.3850 398.1460
[6] 400.9114
> sma <- trdsma(y, n.periods = 24)
> head(sma, 30)
 [1]       NA       NA       NA       NA       NA
 [6]       NA       NA       NA       NA       NA
[11]       NA       NA       NA       NA       NA
[16]       NA       NA       NA       NA       NA
[21]       NA       NA       NA 418.2483 420.7617
[26] 423.6004 426.5617 429.7025 433.3096 435.5387
> wma <- trdwma(y, weights = c(0.4, 0.3, 0.2, 0.1))
> head(wma, 30)
 [1]      NA      NA      NA 391.205 384.938
 [6] 395.898 402.343 408.264 408.556 410.505
[11] 412.633 411.220 416.691 416.362 417.228
[16] 418.201 423.188 430.449 435.296 439.885
[21] 445.320 444.131 446.805 448.474 448.468
[26] 454.988 457.319 462.336 463.207 465.452
> require(graphics)
> x <- c(0,0,0,100,0,0,0)
> y <- c(0,0,1, 2 ,1,0,0)/4
> zapsmall(convolve(x, y))
[1] 50 25  0  0  0  0 25
> zapsmall(convolve(x, y[3:5], type = "f"))
[1]  0 25 50 25  0
> x <- rnorm(50)
> y <- rnorm(50)
> all.equal(convolve(x, y, conj = FALSE), rev(convolve(rev(y),x)))
[1] TRUE
> n <- length(x <- -20:24)
> y <- (x-10)^2/1000 + rnorm(x)/8
> Han <- function(y) # Hanning
+     convolve(y, c(1,2,1)/4, type = "filter")
> plot(x, y, main = "Using  convolve(.) for Hanning filters")
> lines(x[-c(1  , n)      ], Han(y), col = "red")
> lines(x[-c(1:2, (n-1):n)], Han(Han(y)), lwd = 2, col = "dark blue")
> library("cvar", lib.loc="~/R/win-library/3.5")

Attaching package: ‘cvar’

The following objects are masked from ‘package:PerformanceAnalytics’:

    ES, VaR

Warning message:
package ‘cvar’ was built under R version 3.5.2
> library(cvar)
> mo <- GarchModel(omega = 0.4, alpha = 0.3, beta = 0.5)
> a1 <- sim_garch1c1(mo, n = 1000, n.start = 100)
> a.pred <- predict(mo, n.ahead = 5, Nsim = 1000, eps = a1$eps, sigmasq = a1$h, seed = 1234)
> a.pred$eps
[1] 0 0 0 0 0
> a.pred$pi_plugin
           lwr      upr
[1,] -1.992935 1.992935
[2,] -2.171179 2.171179
[3,] -2.303866 2.303866
[4,] -2.404750 2.404750
[5,] -2.482507 2.482507
> a.pred$pi_sim
          2.5%    97.5%
[1,] -1.962977 1.978131
[2,] -2.255450 2.206946
[3,] -2.182829 2.342846
[4,] -2.310415 2.150915
[5,] -2.440387 2.461284
> t(apply(a.pred$dist_sim$eps, 1, function(x) quantile(x, c(0.025, 0.975))))
          2.5%    97.5%
[1,] -1.962977 1.978131
[2,] -2.255450 2.206946
[3,] -2.182829 2.342846
[4,] -2.310415 2.150915
[5,] -2.440387 2.461284
> t(apply(a.pred$dist_sim$eps, 1, function(x) summary(x)))
          Min.    1st Qu.       Median
[1,] -3.146715 -0.6712101 -0.051807252
[2,] -5.517102 -0.7102704  0.046509597
[3,] -4.474419 -0.7719921 -0.013547190
[4,] -3.533618 -0.7864974 -0.001579576
[5,] -4.618284 -0.7965248 -0.018277768
             Mean   3rd Qu.     Max.
[1,] -0.003854556 0.6966841 3.249664
[2,]  0.012472968 0.7621015 3.638396
[3,] -0.005678992 0.7372569 3.750280
[4,] -0.027251506 0.7057924 3.989376
[5,] -0.015917251 0.7528478 4.066457
> plot(density(a.pred$dist_sim$eps[2, ]), ylim = c(0,.25))
> lines(density(a.pred$dist_sim$eps[5, ]), col = "blue")
> a.pred$h
[1] 1.033928 1.227142 1.381714 1.505371 1.604297
> sqrt(a.pred$h)
[1] 1.016823 1.107765 1.175463 1.226936 1.266608
> plot(density(sqrt(a.pred$dist_sim$h[2, ])), xlim = c(0, 6))
> x <- seq(0.01, 0.99, length = 100)
> y <- sapply(x, function(p) cvar::ES(qnorm, x = p, dist.type = "qf"))
> yS <- sapply(x, function(p) - VaRES::esnormal(p))
Error in loadNamespace(name) : there is no package called ‘VaRES’
Called from: value[[3L]](cond)
Browse[1]> plot(x, y)
Browse[1]>
Found more than one class "xts" in cache; using the first, from namespace 'quantmod'
Also defined by ‘FRAPO’'
Also defined by ‘FRAPO’
> lines(x, yS, col = "blue")
Error in xy.coords(x, y) : object 'yS' not found
> mo1a <- GarchModel(omega = 1, alpha = 0.3, beta = 0.5)
> mo1b <- GarchModel(omega = 1, alpha = 0.3, beta = 0.5, cond.dist = "norm")
>
>
> mo2 <- GarchModel(omega = 1, alpha = 0.3, beta = 0.5, esp0 = - 1.5, h0 = 4.96)
> mot <- GarchModel(omega = 1, alpha = 0.3, beta = 0.5, cond.dist = list("std", nu = 5)
+ )
> View(mot)
>
> View(mo1b)
> View(mo2)
> mo2
$`omega`
[1] 1

$alpha
[1] 0.3

$beta
[1] 0.5

$esp0
[1] -1.5

$h0
[1] 4.96

attr(,"class")
[1] "garch1c1"
> mo <- GarchModel(omega = 0.4, alpha = 0.3, beta = 0.5)
> a1 <- sim_garch1c1(mo, n = 1000, n.start = 100)
> a.pred <- predict(mo, n.ahead = 5, Nsim = 1000, eps = a1$eps, sigmasq = a1$h, seed = 1234)
> a.pred$eps
[1] 0 0 0 0 0
> a.pred$pi_plugin
           lwr      upr
[1,] -3.438538 3.438538
[2,] -3.315934 3.315934
[3,] -3.214486 3.214486
[4,] -3.130961 3.130961
[5,] -3.062502 3.062502
> a.pred$pi_sim
          2.5%    97.5%
[1,] -3.386849 3.412995
[2,] -3.497097 3.303965
[3,] -2.941401 3.237982
[4,] -3.082723 2.933992
[5,] -3.021358 3.018965
> t(apply(a.pred$dist_sim$eps, 1, function(x) quantile(x, c(0.025, 0.975))
+ )
+ )
          2.5%    97.5%
[1,] -3.386849 3.412995
[2,] -3.497097 3.303965
[3,] -2.941401 3.237982
[4,] -3.082723 2.933992
[5,] -3.021358 3.018965
> t(apply(a.pred$dist_sim$eps, 1, function(x) summary(x)))
          Min.    1st Qu.       Median
[1,] -5.429227 -1.1580814 -0.089386336
[2,] -9.204317 -1.0739463  0.071430081
[3,] -6.710671 -1.0521054 -0.019127552
[4,] -4.758922 -0.9992271 -0.001989747
[5,] -6.334744 -0.9571489 -0.020867447
             Mean   3rd Qu.     Max.
[1,] -0.006650510 1.2020332 5.606852
[2,]  0.014196985 1.1367528 5.709787
[3,] -0.002970354 0.9980880 5.437128
[4,] -0.034739711 0.8886597 4.829568
[5,] -0.017372954 0.8908675 6.468912
> plot(density(a.pred$dist_sim$eps[2, ]), ylim = c(0,.25))
> lines(density(a.pred$dist_sim$eps[5, ]), col = "blue")
> cvar::VaR(qnorm, x = c(0.01, 0.05), dist.type = "qf")
[1] 2.326348 1.644854
> muA <- 0.006408553
> sigma2A <- 0.0004018977
> res1 <- cvar::VaR(qnorm, x = 0.05, mean = muA, sd = sqrt(sigma2A))
> res2 <- cvar::VaR(qnorm, x = 0.05, intercept = muA, slope = sqrt(sigma2A))
> abs((res2 - res1)) # 0, intercept/slope equivalent to mean/sd
[1] 0
> res1a <- cvar::VaR(pnorm, x = 0.05, dist.type = "cdf", mean = muA, sd = sqrt(sigma2A))
> res2a <- cvar::VaR(pnorm, x = 0.05, dist.type = "cdf", intercept = muA, slope = sqrt(sigma2A))
> abs((res1a - res2)) # 3.287939e-09
[1] 3.287939e-09
> abs((res2a - res2)) # 5.331195e-11, intercept/slope better numerically
[1] 5.331195e-11
> res1b <- cvar::VaR(pnorm, x = 0.05, dist.type = "cdf",
+                    mean = muA, sd = sqrt(sigma2A), tol = .Machine$double.eps^0.75)
> res2b <- cvar::VaR(pnorm, x = 0.05, dist.type = "cdf",
+                    intercept = muA, slope = sqrt(sigma2A), tol = .Machine$double.eps^0.75)
> abs((res1b - res2)) # 6.938894e-18 # both within machine precision
[1] 6.938894e-18
> abs((res2b - res2)) # 1.040834e-16
[1] 1.040834e-16
> abs((res1b - res2)/res2) # 2.6119e-16 # both within machine precision
[1] 2.6119e-16
> abs((res2b - res2)/res2) # 3.91785e-15
[1] 3.91785e-15
> if (requireNamespace("PerformanceAnalytics", quietly = TRUE)) withAutoprint({
+     data(edhec, package = "PerformanceAnalytics")
+     mu <- apply(edhec, 2, mean)
+     sigma2 <- apply(edhec, 2, var)
+     musigma2 <- cbind(mu, sigma2)
+ ## analogous calc. with PerformanceAnalytics::VaR
+ vPA <- apply(musigma2, 1, function(x)
+     PerformanceAnalytics::VaR(p = .95, method = "gaussian", invert = FALSE,
+                               mu = x[1], sigma = x[2], weights = 1)
+ max(abs((vPA - vAz1))) # 5.551115e-17
Error: unexpected symbol in:
"                              mu = x[1], sigma = x[2], weights = 1)
max"
> max(abs((vPA - vAz2))) #   ""
Error: object 'vPA' not found
>
> max(abs((vPA - vAz1a))) # 3.287941e-09
Error: object 'vPA' not found
> max(abs((vPA - vAz2a))) #  1.465251e-10, intercept/slope better
Error: object 'vPA' not found
>
> max(abs((vPA - vAz1b))) # 4.374869e-13
Error: object 'vPA' not found
> max(abs((vPA - vAz2b))) # 3.330669e-16
Error: object 'vPA' not found
> }
Error: unexpected '}' in "}"
> library("scenario", lib.loc="~/R/win-library/3.5")
Warning message:
package ‘scenario’ was built under R version 3.5.2
> library(scenario)
> centroids <- cbind(c(0,2,3), c(0,2,1), c(0,-2,-3),c(0,-2,-1))
There were 36 warnings (use warnings() to see them)
> matplot(centroids, type="l", lwd = 3, col = "black", lty = 3)
> scenarios <- matrix(rep(centroids,5), ncol=20) + matrix(rnorm(60,0,0.25),ncol=20)
> matlines(scenarios, col = "grey")
> treeStruct <- rbind(c(1,1,1,1),
+                     c(2,2,5,5),
+                     c(3,4,6,7))
> checktree(treeStruct)
>

> tree <- buildtree(scenarios, treeStruct, jMax = 1000)
> matlines(centroids,lwd = 3, col = "black", lty = 3)
> matlines(centroids,lwd = 3, col = "green", lty = 3)

Post a Comment

0 Comments