# SDE simulations
# STAT 753
# April 30, 2020

###########################################################
# Use the Euler method for simulating SDEs
###########################################################

# Brownian motion
T <- 1
k <- 1000
dt <- T/k
X <- rep(NA,(k+1))

# Generate a sample path
X[1] = 0
for(i in 1:k){
  X[i+1] = X[i] + sqrt(dt)*rnorm(1)
}

# Plot
ts = seq(0,T,by=dt)
plot(ts, X, xlab="Time", main="Brownian motion: Euler approximation sample path", col="blue", type="l")


### Exercise 1 
### Modify the code above for the following processes:
### Brownian motion with drift, geometric BM, Brownian bridge, and an Ornstein-Uhlenbeck process


### Exercise 2
### Adapt the code above to generate an arbitrary number of sample paths and plot them on the same figure

