This is my first post here so I am not quite sure how to frame a question here but I will try my best
I am trying to forecast densities of daily exchange rates, I have chosen EUR/USD as my currency pair that I'd like to forecast. I am using GARCH models to do the forecast. I have done my coding using "rugarch" package. The code looks like this
> ex1 <- as.xts(DEXUSEU) #DEXUSEU is the daily data of exchange rates
> ex2 <- ex1[!is.na(ex1)]
> lex2 <- 100*log((ex2[2:Nd,])/(ex2[1:(Nd-1),])) #taking log differences, it
has 4496 observations
> model1=ugarchspec (variance.model = list(model = "sGARCH", garchOrder = c(1,
1), submodel = NULL, external.regressors = NULL, variance.targeting =
FALSE),mean.model = list(armaOrder = c(0, 0), include.mean = TRUE,
archm = FALSE, archpow = 1, arfima = FALSE, external.regressors = NULL,
archex = FALSE),distribution.model = "std") #specifying garch model with
student t distribution
> modelfit1=ugarchfit(model,data=lex2,out.sample = 2000) #fitting model
> modelroll1=ugarchroll (
model1, data=lex2, n.ahead = 1, forecast.length = 2000,
n.start = NULL, refit.every = 50, refit.window = c("rolling"),
window.size = NULL, solver = "hybrid", fit.control = list(),
solver.control = list(), calculate.VaR = TRUE, VaR.alpha = 0.01,
cluster = NULL, keep.coef = TRUE) #doing rolling window forecast
> plot(modelroll1,which=1)
Thats how the density forecast looks like, and i am quite sure that something is wrong here, it shouldn't look like this:
Can anybody please help and tell me what I did wrong. I can provide additional data/information if needed. I am just not sure what else to provide as this is my first post here. Any kind of help would be very much appreciated.
Related
I have a code for a neural network model which uses keras.
features <- layer_input ( shape=c(ncol(feature_matrix)))
net <- features %>%
layer_dense(units=q,activation='tanh') %>%
layer_dense(units=1,activation=k_exp)
volumes <- layer_input(shape=c(1))
offset <- volumes %>%
layer_dense(units=1,activation='linear',use_bias=FALSE,trainable=FALSE,
weights=list(array(1,dim=c(1,1))))
merged <- list(net,offset) %>%
layer_multiply()
model <- keras_model(inputs=list(features,volumes),outputs=merged)
model %>% compile(loss='mse',optimizer='rmsprop')
fit <- model %>% fit(list(feature_matrix,offset_matrix),response_matrix,epochs=100,
batch_size=10000,validation_split=0.1)
However, I cannot find a way to display the network architecture with keras. I want to redefine my neural network using the neuralnet package instead.
I just encountered neuralnet and am clueless on where I should insert the custom bias/offset that I have.
Its usage is given by
neuralnet(formula, data, hidden = 1, threshold = 0.01,
stepmax = 1e+05, rep = 1, startweights = NULL,
learningrate.limit = NULL, learningrate.factor = list(minus = 0.5,
plus = 1.2), learningrate = NULL, lifesign = "none",
lifesign.step = 1000, algorithm = "rprop+", err.fct = "sse",
act.fct = "logistic", linear.output = TRUE, exclude = NULL,
constant.weights = NULL, likelihood = FALSE)
How do I do that?
I use the R package fPortfolio for portfolio optimization for a rolling portfolio (adaptive asset allocation). Therefore, I use the backtesting function.
I aim at constructing a portfolio for a set of assets for a predefined target return (and minimized risk) or for a predefined target risk and maximized returns.
Even allowing for short selling (as proposed in another post 5 years ago) seems not to work. Besides, I do not want to allow for short selling in my approach.
I cannot figure out why changing values for target return or target risk do not influence the solution at all.
Where do I go wrong?
require(quantmod)
require(fPortfolio)
require(PortfolioAnalytics)
tickers= c("SPY","TLT","GLD","VEIEX","QQQ","SHY")
getSymbols(tickers)
data.raw = as.timeSeries(na.omit(cbind(Ad(SPY),Ad(TLT),Ad(GLD),Ad(VEIEX),Ad(QQQ),Ad(SHY))))
data.arith = na.omit(Return.calculate(data.raw, method="simple"))
colnames(data.arith) = c("SPY","TLT","GLD","VEIEX","QQQ","SHY")
cvarSpec <- portfolioSpec(
model = list(
type = "CVAR",
optimize = "maxReturn",
estimator = "covEstimator",
tailRisk = list(),
params = list(alpha = 0.05, a = 1)),
portfolio = list(
weights = NULL,
targetReturn = NULL,
targetRisk = 0.08,
riskFreeRate = 0,
nFrontierPoints = 50,
status = 0),
optim = list(
solver = "solveRglpk.CVAR",
objective = NULL,
params = list(),
control = list(),
trace = FALSE))
backtest = portfolioBacktest()
setWindowsHorizon(backtest) = "12m"
assets <- SPY ~ SPY + TLT + GLD + VEIEX + QQQ + SHY
portConstraints ="LongOnly"
myPortfolio = portfolioBacktesting(
formula = assets,
data = data.arith,
spec = cvarSpec,
constraints = portConstraints,
backtest = backtest,
trace = TRUE)
setSmootherLambda(myPortfolio$backtest) <- "1m"
myPortfolioSmooth <- portfolioSmoothing(myPortfolio)
backtestPlot(myPortfolioSmooth, cex = 0.6, font = 1, family = "mono")
Recently, I am learning the LightGBM package and want to tune the parameters of it.
I want to try all the parameters which can be tuned in the LightGBM.
One question is: when I build the model using the function: lightgbm(data, label = NULL, weight = NULL, params = list(), nrounds = 10, verbose = 1), can I put the weight and nrounds and many other parameters into a list object and feed to the params argument?
The following code is what I used:
# input data for lgb.Dataset()
data_lgb <- lgb.Dataset(
data = X_tr,
label = y_tr
)
# can I put all parameters to be tuned into this list?
params_list <- list(weight = NULL, nrounds = 20, verbose = 1, learning_rate = 0.1)
# build lightgbm model using only: data_lgb and params_list
lgb_model <- lightgbm(data_lgb, params = params_list)
Can I do this using the above code?
I ask because I have a large training data set (2 million rows and 700 features). If I put the lgb.Dataset() into the lightgbm such as lightgbm(data = lgb.Dataset(data = X_tr,label = y_tr), params = params_list), then It takes time for multiple model building. Therefore, I first get the dataset used for lightgbm and for each model, the dataset is constant, what I did can only focus on the different parameters.
However, I am not sure, in total, how many parameters can be put into the params_list? Such as can the weight parameter be in the params_list? When I look the help ?lightgbm, I notice that the weight parameter and many other parameters are out side of the params_list.
Can you help me figure out: in total which parameters can be put into the params_list? That is the final model is built only using the data argument and params argument (other parameters are put into the params list object) as shown above, is that feasible?
Thank you.
Lightgbm has many params which you can tune. Please read the documentation.
I am pasting some part from one of my model script which shows the process. Should be a good hint for you.
nthread <- as.integer(future::availableCores())
seed <- 1000
EARLY_STOPPING <- 50
nrounds <- 1000
param <- list(objective = "regression"
metric = "rmse",
max_depth = 3,
num_leaves = 5,
learning_rate = 0.1,
nthread = nthread,
bagging_fraction = 0.7,
feature_fraction = 0.7,
bagging_freq = 5,
bagging_seed = seed,
verbosity = -1,
min_data_in_leaf = 5)
dtrain <- lgb.Dataset(data = as.matrix(train_X),
label = train_y)
dval <- lgb.Dataset(data = as.matrix(val_X),
label = val_y)
valids <- list(val = dval)
bst <- lgb.train(param,
data = dtrain,
nrounds = nrounds,
data_random_seed = seed,
early_stopping_rounds = EARLY_STOPPING,
valids = valids)
I have some issues implementing the DCC-GARCH in R. When I run the following code in R, I always get the same error message that says:
Error in UseMethod("convergence") :
no applicable method for 'convergence' applied to an object of class "try-error"
Unfortunately I have no idea how to fix this...
install.packages("fGarch")
install.packages("rugarch")
install.packages("rmgarch")
library(fGarch)
library(rmgarch)
library(rugarch)
library(tseries)
library(zoo)
#Daten runterladen
ibm <- get.hist.quote(instrument = "DB", start = "2005-11-21",
quote = "AdjClose")
sys<- get.hist.quote(instrument = "^STOXX50E", start = "2005-11-21",
quote = "AdjClose")
#Returns
retibm<-diff(log(ibm))
retsys<-diff(log(sys))
# univariate normal GARCH(1,1) for each series
garch11.spec = ugarchspec(mean.model = list(armaOrder = c(0,0)),
variance.model = list(garchOrder = c(1,1),
model = "sGARCH"),
distribution.model = "norm")
# dcc specification - GARCH(1,1) for conditional correlations
dcc.garch11.spec = dccspec(uspec = multispec( replicate(2, garch11.spec) ),
dccOrder = c(1,1),
distribution = "mvnorm")
dcc.garch11.spec
MSFT.GSPC.ret = merge(retsys,retibm)
plot(MSFT.GSPC.ret)
dcc.fit = dccfit(dcc.garch11.spec, data = MSFT.GSPC.ret)
I wasn't sure if this subforum was the right one, but it seemed more appropiate than the quantitative finance forum. If it is the wrong one, I apologize.
The problem is caused by a somewhat nonstandard behaviour of merge. When merging by column names, we have all = FALSE by default. However, when merging by row names, as in this case, it seems that we have all = TRUE and, hence, MSFT.GSPC.ret contains NA values.
So, using either
MSFT.GSPC.ret <- merge(retsys, retibm, all = FALSE)
or
dcc.fit <- dccfit(dcc.garch11.spec, data = na.omit(MSFT.GSPC.ret))
solves the problem.
I am looking at the ugarchboot function in rugarch but I am having trouble getting the Series (summary) into a dataframe.
library(rugarch)
data(dji30ret)
spec = ugarchspec(variance.model=list(model="gjrGARCH", garchOrder=c(1,1)),
mean.model=list(armaOrder=c(1,1), arfima=FALSE, include.mean=TRUE,
archm = FALSE, archpow = 1), distribution.model="std")
ctrl = list(tol = 1e-7, delta = 1e-9)
fit = ugarchfit(data=dji30ret[, "BA", drop = FALSE], out.sample = 0,
spec = spec, solver = "solnp", solver.control = ctrl,
fit.control = list(scale = 1))
bootpred = ugarchboot(fit, method = "Partial", n.ahead = 120, n.bootpred = 2000)
bootpred
as.data.frame(bootpred, which = "sigma", type = "q", qtile = c(0.01, 0.05))
##I am tring to get this into a dataframe:
Series (summary):
min q.25 mean q.75 max forecast
t+1 -0.24531 -0.016272 0.000143 0.018591 0.16263 0.000743
t+2 -0.24608 -0.018006 -0.000290 0.017816 0.16160 0.000232
t+3 -0.24333 -0.017131 0.001007 0.017884 0.31861 0.000413
t+4 -0.26126 -0.018643 -0.000618 0.017320 0.34078 0.000349
t+5 -0.19406 -0.018545 -0.000453 0.016690 0.33356 0.000372
t+6 -0.23864 -0.017268 -0.000113 0.016001 0.18233 0.000364
t+7 -0.27024 -0.018031 -0.000514 0.017852 0.18436 0.000367
t+8 -0.13926 -0.016676 0.000539 0.017904 0.16271 0.000366
t+9 -0.32941 -0.017221 -0.000194 0.016718 0.13894 0.000366
t+10 -0.19013 -0.015845 0.001095 0.017064 0.14498 0.000366
Thank you for your help.