Trying to do a simulation in R -
i'm pretty new r, hope can me! i'm trying simulation bachelor's thesis, want simulate how stock evolves. i've done simulation in excel, problem can't make large of simulation, program crashes! therefore i'm trying in r.
the stock evolves follows (everything except $\epsilon$ consists of constants known): $$w_{t+\delta t} = w_t exp^{r \delta t}(1+\pi(exp((\sigma \lambda -0.5\sigma^2) \delta t+\sigma \epsilon_{t+\delta t} \sqrt{\delta t}-1))$$
the thing here stochastic $\epsilon$, represented brownian motion n(0,1).
what i've done in excel:
- made 100 samples size of 40. these samples standard normal distributed: n(0,1).
- then these outcomes used calculate how stock affected these (the normal distribution represent shocks economy).
my problem in r:
i've used sample function: x <- sample(norm(0,1), 1000, t)
have 1000 samples, distributed. don't know how put these results formula have evolution of stock. can help?
using r (discrete) simulation
there 2 aspects question: conceptual , coding.
let's deal conceptual first, starting meaning of equation:
1. conceptual issues
the first thing note evolution equation continuous in time, running simulation described above means accepting discretisation of problem. whether or not appropriate depends on model , how have obtained evolution equation.
if run discrete simulation, key decision have make stepsize $\delta t$ use. can explore different step-sizes observe effect of step-size, or can proceed analytically , attempt derive appropriate step-size.
once have step-size, simulation consists of pulling new shocks (samples of standard normal distribution), , evolving equation iteratively until desired time has elapsed. final state $w_t$ available analyse wish. (if retain of $w_t$, have distribution of trajectory of system well, can analyse.)
so:
- your $x$ sampled distribution of shocks, i.e. $\epsilon_t=0$.
- to simulate evolution of $w_t$, need initial condition $w_0$. depends on you're modelling. if you're modelling values of single stock starting @ initial price $w_0$, initial state 1000 element vector constant value.
- now evaluate equation, plugging in constants, $w_0$, , initial shocks $\epsilon_0 = x$ distribution of prices $w_1$.
- repeat: sample $x$ again -- $\epsilon_1$. plugging in, gives $w_2$ etc.
2. coding simulation (simple example)
one of useful features of r operators work element-wise on vectors.
so can pretty type in equation more or less is.
i've made few assumptions parameters in equation, , i've ignored $\pi$ function -- can add in later.
so end code looks this:
dt <- 0.5 # step-size r <- 1 # parameters lambda <- 1 sigma <- 1 # std deviation w0 <- rep(1,1000) # presumed initial condition -- prices start @ 1 # show example iteration -- incorporate 1 line production code... x <- rnorm(1000,mean=0,sd=1) # random shock w1 <- w0*exp(r*dt)*(1+exp((sigma*lambda-0.5*sigma^2)*dt + sigma*x*sqrt(dt) -1)) # evolution
when you're ready let simulation run, merge last 2 lines, i.e. include sampling statement in evolution statement. 1 line of code can run manually or embed loop, along other analysis want run.
# general simulation step w <- w*exp(r*dt)*(1+exp((sigma*lambda-0.5*sigma^2)*dt + sigma*rnorm(1000,mean=0,sd=1)*sqrt(dt) -1))
you can visualise changes , obtain summary statistics (5-number summary):
hist(w) summary(w)
of course, you'll still need work through details of want model , how want go analysing --- , you've got $\pi$ function deal --- should started toward using r discrete simulation.
Comments
Post a Comment