1 Purpose

The purpose is to use R to explore the dynamics of a volatility control index and obtain model prices. The volatility control index includes a mechanism that rebalances portfolio exposure to a risky asset based on the historical volatility of the risky asset. In this analysis the horizon is one year. Over this horizon the risky asset is projected, portfolio rebalancing occurs, and performance is measured.

This document is presented as follows. Section 2 explains how historical data is collected, a valuation date is established, and a daily calendar of trading days is developed. In section 3 the model parameters are shown and the number of simulations and projection periods are defined. Section 4 includes the implementation of the Two-State Regime Switching Log-normal model including underlying asset prices, application of the volatility control mechanics, and finally the model prices of call options on the volatility control index- one at-the-money and another at 2.25% above the spot level. Similarly section 5 provides the same information but using the Heston-Nandi GARCH (1,1) model for projecting the underlying asset prices. Both models in sections 4 and 5 allow for stochastic volatility which is important due to the volatility path dependence. Section 6 is an Appendix that shows how the models perform in replicating Black-Scholes for a simple European call option. The Appendix also provides the code for the functions used in the analysis.

This calculation is fairly demanding given the need for daily projections and rolling calculations of volatility. The daily projections alone create a need for a large number of simulations to achieve convergence. This can be observed in the Appendix where the daily time-step model is compared against the analytical solution of Black-Scholes.

Finally, R is chosen over Excel for performance and the wealth of packages available for R. For example, this document is written using the ‘knitr’ package which allows creation of reproducible research that blends commentary with R code, execution, and output. R also allows for easy access to superior random number generation methodologies such as Mersenne-Twister.

2 Historical Data

Obtain S&P 500 index data to serve as the risky asset for this example. Calculate the 20 and 40 day rolling historical volatility and find the maximum. Obtain the one-year swap rate history too. The one-year swap rates will be used as the risk-free rate. The code below stores S&P 500 data into a global variable called “SP500” and one-year swap rate data into a global variable called “DSWP1.” The source of historical data is the Federal Reserve Bank of Saint Louis.

getFred("SP500", freq="d")
getFred("DSWP1", freq="d")
vhp1 <- 20
vhp2 <- 40
v1 <- runSD(ROC(SP500), n=vhp1)
v2 <- runSD(ROC(SP500), n=vhp2)
v <- na.omit(merge(v1, v2, all=FALSE))
maxv <-xts(apply(v, 1, max), index(v))
v<- merge(v, maxv, all=FALSE)
colnames(v) <- c("Days_20", "Days_40", "Max")

2.1 Plot Historical Volatility

The importance of historical volatility is that it defines how much the portfolio is exposed to the risky asset versus cash. This analysis examines both the 20 and 40 day historical volatility to find the maximum. Eventually an allocation to the risky asset will be determined by taking a target volatility and dividing by this maximum.

The minimum voloatility by this measure was on 2014-07-14 with a daily volatility of 0.004118.

minVolDate <- index(v)[match(min(v$Max), v$Max)]

2.2 Valuation Date

Define a valuation date and use the one-year swap rate as of that date as the risk-free rate. Also use the index price/level as of this date as the starting value of the simulations.

The valuation date selected is August 31, 2015. The risk-free rate is stored as “r” and the S&P 500 level as of the valuation date is stored as “S.” Finally, a subset of the available S&P 500 data is stored as “SP500.” This subset cuts off the historical data as of the valuation date so that any subsequent periods will be replaced with simulated values.

valdate <- as.Date("2015-08-31")
r <- as.numeric(subset(data.frame(date=index(DSWP1), coredata(DSWP1)), date==valdate)[2])/100
S <- as.numeric(subset(data.frame(date=index(SP500), coredata(SP500)), date==valdate)[2])
SP500 <- SP500[paste0("::", as.character(valdate))] #only want history through the valuation date

A daily calendar over the projection horizon is created below. A full year is desired but it is important to remove weekends and NYSE holidays. Keep in mind that 2016 is a leap-year. Notice that the first day is the day following the valuation date and the last day is the business day one year later.

calendar<-timeSequence(from=valdate+1, by="day", length.out=366) #get a full calendar year
calendar<-calendar[isBizday(calendar, holidayNYSE())] #remove weekends and NYSE holidays
head(calendar,5) #show the first 5 days
## GMT
## [1] [2015-09-01] [2015-09-02] [2015-09-03] [2015-09-04] [2015-09-08]
tail(calendar,5) #show the last 5 days
## GMT
## [1] [2016-08-25] [2016-08-26] [2016-08-29] [2016-08-30] [2016-08-31]
nTradingDays <- length(calendar) #count the number of trading days in the one-year horizon

The number of trading days in the projection horizon is 259.

The daily calendar is important because it is convenient to organize projections as multivariate time-series where future values are associated with specific dates. It helps to ensure the projections covers a full year.

3 Simulation Parameters

3.1 RSLN

The first model examined is a Two-State Regime Switching Log-normal (RSLN) model. This model allows for stochastic volatility in the form of transitions between lower and higher volatility regimes or states.

The asset dynamics under the Regime Switching Log-normal model are given by:

\(Ln\frac{{S_t}}{S_{t-{\Delta_t}}} = ({r_i} -\frac{1}{2}{\sigma_i}^2){\Delta_t}+\sigma_i\sqrt{\Delta_t}{z_t}\)

where \({r_i}\) and \({\sigma_i}\) represent returns and volatilities for \(i \in \left\{{1,2}\right\}\) where 1 and 2 represent the two states.

The returns in each state are set equal to the risk-free rate. The weighted-average volatility of the states is set equal to the one-year rolling S&P implied volatility with spot moneyness of 100%. The value on 8/31 was 19.767%. The probability of transitioning from state 1 to state 2 is set to 25% and the probability of transitioning from state 2 to state 1 is set to 75%. These parameters can be explored in detail later.

To set the volatility in state 1, the historical volatility over 252 day windows is examined.

##       date               Days_252      
##  Min.   :2005-09-09   Min.   :0.00605  
##  1st Qu.:2008-03-10   1st Qu.:0.00733  
##  Median :2010-09-03   Median :0.01051  
##  Mean   :2010-09-04   Mean   :0.01191  
##  3rd Qu.:2013-03-05   3rd Qu.:0.01371  
##  Max.   :2015-08-31   Max.   :0.02873  
##                       NA's   :252

To establish the volatility associated with state 1, the 75th percentile is examined. The mean volatility below that level is calculated and recognized as the volatility for state 1.

q<- quantile(df$Days_252, c(.75), na.rm=TRUE)
sub <- subset(na.omit(df$Days_252), na.omit(df$Days_252)<=as.numeric(q))
mean(sub) #daily
## [1] 0.008956907
mean(sub)*sqrt(252) #annualized
## [1] 0.1421865

Now set all RSLN parameters

implied_vol <- .19767 #implied one-year volatility as of valuation date
mu1 <- r #one-year swap rate
mu2 <- r #one-year swap rate
p12 <- .25 #probability of transitioning from state 1 to state 2
p21 <- .75 #probability of transitioning from state 2 to state 1
pi1<- p21/(p12+p21) #probability of being in state 1 (function of prior input)
pi2 <- p12/(p12+p21) #probability of being in state 2 (function of prior input)
vol1 <- mean(sub)*sqrt(252) #mean of the 252 day rolling volatility for those values below the target percentile
vol2 <- (implied_vol - pi1*vol1)/pi2 #solve so that weighted-average vol = implied-vol
vol2
## [1] 0.3641205
#Perform a quick check to see that weighted average is equal to implied_vol
vol1*pi1 + vol2*pi2 #matches implied-vol
## [1] 0.19767

The volatility is 0.1421865 in state 1, 0.3641205 in state 2, and 0.19767 on average.

3.2 Heston-Nandi GARCH(1,1)

The next model examined is the Heston-Nandi GARCH (1,1) model. This model allows for stochastic volatility that is correlated with the returns of the asset.

The asset dynamics under the Heston-Nandi model are given by:

\(Ln\frac{{S_t}}{S_{t-{\Delta_t}}} = r + {\lambda}{h_t}+\sqrt{{h_t}}z{_t}\)

where

\(h{_t} = \omega + \beta {h_{t-{\Delta_t}}}+\alpha (z{_{t-{\Delta_t}}}-\gamma \sqrt{h{_{t-{\Delta_t}}}})^2\)

The ‘fOptions’ package is used to implement the Heston-Nandi GARCH(1,1) model. The ‘fOptions’ package includes functions for obtaining the maximum log-likelihood estimate parameters and for projecting the asset values. Ultimately the risky asset will be projected using the risk-neutral formulation described in the Heston-Nandi paper.

hnpr <- periodReturn(SP500, period="daily", type="log", leading=FALSE)[-1]
model = list(lambda = -1/2, omega = var(hnpr), alpha = var(hnpr)*0.1, beta = 0.1, gamma = 0, rf = log(1+r))
hngf <- hngarchFit(x=hnpr, model = model)
hngf
## 
## Title:
##  Heston-Nandi Garch Parameter Estimation 
## 
## Call:
##  hngarchFit(x = hnpr, model = model)
## 
## Parameters:
##     lambda       omega       alpha        beta       gamma          rf  
## -5.000e-01   1.693e-04   1.693e-05   1.000e-01   0.000e+00   5.286e-03  
## 
## Coefficients: lambda, omega, alpha, beta, gamma
##     lambda       omega       alpha        beta       gamma  
## -3.801e+01   9.884e-36   7.962e-06   9.389e-01   0.000e+00  
## 
## Log-Likelihood:
##  14158.33 
## 
## Persistence and Variance:
##  0.9388523 
##  0.0001302156 
## 
## Description:
##  Thu Sep 10 19:00:24 2015 by user: Jason

3.3 Simulation

Set up for n trading days and thousands of simulations. Set the incremental time period (dt) to 1/(number of trading days)

Nproj <- nTradingDays #this is the number of trading days in the daily calendar 
Nsim <- 50000
dt <- 1/Nproj

4 Simulate Two-State RSLN Values

4.1 Underlying Asset Values

In this section the prices of the risky-asset are simulated for each day beyond the valuation date and extending to the end of the projection horizon of one year. The code below uses two custom functions shown in the Appendix. The first line simulates the future prices and the second line combines projected values with historical values into a single multi-variate time-series.

pricesRSLN <- SimulateRSLNPrices(mu1, mu2, vol1, vol2, p12, p21, Nproj, dt, Nsim, calendar, S)
pricesRSLN <- combine.past.proj(SP500, pricesRSLN, Nsim)

4.2 Apply Volatility Control Mechanics

Now the volatility control mechanics are applied to the simulated prices. The code below uses a custom function shown in the Appendix. The data stored as “risk control final levels.” The values are the simulated index levels for the volatility control index taking into account the starting level, portfolio rebalancing, and the returns on the risky asset. Please note that this implementation assumes the non-risky asset portfolio of the portfolio earns the risk-free rate or the 1-year swap rate.

target_level <- .055
max_leverage <- 1
max_change <- .1
lag <- 2 #2 days
rcflRSLN <- ApplyVolControl(valdate, pricesRSLN, r, 100, Nsim, nTradingDays, vhp1, vhp2, target_level, max_leverage, lag, calendar, FALSE)

4.3 Option Payoff and Value

In order to get a call option price, the strike is deducted from the risk control final level (floored at zero), the average is taken across all simulations and resulting value is discounted back to the valuation date.

It is also useful to consider the option payoffs relative to their cost. An option cost of 2.18% is shown on the graphs below as a yellow dashed line. This option cost is used for both the at-the-money and in-the-money calculations.

4.3.1 At-the-money

K <- 100
payoffs <- as.vector(coredata(rcflRSLN)[nrow(coredata(rcflRSLN)),]) - K
payoffs <- ifelse(payoffs<0, 0, payoffs)
pv_payoffs <- payoffs*exp(-r)
mean(pv_payoffs)
## [1] 2.261755

The risk-neutral probability of a non-zero payoff is 0.49222.

Now the assumed hedge-cost is considered.

oc <- 100*.0218
pv_payoffs_lesscost <- pv_payoffs-oc
mean(pv_payoffs_lesscost)
## [1] 0.08175457

The risk-neutral probability of the present value of the payoff exceeding the hedge-cost is 0.34392.

4.3.2 Out-of-the-money

K <- 100*1.0225
payoffs <- as.vector(coredata(rcflRSLN)[nrow(coredata(rcflRSLN)),]) - K
payoffs <- ifelse(payoffs<0, 0, payoffs)
pv_payoffs <- payoffs*exp(-r)
mean(pv_payoffs)
## [1] 1.332616

The risk-neutral probability of a non-zero payoff is 0.33996.

Now the assumed hedge-cost is considered.

pv_payoffs_lesscost <- pv_payoffs-oc
mean(pv_payoffs_lesscost)
## [1] -0.8473841

The risk-neutral probability of the present value of the payoff exceeding the hedge-cost is 0.2139.

5 Simulate Heston-Nandi GARCH(1,1) Values

5.1 Underlying Values

pricesHNG <- SimulateHNGPrices(hngf$model, Nproj, Nsim, calendar, S)
pricesHNG <- combine.past.proj(SP500, pricesHNG, Nsim)

5.2 Apply Volatility Control Mechanics

rcflHNG <- ApplyVolControl(valdate, pricesHNG, r, 100, Nsim, nTradingDays, vhp1, vhp2, target_level, max_leverage, lag, calendar, TRUE)

5.3 Option Payoff and Value

5.3.1 At-the-money

K <- 100
payoffs <- as.vector(coredata(rcflHNG)[nrow(coredata(rcflHNG)),]) - K
payoffs <- ifelse(payoffs<0, 0, payoffs)
pv_payoffs <- payoffs*exp(-r)
mean(pv_payoffs)
## [1] 2.229852

The risk-neutral probability of a non-zero payoff is 0.49804.

Now the assumed hedge-cost is considered.

pv_payoffs_lesscost <- pv_payoffs-oc
mean(pv_payoffs_lesscost)
## [1] 0.0498521

The risk-neutral probability of the present value of the payoff exceeding the hedge-cost is 0.34172.

5.3.2 Out-of-the-money

K <- 100*1.0225
payoffs <- as.vector(coredata(rcflHNG)[nrow(coredata(rcflHNG)),]) - K
payoffs <- ifelse(payoffs<0, 0, payoffs)
pv_payoffs <- payoffs*exp(-r)
mean(pv_payoffs)
## [1] 1.297948

The risk-neutral probability of a non-zero payoff is 0.33824.

Now the assumed hedge-cost is considered.

pv_payoffs_lesscost <- pv_payoffs-oc
mean(pv_payoffs_lesscost)
## [1] -0.8820521

The risk-neutral probability of the present value of the payoff exceeding the hedge-cost is 0.21076.

6 Appendix

6.1 Model Check - RSLN

Perform a check of the model to gain comfort. In this simplified example a call option is priced using the simulations as well as using the analytical Black-Scholes formula. The convergence will then be demonstrated.

Focus on call option with strike K. In this case, set K = S.

mu1 <- r #one-year swap rate
mu2 <- r #one-year swap rate
p12 <- 0 #probability of transitioning from state 1 to state 2
p21 <- 1 #probability of transitioning from state 2 to state 1
vol1 <- implied_vol
vol2 <- implied_vol
prices<- SimulateRSLNPrices(mu1, mu2, vol1, vol2, p12, p21, Nproj, dt, Nsim, calendar, S)
K <- S
payoffs <- as.vector(coredata(prices)[nrow(coredata(prices)),]) - K
payoffs <- ifelse(payoffs<0, 0, payoffs)
pv_payoffs <- payoffs*exp(-r)
mean(pv_payoffs)
## [1] 159.6744
bsp <- BS(S, S, r, 0, implied_vol, 1)
bsp 
## [1] 160.1287
pv_payoff_converge <- cumsum(pv_payoffs)/index(pv_payoffs)
pv_payoff_converge.df <- data.frame(cbind(index(pv_payoffs),pv_payoff_converge))
pv_payoff_converge_pct.df <- data.frame(cbind(index(pv_payoffs),(pv_payoff_converge-bsp)/bsp))
colnames(pv_payoff_converge.df) <- c("Simulations", "AveragePresentValue")
colnames(pv_payoff_converge_pct.df) <- c("Simulations", "PctDifference")
pv_payoff_converge_pct.df <- subset(pv_payoff_converge_pct.df, Simulations>1000)

6.2 Model Check - Heston-Nandi GARCH(1,1)

Like Black-Scholes, the Heston-Nandi GARCH(1,1) model has analytical solutions for simple options. The call option examined above has a Heston-Nandi GARCH(1,1) price of 164.2191884. Below we see that the Heston-Nandi GARCH(1,1) simulations converge to the analytical solution and compare well with Black-Scholes.

payoffs <- as.vector(coredata(pricesHNG)[nrow(coredata(pricesHNG)),]) - K
payoffs <- ifelse(payoffs<0, 0, payoffs)
pv_payoffs <- payoffs*exp(-r)
mean(pv_payoffs)
## [1] 164.1752
hngo <- HNGOption(model=hngf$model, TypeFlag = "c", S=S, X=S, Time.inDays = Nproj, r.daily=log(1+r)/Nproj)
hngo
## 
## Call:
## HNGOption(TypeFlag = "c", model = hngf$model, S = S, X = S, Time.inDays = Nproj, 
##     r.daily = log(1 + r)/Nproj)
## 
## Option Price:
## 164.2192
pv_payoff_converge <- cumsum(pv_payoffs)/index(pv_payoffs)
pv_payoff_converge.df <- data.frame(cbind(index(pv_payoffs),pv_payoff_converge))
pv_payoff_converge_pct.df <- data.frame(cbind(index(pv_payoffs),(pv_payoff_converge-hngo$price)/hngo$price))
colnames(pv_payoff_converge.df) <- c("Simulations", "AveragePresentValue")
colnames(pv_payoff_converge_pct.df) <- c("Simulations", "PctDifference")
pv_payoff_converge_pct.df <- subset(pv_payoff_converge_pct.df, Simulations>1000)

6.3 Main Functions

SimulateRSLNPrices <- function(mu1, mu2, vol1, vol2, p12,p21, nproj, dt, nsim, calendar, startprice){
  pi1<- p21/(p12+p21)
  pi2 <- p12/(p12+p21)
  #Generate random numbers to simlute the current state
  stateRnd <- runif(nproj*nsim)
  #Based on state, pick the corresponding return and vol
  mu_sim<- matrix(ifelse(stateRnd< pi1,mu1,mu2), ncol=nproj, nrow=nsim)
  vol_sim<- matrix(ifelse(stateRnd< pi1,vol1,vol2), ncol=nproj, nrow=nsim)
  #Simulate the future prices
  U <- matrix(exp((mu_sim-vol_sim^2/2)*dt+vol_sim*sqrt(dt)*rnorm(nproj*nsim)), ncol=nproj, nrow=nsim)
  U <- t(apply(U, 1, cumprod))
  U <- startprice*U
  write.csv(U, "SimulateRSLNPrices.csv")
  tU <- t(U)
  tU.df <- data.frame(tU)
  tU.xts <- xts(tU.df,order.by=calendar)
  return(tU.xts)
}

hngarchSim.RN <- function (model = list(lambda = 4, omega = 4 * 2e-04, alpha = 0.3 * 
                         2e-04, beta = 0.3, gamma = 0, rf = 0), n = 1000, innov = NULL, 
          n.start = 100, start.innov = NULL, rand.gen = rnorm, ...) 
{
  if (is.null(innov)) 
    innov = rand.gen(n, ...)
  if (is.null(start.innov)) 
    start.innov = rand.gen(n.start, ...)
  lambda = model$lambda
  omega = model$omega
  alpha = model$alpha
  beta = model$beta
  gamma = model$gamma
  rfr = model$rf/n
  x = h = Z = Z.star = c(start.innov, innov)
  gamma.star <- gamma + lambda +.5
  lambda = -.5
  nt = n.start + n
  h[1] = (omega + alpha)/(1 - alpha * gamma * gamma - beta)
  x[1] = rfr + lambda * h[1] + sqrt(h[1]) * Z[1]
  #Z.star[1] = Z[1] + (lambda+.5)*sqrt(h[1])
    
  for (i in 2:nt) {
    h[i] = omega + alpha * (Z[i - 1] - gamma.star * sqrt(h[i - 1]))^2 + beta * h[i - 1]
    x[i] = rfr + lambda * h[i] + sqrt(h[i]) * Z[i]
  }
  x = x[-(1:n.start)]
  x
  print(mean(h))
  print(mean(Z.star))
  return(x)
}

SimulateHNGPrices <- function(model, nproj, nsim, calendar, startprice){
  U <- create.matrix(nsim, nproj)
  for(i in 1:nsim){
    pricesHNG <- hngarchSim.RN(model=model, n=nproj)
    U[i,]<- exp(pricesHNG)
  }
  
  U <- t(apply(U,1,cumprod))
  U <- startprice*U
  write.csv(U, "SimulateHNGPrices.csv")
  tU <- t(U)
  tU.df <- data.frame(tU)
  tU.xts <- xts(tU.df,order.by=calendar)
  return(tU.xts)
}

ApplyVolControl <- function(valdate, prices, riskfree, startvalue, nsims, ntrading, voldays1, voldays2, targetlevel, maxleverage, daylag, calendar, write.intermediate=FALSE){
  valdate_loc <- match(valdate, index(prices))
  start_price_hist_date <- index(prices)[valdate_loc-max(voldays1, voldays2)-daylag]
  enddate_loc <- match(max(index(prices)), index(prices))
  end_price_hist_date <- index(prices)[enddate_loc-daylag-1]
  prices_short <- prices[paste0(as.character(start_price_hist_date),"::",as.character(end_price_hist_date))]
  prices_shorter <- prices[paste0(as.character(valdate),"::")]
  
  max_vol <- create.matrix(ntrading, nsims)
  leverage <- create.matrix(ntrading, nsims)
  max_leverage <- create.matrix(ntrading, nsims)
  daily_index_returns <- create.matrix(ntrading, nsims)
  daily_risk_control_returns <- create.matrix(ntrading, nsims)
  final_levels <- create.matrix(ntrading, nsims)
  risk_control_final_levels <- create.matrix(ntrading, nsims)
  price_value <- create.matrix(1, nsims)
  
  for(j in 1:nsims){
    v_all_20 <- runSD(ROC(prices_short[,j]),n=voldays1, sample=FALSE)*sqrt(252)
    v_all_40 <- runSD(ROC(prices_short[,j]),n=voldays2, sample=FALSE)*sqrt(252)
    v_all <- na.omit(merge(v_all_20, v_all_40, all=FALSE))
    check <- nrow(v_all)
    
    max_vol[,j]<- apply(v_all, 1, max)
    leverage[,j]<- target_level/max_vol[,j]
    leverage[,j] <- matrix(sapply(leverage[,j], function(x) min(x, maxleverage)), nrow=ntrading, ncol=1)
    pr <- periodReturn(prices_shorter[,j], period="daily", type="log", leading=FALSE)
    tmp <- na.omit(as.matrix(coredata(pr)))
    daily_index_returns[,j]<- tmp[,1]
    
    for(i in 1:ntrading){
      if(i ==1){
        max_leverage[i,j] <- leverage[i,j]
      }else{
        if(max_leverage[i-1, j]-leverage[i,j] >max_change){
          max_leverage[i,j] <- max_leverage[i-1, j] - max_change
        }else{
          if(leverage[i,j]-max_leverage[i-1, j]>max_change){
            max_leverage[i,j] <- max_leverage[i-1,j] + max_change
          }else{
            max_leverage[i,j]<-leverage[i,j]
          }
        }
      }  
    }
    #print(j)
  }
  # Assumes that non-risky portion earns the swap rate
  daily_risk_control_returns <- max_leverage*daily_index_returns + (1-max_leverage)*riskfree/ntrading
  risk_control_final_levels <- exp(apply(daily_risk_control_returns, 2, cumsum))*startvalue
  result <- mean(risk_control_final_levels[nrow(risk_control_final_levels),])*exp(-r)
  
  if(write.intermediate){
    print("Writing output")
    write.zoo.transpose(prices, "prices.csv")
    write.zoo.transpose(prices_short, "prices_short.csv")
    write.csv(t(max_vol), "max_vol.csv")
    write.csv(t(leverage), "leverage.csv")
    write.csv(t(max_leverage), "max_leverage.csv")
    write.csv(t(daily_risk_control_returns), "daily_risk_control_returns.csv")
    write.csv(t(risk_control_final_levels), "risk_control_final_levels.csv")
    write.csv(result, "pv.csv")
  }
  
  risk_control_final_levels <- xts(risk_control_final_levels, order.by = calendar)
  return(risk_control_final_levels)  
}

6.4 Helper Functions

getFred<-function(id, freq){
  json_file <- paste0("https://api.stlouisfed.org/fred/series/observations?series_id=", id ,"&frequency=", freq , "&aggregation_method=eop&file_type=json&api_key=29fb098284eda4e97e4678353c8f25f2")
  json_data <- fromJSON(txt=json_file)
  myDat <- suppressWarnings(xts(as.numeric(json_data$observations$value), order.by=as.Date(json_data$observations$date)))
  myDat <- na.omit(myDat)
  colnames(myDat)<- id
  assign(id, myDat, .GlobalEnv)
}

mult.xts <- function (x, y) 
{
  xmat <- as.matrix(x)
  ymat <- as.matrix(y)
  result <- c(rep(NA, NAs), result)
  reclass(result, x)
}

create.matrix <- function(i, j) {
  x <- matrix()
  length(x) <- i*j
  dim(x) <- c(i,j)
  x
}

combine.past.proj <- function(past, proj, nsim){
  tmp <- create.matrix(nrow(past), nsim) 
  for(i in 1:nsim){
    tmp[,i] <- coredata(past)
  }
  tmp <- xts(tmp, order.by=index(past))
  return(rbind(tmp, proj))
}

write.zoo.transpose<-function(x, file){
  df<- data.frame(t(coredata(x)))
  colnames(df)<-index(x)
  write.csv(df,file=file)
}