The functions are exported via the NAMESPACE mechanism
You are welcome to redistribute it under certain conditions.Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
[Workspace loaded from ~/.RData]
Loading required package: fOptions
Loading required package: timeDate
Loading required package: timeSeries
Loading required package: fBasics
> install.packages("BLCOP")
Installing package into ‘C:/Users/ADMIN/Documents/R/win-library/3.5’
(as ‘lib’ is unspecified)
also installing the dependency ‘RUnit’
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/RUnit_0.4.32.zip'
Content type 'application/zip' length 296247 bytes (289 KB)
downloaded 289 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/BLCOP_0.3.1.zip'
Content type 'application/zip' length 811518 bytes (792 KB)
downloaded 792 KB
package ‘RUnit’ successfully unpacked and MD5 sums checked
package ‘BLCOP’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\ADMIN\AppData\Local\Temp\RtmpM3bcRt\downloaded_packages
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(BLCOP)
Loading required package: MASS
Loading required package: quadprog
Warning message:
package ‘BLCOP’ was built under R version 3.5.3
> pick <- newPMatrix(letters[1:8], 3)
> pick[1,7] <- 1
> pick[2,1] <- -1
> pick[2,2] <- 1
> pick[3, 3:6] <- c(0.9, -0.9, .1, -.1)
> confidences <- 1 / c(0.00709, 0.000141, 0.000866)
> myViews <- BLViews(pick, q = c(0.0525, 0.0025, 0.02), confidences, letters[1:8]
+ )
> myViews
1 : 1*g=0.0525 + eps. Confidence: 141.0437
2 : -1*a+1*b=0.0025 + eps. Confidence: 7092.199
3 : 0.9*c+-0.9*d+0.1*e+-0.1*f=0.02 + eps. Confidence: 1154.734
> dispersion <- c(.376,.253,.360,.333,.360,.600,.397,.396,.578,.775) / 1000
> sigma <- BLCOP:::.symmetricMatrix(dispersion, dim = 4)
> caps <- rep(1/4, 4)
> mu <- 2.5 * sigma
> dim(mu) <- NULL
> marketDistribution <- mvdistribution("mt", mean = mu, S = sigma, df = 5 )
Error in mvdistribution("mt", mean = mu, S = sigma, df = 5) :
Sampling function for this distribution does not exist!
> pick <- newPMatrix(c("SP", "FTSE", "CAC", "DAX"), 1)
> pick[1,4] <- 1
> vdist <- list(distribution("unif", min = -0.02, max = 0))
> views <- COPViews(pick, vdist, 0.2, c("SP", "FTSE", "CAC", "DAX"))
> CAPMList(monthlyReturns, marketIndex = sp500Returns, riskFree = US13wTB, regFunc = "rlm")
alphas betas
IBM 0.015540244 1.1901296
MS 0.053239036 1.7398334
DELL 0.023938008 1.3389280
C 0.016934353 1.1822813
JPM 0.035134257 1.5712330
BAC 0.007196188 0.9702085
> x <- c(0.001005,0.001328,-0.000579,-0.000675,0.000121,0.000128,-0.000445,-0.000437 ,
+ 0.001328,0.007277,-0.001307,-0.000610,-0.002237,-0.000989,0.001442,-0.001535 ,
+ -0.000579,-0.001307,0.059852,0.027588,0.063497,0.023036,0.032967,0.048039 ,
+ -0.000675,-0.000610,0.027588,0.029609,0.026572,0.021465,0.020697,0.029854 ,
+ 0.000121,-0.002237,0.063497,0.026572,0.102488,0.042744,0.039943,0.065994 ,
+ 0.000128,-0.000989,0.023036,0.021465,0.042744,0.032056,0.019881,0.032235 ,
+ -0.000445,0.001442,0.032967,0.020697,0.039943,0.019881,0.028355,0.035064 ,
+ -0.000437,-0.001535,0.048039,0.029854,0.065994,0.032235,0.035064,0.079958 )
> varCov <- matrix(x, ncol = 8, nrow = 8)
> mu <- c(0.08, 0.67,6.41, 4.08, 7.43, 3.70, 4.80, 6.60) / 100
> pick <- matrix(0, ncol = 8, nrow = 3, dimnames = list(NULL, letters[1:8]))
> pick[1,7] <- 1
> pick[2,1] <- -1; pick[2,2] <- 1
> pick[3, 3:6] <- c(0.9, -0.9, .1, -.1)
> confidences <- 1 / c(0.000709, 0.000141, 0.000866)
> myViews <- BLViews(pick, c(0.0525, 0.0025, 0.02), confidences, letters[1:8])
> myPosterior <- posteriorEst(myViews, tau = 0.025, mu, varCov )
> myPosterior
Prior means:
a b c d e
0.0008 0.0067 0.0641 0.0408 0.0743
f g h
0.0370 0.0480 0.0660
Posterior means:
a b c
0.000636636 0.004983111 0.064945904
d e f
0.043209911 0.075889579 0.039353875
g h
0.049335088 0.068439660
Posterior covariance:
[,1] [,2]
[1,] 0.0010297637 0.0013570700
[2,] 0.0013570700 0.0073802381
[3,] -0.0005877981 -0.0013326418
[4,] -0.0006868193 -0.0006311548
[5,] 0.0001321052 -0.0022651315
[6,] 0.0001366368 -0.0010033018
[7,] -0.0004518421 0.0014486393
[8,] -0.0004403174 -0.0015640550
[,3] [,4]
[1,] -0.0005877981 -0.0006868193
[2,] -0.0013326418 -0.0006311548
[3,] 0.0606199378 0.0280331207
[4,] 0.0280331207 0.0301452738
[5,] 0.0642059208 0.0269354857
[6,] 0.0233376823 0.0218124452
[7,] 0.0333195421 0.0209724486
[8,] 0.0486120217 0.0303038126
[,5] [,6]
[1,] 0.0001321052 0.0001366368
[2,] -0.0022651315 -0.0010033018
[3,] 0.0642059208 0.0233376823
[4,] 0.0269354857 0.0218124452
[5,] 0.1039853518 0.0434735925
[6,] 0.0434735925 0.0326741247
[7,] 0.0403758705 0.0201390307
[8,] 0.0668800713 0.0327345035
[,7] [,8]
[1,] -0.0004518421 -0.0004403174
[2,] 0.0014486393 -0.0015640550
[3,] 0.0333195421 0.0486120217
[4,] 0.0209724486 0.0303038126
[5,] 0.0403758705 0.0668800713
[6,] 0.0201390307 0.0327345035
[7,] 0.0286908498 0.0354784746
[8,] 0.0354784746 0.0813542571
> confidences(myViews) <- 1 / c(0.0001, 0.0001, 0.0005)
> myPosterior2 <- posteriorEst(myViews, tau = 0.025, mu, varCov )
> myPosterior2
Prior means:
a b c d e
0.0008 0.0067 0.0641 0.0408 0.0743
f g h
0.0370 0.0480 0.0660
Posterior means:
a b c
0.0005567763 0.0046938861 0.0668960593
d e f
0.0455483701 0.0784192403 0.0415007851
g h
0.0516138866 0.0713497046
Posterior covariance:
[,1] [,2]
[1,] 0.0010295987 0.0013563765
[2,] 0.0013563765 0.0073668897
[3,] -0.0005828989 -0.0013321345
[4,] -0.0006820877 -0.0006324597
[5,] 0.0001383777 -0.0022612469
[6,] 0.0001410988 -0.0010018496
[7,] -0.0004468617 0.0014431554
[8,] -0.0004338236 -0.0015631163
[,3] [,4]
[1,] -0.0005828989 -0.0006820877
[2,] -0.0013321345 -0.0006324597
[3,] 0.0602995605 0.0278395293
[4,] 0.0278395293 0.0299654279
[5,] 0.0638145738 0.0266953375
[6,] 0.0231479118 0.0216471782
[7,] 0.0330542399 0.0207699549
[8,] 0.0482749436 0.0300556089
[,5] [,6]
[1,] 0.0001383777 0.0001410988
[2,] -0.0022612469 -0.0010018496
[3,] 0.0638145738 0.0231479118
[4,] 0.0266953375 0.0216471782
[5,] 0.1035062513 0.0432382103
[6,] 0.0432382103 0.0325205456
[7,] 0.0400505520 0.0199488216
[8,] 0.0664653327 0.0324987249
[,7] [,8]
[1,] -0.0004468617 -0.0004338236
[2,] 0.0014431554 -0.0015631163
[3,] 0.0330542399 0.0482749436
[4,] 0.0207699549 0.0300556089
[5,] 0.0400505520 0.0664653327
[6,] 0.0199488216 0.0324987249
[7,] 0.0284411037 0.0351700306
[8,] 0.0351700306 0.0809678994
> dispersion <- c(.376,.253,.360,.333,.360,.600,.397,.396,.578,.775) / 1000
> sigma <- BLCOP:::.symmetricMatrix(dispersion, dim = 4)
> caps <- rep(1/4, 4)
> mu <- 2.5 * sigma
> dim(mu) <- NULL
> marketDistribution <- mvdistribution("mt", mean = mu, S = sigma, df = 5 )
Error in mvdistribution("mt", mean = mu, S = sigma, df = 5) :
Sampling function for this distribution does not exist!
> pick <- matrix(0, ncol = 4, nrow = 1, dimnames = list(NULL, c("SP", "FTSE", "CAC", "DAX"))
+ )
> pick[1,4] <- 1
> vdist <- list(distribution("unif", min = -0.02, max = 0))
> views <- COPViews(pick, vdist, 0.2, c("SP", "FTSE", "CAC", "DAX"))
> posterior <- COPPosterior(marketDistribution, views)
Error in classNames %in% class(object) :
object 'marketDistribution' not found
> stocks <- colnames(monthlyReturns)
> pick <- matrix(0, ncol = 6, nrow = 2, dimnames = list(NULL, stocks))
> pick[1,"IBM"] <- 1
> pick[1, "DELL"] <- 0.04
> pick[2, "C"] <- 1
> pick[2, "JPM"] <- 0.6
> confidences <- 1 / c(0.7, 0.1)
> views <- BLViews( P = pick, q = c(0.1,0.1) , confidences = confidences,stocks)
> deleteViews(views, 1)
1 : 1*C+0.6*JPM=0.1 + eps. Confidence: 10
> dispersion <- c(.376,.253,.360,.333,.360,.600,.397,.396,.578,.775) / 1000
> sigma <- BLCOP:::.symmetricMatrix(dispersion, dim = 4)
> caps <- rep(1/4, 4)
> mu <- 2.5 * sigma
> dim(mu) <- NULL
> marketDistribution <- mvdistribution("mt", mean = mu, S = sigma, df = 5 )
Error in mvdistribution("mt", mean = mu, S = sigma, df = 5) :
Sampling function for this distribution does not exist!
> pick <- matrix(0, ncol = 4, nrow = 1, dimnames = list(NULL, c("SP", "FTSE", "CAC", "DAX")))
> pick[1,4] <- 1
> vdist <- list(distribution("unif", min = -0.02, max = 0))
> views <- COPViews(pick, vdist, 0.2, c("SP", "FTSE", "CAC", "DAX"))
> posterior <- COPPosterior(marketDistribution, views)
Error in classNames %in% class(object) :
object 'marketDistribution' not found
> densityPlots(posterior, 4)
Error in densityPlots(posterior, 4) : object 'posterior' not found
> ts.plot(sp500Returns)
> ts.plot(US13wTB)
> x <- distribution("pois", lambda = 5)
> hist(sampleFrom(x, 1000), col = "blue", prob = TRUE)
> myUnif <- distribution("unif", min = -0.1, max = 0.1)
> hist(sampleFrom(myUnif, 1000))
> mvNormal <- mvdistribution("mnorm", mean = c(1, 5), varcov = diag(c(2, 0.1)))
Error in mvdistribution("mnorm", mean = c(1, 5), varcov = diag(c(2, 0.1))) :
Sampling function for this distribution does not exist!
> x <- sampleFrom(mvNormal, 1000)
Error in classNames %in% class(object) : object 'mvNormal' not found
> mvNormal <- mvdistribution("mnorm", mean = c(1, 5), varcov = diag(c(2, 0.1)))
Error in mvdistribution("mnorm", mean = c(1, 5), varcov = diag(c(2, 0.1))) :
Sampling function for this distribution does not exist!
> showClass("distribution")
Class "distribution" [package "BLCOP"]
Slots:
Name: RName parameters
Class: character numeric
> showClass("mvdistribution")Class "mvdistribution" [package "BLCOP"]
Slots:
Name: RName parameters
Class: character list
> showClass("distribution")
Class "distribution" [package "BLCOP"]
Slots:
Name: RName parameters
Class: character numeric
> CAPMList(monthlyReturns, marketIndex = sp500Returns, riskFree = US13wTB)
alphas betas
IBM 0.020883598 1.2806423
MS 0.059548398 1.8186393
DELL 0.017010062 1.2843338
C 0.014492325 1.1953415
JPM 0.027365230 1.5623610
BAC 0.002829908 0.9628988
> pickMatrix <- matrix(c(rep(1/2, 2), -1, rep(0, 3)), nrow = 1, ncol = 6 )
> views <- BLViews(P = pickMatrix, q = 0.08,confidences = 100,
+ assetNames = colnames(monthlyReturns))
> marketPosterior <- BLPosterior(monthlyReturns, views, marketIndex = sp500Returns,
+ riskFree = US13wTB)
> posteriorFeasibility(marketPosterior)
$`mahalDist`
[1] 0.06484571
$mahalDistProb
[1] 0.9999945
$sensitivities
[1] "Not implemented yet"
> install.packages("gumbel")
Installing package into ‘C:/Users/ADMIN/Documents/R/win-library/3.5’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/gumbel_1.10-2.zip'
Content type 'application/zip' length 471003 bytes (459 KB)
downloaded 459 KB
package ‘gumbel’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\ADMIN\AppData\Local\Temp\RtmpM3bcRt\downloaded_packages
> library(gumbel)
Warning message:
package ‘gumbel’ was built under R version 3.5.2
> u<-seq(0,1, .1)
> v<-u
> dgumbel(u,v,1)
[1] NaN 1 1 1 1 1 1 1 1
[10] 1 NaN
> pgumbel(u,v,1)
[1] 0.00 0.01 0.04 0.09 0.16 0.25 0.36
[8] 0.49 0.64 0.81 1.00
> dgumbel(cbind(u,v), alpha=1)
[1] NaN 1 1 1 1 1 1 1 1
[10] 1 NaN
> pgumbel(cbind(u,v), alpha=1)
[1] 0.00 0.01 0.04 0.09 0.16 0.25 0.36
[8] 0.49 0.64 0.81 1.00
> x <- cbind(u,u,u)
> y <- array(u, c(1,11,3))
> pgumbel(x, alpha=2, dim=3)
[1] 0.00000000 0.01853315 0.06156706
[4] 0.12426461 0.20452561 0.30102374
[7] 0.41280666 0.53914047 0.67943346
[10] 0.83319317 1.00000000
> pgumbel(y, alpha=2, dim=3)
[,1] [,2] [,3]
[1,] 0 0.01853315 0.06156706
[,4] [,5] [,6]
[1,] 0.1242646 0.2045256 0.3010237
[,7] [,8] [,9]
[1,] 0.4128067 0.5391405 0.6794335
[,10] [,11]
[1,] 0.8331932 1
> dgumbel(x, alpha=2, dim=3)
[1] NaN 6.922376 3.646759
[4] 2.770993 2.510089 2.586175
[7] 3.024395 4.149303 7.366620
[10] 23.650259 NaN
> dgumbel(y, alpha=2, dim=3)
[,1] [,2] [,3] [,4]
[1,] NaN 6.922376 3.646759 2.770993
[,5] [,6] [,7] [,8]
[1,] 2.510089 2.586175 3.024395 4.149303
[,9] [,10] [,11]
[1,] 7.36662 23.65026 NaN
> x <- cbind(x,u)
> pgumbel(x, alpha=3, dim=4)
[1] 0.00000000 0.02585824 0.07770595
[4] 0.14790462 0.23351222 0.33277038
[7] 0.44446448 0.56768637 0.70172176
[10] 0.84598860 1.00000000
> y <- array(u, c(2,1,11,4))
> pgumbel(y, alpha=3, dim=4)
, , 1
[,1]
[1,] 0.00000000
[2,] 0.02585824
, , 2
[,1]
[1,] 0.07770595
[2,] 0.14790462
, , 3
[,1]
[1,] 0.2335122
[2,] 0.3327704
, , 4
[,1]
[1,] 0.4444645
[2,] 0.5676864
, , 5
[,1]
[1,] 0.7017218
[2,] 0.8459886
, , 6
[,1]
[1,] 1
[2,] 0
, , 7
[,1]
[1,] 0.02585824
[2,] 0.07770595
, , 8
[,1]
[1,] 0.1479046
[2,] 0.2335122
, , 9
[,1]
[1,] 0.3327704
[2,] 0.4444645
, , 10
[,1]
[1,] 0.5676864
[2,] 0.7017218
, , 11
[,1]
[1,] 0.8459886
[2,] 1.0000000
> rand <- t(rgumbel(200,1))
> plot(rand[1,], rand[2,], col="green", main="Gumbel copula")
> rand <- t(rgumbel(200,2))
> plot(rand[1,], rand[2,], col="red", main="Gumbel copula")
> nbsimu <- 10000
> system.time(rgumbel(nbsimu, 2, dim=2, method=1))[3]
elapsed
0.01
> system.time(rgumbel(nbsimu, 2, dim=2, method=2))[3]
elapsed
0.61
> anim <-function(n, max=50)
+ {
+ for(i in seq(1,max,length.out=n))
+ {
+ u <- t(rgumbel(10000, i, method=2))
+ plot(u[1,], u[2,], col="green", main="Gumbel copula",
+ xlim=c(0,1), ylim=c(0,1), pch=".")
+ cat()
+ }
+ }
> anim(20, 20)
> x <- seq(.05, .95, length = 30)
>
> y <- x
> z <- outer(x, y, dgumbel, alpha=2)
> persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue",
+ ltheta = 100, shade = 0.25, ticktype = "detailed",
+ xlab = "x", ylab = "y", zlab = "density")
> zlim <- c(0, max(z))
> ncol <- 100
> nrz <- nrow(z)
> ncz <- ncol(z)
> couleurs <- tail(jet.colors(1.2*ncol),ncol)
Error in jet.colors(1.2 * ncol) : could not find function "jet.colors"
> couleurs <- tail(colors(1.2*ncol),ncol)
> fcol <- couleurs[trunc(z/zlim[2]*(ncol-1))+1]
> dim(fcol) <- c(nrz,ncz)
> fcol <- fcol[-nrz,-ncz]
> persp(x, y, z, col=fcol, zlim=zlim, theta=30, phi=30, ticktype = "detailed",
+ box = TRUE, xlab = "x", ylab = "y", zlab = "density")
> z <- outer(x, y, pgumbel, alpha=2)
> persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue",
+ ltheta = 100, shade = 0.25, ticktype = "detailed",
+ xlab = "u", ylab = "v", zlab = "cdf")
> simu <- qexp(rgumbel(200, 2))
> gumbel.MBE(simu[,1], simu[,2])
[1] 1.008945 1.011470 1.951363
> gumbel.IFM(simu[,1], simu[,2])
[1] 1.014007 1.016544 2.022765
> gumbel.EML(simu[,1], simu[,2])
[1] 1.025841 1.028801 2.011885
> gumbel.CML(simu[,1], simu[,2]
+ )
[1] 1.893303
> simu <- qgamma(rgumbel(200, 3), 2, 1)
> gumbel.MBE(simu[,1], simu[,2], "gamma")
[1] 0.9459023 1.8808981 1.0406970
[4] 2.0350758 3.0133253
> gumbel.IFM(simu[,1], simu[,2], "gamma")
[1] 0.9667415 1.9224548 1.0005313
[4] 1.9565467 2.9528046
> gumbel.EML(simu[,1], simu[,2], "gamma")
[1] 0.9618522 1.9176133 1.0059094
[4] 1.9628113 2.9535937
> gumbel.CML(simu[,1], simu[,2])
[1] 2.917404
0 Comments