Search code examples
rreturntime-series

convert log returns to actual price of a time series forecast using R


I have this simulated data and i fitted the ARMA-GARCH model using rugarch package. My code so far as follows,

    ar.sim<-arima.sim(model=list(ar=c(.9,-.2),ma=c(-.7,.1)),n=100)
    logr=diff(log(na.omit(ar.sim)))
require(rugarch)
    gar<-ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(2, 1)), 
                        mean.model = list(armaOrder = c(2, 1)), 
                        distribution.model = "norm");
    fitg=ugarchfit(spec = gar,data = ar.sim,solver = "hybrid");
    ugarchforecast(fitg,n.ahead =10) 

In this model , i used log returns . So my forecast also based on log returns. But i need the actual price. I googled to find any R function that convert this log return to actual price. But i couldn't find any.

Is there any function in R to extract the actual price from this log return of do i need to do that manually ?


Solution

  • The price should be [initial price] * exp(cumulative log returns). For example:

    df <- data.frame(price = c(90, 108, 81, 105, 180))
    
    df$log = log(df$price)
    df$logr = c(NA, diff(df$log))
    df$logr_na0 = ifelse(is.na(df$logr), 0, df$logr)
    df$cuml_log= cumsum(df$logr_na0)
    df$reconstructed_price_norm = exp(df$cuml_log)
    
    initial_price <- 90
    df$reconstructed_price = initial_price * df$reconstructed_price_norm
    

    output

    > df
      price      log       logr   logr_na0   cuml_log reconstructed_price_norm reconstructed_price
    1    90 4.499810         NA  0.0000000  0.0000000                 1.000000                  90
    2   108 4.682131  0.1823216  0.1823216  0.1823216                 1.200000                 108
    3    81 4.394449 -0.2876821 -0.2876821 -0.1053605                 0.900000                  81
    4   105 4.653960  0.2595112  0.2595112  0.1541507                 1.166667                 105
    5   180 5.192957  0.5389965  0.5389965  0.6931472                 2.000000                 180