Example of how to use bExtremes package
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | loadNamespace('devtools')
devtools::dev_mode(on=TRUE)
attempt <- try(loadNamespace('bExtremes'))
if(inherits(attempt, 'try-error')) {
devtools::install_github('cinnober/bExtremes')
}
library(bExtremes)
#cat('Getting data...\n')
#orgData <- fetchExampleStockData('BAC') #function unavailable
#Transforming and flipping data (because right tail is modelled)
#transformedData <- -transformStockData(orgData)
#cat('Filtering volatility...\n')
#transformedData <- filterVolatility(transformedData)
cat('Simulating data...\n')
transformedData <- simulateGHGP()
cat('Creating prior...\n')
#Expert prior (Guessing from historical data)
sortedData <- sort(transformedData)
##Median for VaR at 0.01
m <- sortedData[ceiling(length(sortedData)*(1-0.01))]
##90% quantile for VaR 0.01
q <- m*1.2
##Median for difference between VaR at 0.001 and 0.01
mDiff <- sortedData[ceiling(length(sortedData)*(1-0.001))]-m
##90% quantile of above
qDiff <- mDiff*1.2
prior <- createExpertPrior(m,q,mDiff,qDiff,transformedData)
#Uninformed prior (reference prior)
prior <- referencePrior
#Calculate VaR and ES for confidence level p (preferably 0.99 or greater):
p <- 0.99
cat('Setting up model...\n')
data <- createData(transformedData,p,dist='GP')
model <- createModel(data,prior)
cat('Generating IV...\n')
ivAndCov <- generateIV(model,data,n=500) #It can be helpful to increase n
cat('MCMC process...\n')
fit <- fitMCMC(model,data,ivAndCov)
#Information about final MCMC run and results
LaplacesDemon::Consort(fit)
#Summary of posterior samples
fit$Summary2
#Samples
head(fit$Posterior2) #parameters
head(fit$Monitor) #monitored variables (including VaR and ES)
#Example plots
hist(fit$Monitor[,7],breaks=100)
LaplacesDemon::joint.density.plot(fit$Monitor[,7],fit$Monitor[,8])
LaplacesDemon::joint.pr.plot(fit$Posterior2[,3],fit$Monitor[,7])
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.