# ARIMA SIMULATION and ESTIMATION EXAMPLES...
#


# You can execute the lines that are not commented
# (don't leave any out!)

# Example 1

# Simple example, an AR(3) with phi values 0.1, 0.2, and 0.1 with
# error standard deviation of .2

# First, look over the help file on arima.sim

# We need to define the model. This is does with an assignment
# to the argument "model" and the assigned object
# must be a list.
# Thus we use model=list(ar=c(0.1, 0.2, 0.1))

# We need to assign a value to rand.gen to generate the noise terms.
# This assignment needs to be a function.
# Thus we fire up finmetrics

module(finmetrics)

#Now we set up our simulation ----

error.model=function(n){rnorm(n, sd=.2)}

# Now, we put the whole package together and assign it to Ex1.

Ex1=arima.sim(model=list(ar=c(0.1, 0.2, 0.1)), n=1000,
n.start=200, start.innov=rnorm(200, sd=.2),
rand.gen=error.model )

#Explore this object

class(Ex1)
tsplot(Ex1)


#Now let's see if we can get the phi's back from the time series.
#You will first want to read the help file on arima.mle

arima.mle(Ex1, model=list(order=c(3,0,0)))

# check out those parentheses!


# Example 2

# OK, now lets do an ARIMA(1,0,1) with noise standard deviation 0.3,
# with phi=.3 and theta=.6

error.model=function(n){rnorm(n, sd=.3)}

Ex2=arima.sim(model=list(ar=.3, ma=.6), n=1000, n.start=200,
start.innov=rnorm(200, sd=.3), rand.gen=error.model )

arima.mle(Ex2, model=list(order=c(1,0,1)))


# General Observations...
#
# These estimates using 1000 observation
# are perhaps less accurate than one might hope.
# Especially since in the real world, we never know the model
# exactly, yet here we do --- since we simulated it.