Showing posts with label the daily returns are converted to positive loss figures expressed. Show all posts
Showing posts with label the daily returns are converted to positive loss figures expressed. Show all posts

## Friday, April 19, 2019

### The daily returns are converted to positive loss figures expressed

library(ismev)
SieGEV2 <− gev.fit ( SieGEV\$data )
data(dowjones)
> data(engine)
> data(euroex)
> data(exchange)
> data(fremantle)
> years <- 2003:2012
> nyears <- length(years)

> n <- 250 # sample size for each (different) xi
> u <- 200
> # threshold
> rGPD <- function(n, xi, beta) ((1-runif(n))^(-xi)-1)*beta/xi # sampling GPD
> set.seed(17)
> xi.true.A <- seq(0.4, 0.8, length=nyears) # true xi for group "A"
> lossA <- unlist(lapply(1:nyears,
+                        function(y) u + rGPD(n, xi=xi.true.A[y], beta=1)))
> xi.true.B <- xi.true.A^2 # true xi for group "B"
> lossB <- unlist(lapply(1:nyears,
+                        function(y) u + rGPD(n, xi=xi.true.B[y], beta=1)))
> time <- rep(rep(years, each=n), 2) # "2" stands for the two groups
> covar <- rep(c("A","B"), each=n*nyears)
> value <- c(lossA, lossB)
> x <- data.frame(covar=covar, time=time, value=value)
> eps <- 1e-3 # to decrease the run time for this example
> fit <- gamGPDfit(x, threshold=u, datvar="value", xiFrhs=~covar+s(time)-1,
+                  nuFrhs=~covar+s(time)-1, epsxi=eps, epsnu=eps)
Mean relative differences in iteration 1 (xi, nu): (0.122666, 0.267846)
Mean relative differences in iteration 2 (xi, nu): (0.0841554, 0.00188241)
Mean relative differences in iteration 3 (xi, nu): (0.0458509, 0.00192544)
Mean relative differences in iteration 4 (xi, nu): (0.0237893, 0.000632246)
Mean relative differences in iteration 5 (xi, nu): (0.0120535, 0.0002424)
Mean relative differences in iteration 6 (xi, nu): (0.00610084, 0.000103885)
Mean relative differences in iteration 7 (xi, nu): (0.00308787, 4.82454e-05)
Mean relative differences in iteration 8 (xi, nu): (0.00156383, 2.34081e-05)
Mean relative differences in iteration 9 (xi, nu): (0.00079636, 1.16357e-05)
> xi.fit <- fitted(fit\$xiObj)
> xi.fit. <- xi.fit[1+(0:(2*nyears-1))*n]
> xi.fit.A <- xi.fit.[1:nyears]
> xi.fit.B <- xi.fit.[(nyears+1):(2*nyears)]
> par(mfrow=c(1,2))
> plot(years, xi.true.A, type="l", ylim=range(xi.true.A, xi.fit.A),
+      main="Group A", xlab="Year", ylab=expression(xi))
> points(years, xi.fit.A, type="l", col="red")
> legend("topleft", inset=0.04, lty=1, col=c("black", "red"),
+        legend=c("true", "fitted"), bty="n")
> plot(years, xi.true.B, type="l", ylim=range(xi.true.B, xi.fit.B),
+      main="Group B", xlab="Year", ylab=expression(xi))
> points(years, xi.fit.B, type="l", col="blue")
> legend("topleft", inset=0.04, lty=1, col=c("black", "blue"),
+        legend=c("true", "fitted"), bty="n")
First, the daily returns are converted to positive loss figures expressed as percentages.
gev.diag ( SieGEV2 )
> par( mfrow = c(2 , 1) )
> gev.prof ( SieGEV2 , m = 20 , xlow = 5 , xup = 16 , conf = 0.95 )
If routine fails, try changing plotting interval
> gev.profxi ( SieGEV2 , xlow = 0.0 , xup = 0.7 , conf = 0.95 )
If routine fails, try changing plotting interval

> mLoss <− max ( SieGEV\$data )
> mYears <− 1/(1 − pgev ( mLoss , mu = SieGEV2\$mle[ 1 ] ,
+                           sigma = SieGEV2\$mle[ 2 ] ,
+             xi = SieGEV2\$mle[ 3 ] ) )/2
> library ( fExtremes )

Attaching package: ‘fExtremes’

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

dgev, dgpd, pgev, pgpd, qgev,
qgpd, rgev, rgpd

Warning messages:
1: package ‘fExtremes’ was built under R version 3.5.3
2: package ‘fGarch’ was built under R version 3.5.2
> SieGEV3 <− gevFit ( SieGEV\$data , type = "pwm" )
> SieGEV3

Title:
GEV Parameter Estimation

Call:
gevFit(x = SieGEV\$data, type = "pwm")

Estimation Type:
gev pwm

Estimated Parameters:
xi        mu      beta
0.2242576 1.4425233 0.7418871

Description

Fri Apr 19 17:11:21 2019
library ( fBasics )
> library ( fExtremes )
> data ( DowJones30 )
> DJ <− timeSe ries ( DowJones30 [ , −1],
Error: unexpected symbol in "DJ <- timeSe ries"
>                     charvec = as.cha racter ( DowJones30 [ , 1]) )
Error: unexpected symbol in "                    charvec = as.cha racter"
> DJ <− timeSeries ( DowJones30 [ , −1],
+                     charvec = as.cha racter ( DowJones30 [ , 1]) )
Error: unexpected symbol in:
"DJ <- timeSeries ( DowJones30 [ , -1],
charvec = as.cha racter"
> DJ <− timeSeries ( DowJones30 [ , −1],                    charvec = as.character( DowJones30[ , 1]) )
> BALoss <− −1.0 ∗ returns( DJ[ , "BA" ] , percentage = TRUE, trim = TRUE )
> mrlPlot ( BALoss , umin = −10, umax = 10)
> BAFit <− gpdFit( BALoss , u = 3)
> par ( mfrow = c (2 , 2) )
> plot (BAFit , which = 1)
> plot (BAFit , which = 2)
> plot (BAFit , which = 3)
> plot (BAFit , which = 4)
> gpdRiskMeasures ( BAFit , prob = c ( 0.95 , 0.99 , 0. 995) )
Error: unexpected numeric constant in "gpdRiskMeasures ( BAFit , prob = c ( 0.95 , 0.99 , 0. 995"
> gpdRiskMeasures ( BAFit , prob = c ( 0.95 , 0.99 , 0.995) )
p quantile shortfall
1 0.950 2.783288  4.240079
2 0.990 4.855221  7.335843
3 0.995 6.148591  9.268322
> library ( fExtremes )
> library ( fBasics )
> data ( nyse )
> NYSELevel <− timeSeries ( nyse [ , 2] ,
+                           charvec = as..character ( nyse [ , 1]) )
Error in as..character(nyse[, 1]) :
could not find function "as..character"
> NYSELevel <− timeSeries ( nyse [ , 2] ,                           charvec = as.character ( nyse [ , 1]) )
> NYSELoss <− na.omit ( −1.0 ∗ diff ( log ( NYSELevel ) ) ∗ 100)
> colnames ( NYSELoss ) <− "NYSELoss"
> NYSEPP <− point Process ( x = NYSELoss ,
Error: unexpected symbol in "NYSEPP <- point Process"
>             u = quantile ( NYSELoss , 0. 95 ) )
Error: unexpected numeric constant in "            u = quantile ( NYSELoss , 0. 95"
> NYSEPP <− pointProcess ( x = NYSELoss ,
+         u = quantile ( NYSELoss , 0.95 ) )
> DC05 <− deCluster ( x = NYSEPP , run = 5 , doplot = FALSE )
> DC10 <− deCluster ( x = NYSEPP , run =10 , doplot = FALSE )
> DC20 <− deCluster ( x = NYSEPP , run =20 , doplot = FALSE )
> DC40 <− deCluster ( x = NYSEPP , run =40 , doplot = FALSE )
> DC60 <− deCluster ( x = NYSEPP , run =60 , doplot = FALSE )
> DC120 <− deCluster ( x = NYSEPP , run =120 , doplot = FALSE )
> DC05Fit <− gpdFit ( DC05 , u = min ( DC05 ) )
> DC10Fit <− gpdFit ( DC10 , u = min ( DC10 ) )
> DC20Fit <− gpdFit ( DC20 , u = min ( DC20 ) )
> DC40Fit <− gpdFit ( DC40 , u = min ( DC40 ) )
> DC60Fit <− gpdFit ( DC60 , u = min ( DC60 ) )
> DC120Fit <− gpdFit ( DC120 , u = min ( DC40 ) )