Trying to do a simulation in R

I am new to R, so I hope you can help me! I am trying to make a simulation for my bachelor's thesis, where I want to imitate how the stock develops. I did a simulation in Excel, but the problem is that I can’t do such a big simulation because the program crashes! So I try in R.

The action develops as follows (everything except $ \ epsilon $ consists of constants that are 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 only stochastic thing here is $ \ epsilon $, which is represented by the Brownian motion with N (0,1).

What I did in Excel:

  • 100 samples were made with a size of 40. All of these samples are standard normal distributed: N (0,1).
  • These results are then used to calculate how they are affected by the effects of stocks (normal distribution is a shock to the economy).

My problem is in R:

I used the fetch function: x <- sample(norm(0,1), 1000, T) Therefore, I have 1000 samples that are usually distributed. Now I do not know how to bring these results into the formula that I have for the evolution of my stock. Can anyone help?

+4
source share
1 answer

Using R for (discrete) modeling

There are two aspects to your question: conceptual and coding.

, :

1.

, , , , , , . , , , .

, , , , $\ Delta t $. , , .

, , ( ) . $W_t $ , , . ( $W_t $, , .)

:

  • $x $ , .. $\ epsilon_t = 0 $.
  • $W_t $ $W_0 $. , . , $W_0 $, - 1000 .
  • , , $W_0 $ $\ epsilon_0 = x $, $W_1 $.
  • : $x $ - $\ epsilon_1 $. , $W_2 $ ..

2. ( )

R , .

, , .

, $\ pi $- .

, , :

 dt <- 0.5   # step-size
 r <- 1      # parameters
 lambda <- 1
 sigma <- 1    # std deviation
 w0 <- rep(1,1000)   # presumed initial condition -- prices start at 1

 # Show an example iteration -- incorporate into one line for 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

, , . , , , .

 # 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))

(5- ):

 hist(w)
 summary(w)

, , , - $\ pi $, - - R .

+2

Source: https://habr.com/ru/post/1533116/


All Articles