Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

## -- PARETO

library("EnvStats")
x <- seq(from = 0, to = 5, length.out = 100)
alpha <- 2
# ? EnvStats::dpareto()
plot(x, dpareto(x, location = 1, shape = alpha),
type = 'l', ylab = "pdf")
n <- 100
xi <- rpareto(n, location = 1, shape = alpha)
alpha_hat <- n / sum(log(xi))
alpha_hat

## -- REPARAMETRIZATION NLL

library("qrmdata")
data("SP500", package = "qrmdata")
SP500 <- SP500["1999-12-31/2007-12-31"]
rets_SP500 <- 100 * diff(log(as.numeric(SP500)))

# Define negative log-likelihood function


f_nll_t <- function(theta, x) {
mu <- theta[1]
sig <- theta[2]
nu <- theta[3]
out <- -sum(dt((x - mu) / sig,
df = nu,
log = TRUE) - log(sig))
if (!is.finite(out)) {
out <- 1e10
}
out
}

theta0 <- c(mean(rets_SP500), sd(rets_SP500), 5)


theta0
tmp <- optim(par = theta0,
fn = f_nll_t,
x = rets_SP500)
print(tmp)
theta <- tmp$par

f_psi2theta <- function(psi) {


theta <- c(psi[1], exp(psi[2]), exp(psi[3]) + 0)
theta
}

f_theta2psi <- function(theta) {


psi <- c(theta[1], log(theta[2]), log(theta[3] - 0))
psi
}

f_nll_psi_t <- function(psi, x) {


theta <- f_psi2theta(psi)
out <- f_nll_t(theta, x)
out
}

tmp <- optim(par = f_theta2psi(theta0),


fn = f_nll_psi_t,
method = "Nelder-Mead",
x = rets_SP500)
f_psi2theta(tmp$par)

## -- FISHER FOR CONFIDENCE BANDS

tmp <- optim(par = theta0,


fn = f_nll_t,
method = "L-BFGS-B",
lower = c(-Inf, 1e-5, 1e-5),
x = rets_SP500,
hessian = TRUE)
print(tmp)
theta <- tmp$par
theta.se <- sqrt(diag(solve(tmp$hessian)))
theta.se
theta + qnorm(0.05) * theta.se
theta - qnorm(0.05) * theta.se

## -- STUDENT AND FREQUENCY

library("qrmdata")
library("quantmod")
data("SP500", package = "qrmdata")
SP500 <- SP500["1999-12-31/2007-12-31"]

# quantmod : NB first output is zero


# quantmod::dailyReturn()
rets_d <- 100 * dailyReturn(SP500)
rets_w <- 100 * weeklyReturn(SP500)
rets_m <- 100 * monthlyReturn(SP500)

# Define negative log-likelihood function


f_nll_t <- function(theta, x) {
mu <- theta[1]
sig <- theta[2]
nu <- theta[3]
out <- -sum(dt((x - mu) / sig,
df = nu,
log = TRUE) - log(sig))
if (!is.finite(out)) {
out <- 1e10
}
out
}

f_opt <- function(x) {


optim(par = c(mean(x), sd(x), 5),
fn = f_nll_t,
method = "L-BFGS-B",
lower = c(-Inf, 1e-5, 1e-5),
x = x)$par
}

f_opt(rets_d)
f_opt(rets_w)
f_opt(rets_m)

## -- SKEWED STUDENT

library("fGarch")
library("qrmdata")
library("quantmod")
data("SP500", package = "qrmdata")
SP500 <- SP500["1999-12-31/2007-12-31"]
rets <- 100 * dailyReturn(SP500)

f_nll_sstd <- function(theta, x) {


mu <- theta[1]
sig <- theta[2]
nu <- theta[3]
xi <- theta[4]
out <- -sum(dsstd(x, mean = mu, sd = sig, nu = nu, xi = xi, log = TRUE))
#out <- -sum(dt(x - mu))
if (!is.finite(out)) {
out <- 1e10
}
out
}

# Fit Student-t parameters


theta0 <- c(mean(rets), sd(rets), 5, 1.5)
tmp <- optim(par = theta0,
fn = f_nll_sstd,
method = "L-BFGS-B",
lower = c(-Inf, 1e-5, 1e-5, 0.1),
x = rets)
print(tmp)
theta_hat <- tmp$par

x <- seq(from = -10, to = 10, length.out = 1000)


hist(rets, nclass = 10 * round(log(length(rets))), probability = TRUE)
lines(x, dsstd(x, mean = theta_hat[1], sd = theta_hat[2],
nu = theta_hat[3], xi = theta_hat[4]), type = 'l')
lines(x, dnorm(x, mean = mean(rets), sd = sd(rets)), type = 'l', col = "blue")
rug(rets)

You might also like