0

How can I generate a price time series using the following equation:

p(t) = p0(1+A * sin(ωt +0.5η(t)))

where t ranges from 0 to 1 in 1000 time steps, p0 = 100, A = 0.1, and ω = 100. η(t) is a sequence of i.i.d Gaussian random variables with zero mean and unit variance.

I have use the code as follows to generate price, but it seems not as required. So I need helps from the community. Thanks in advance.

from scipy.stats import norm
import numpy as np
import matplotlib.pyplot as plt
mu = 0
sigma = 1
np.random.seed(2020)

dist = norm(loc = mu,scale=sigma)
sample = dist.rvs(size = 1000)
stock_price = np.exp(sample.cumsum())
print(stock_price)
plt.plot(stock_price)
plt.xlabel("Day")
plt.ylabel("Price")
plt.title("Simulated Stock price")
plt.show()
ah bon
  • 9,293
  • 12
  • 65
  • 148

2 Answers2

2

Assuming I haven't missed anything, this should do the trick

import numpy as np
import matplotlib.pyplot as plt

n_t = np.random.normal(0, 1, 1000)
t = np.arange(0, 1, 1/1000)

p_0, A, w = 100, 0.1, 100
ts = p_0 * (1 + A * np.sin(w * t + 0.5 * n_t))

plt.plot(t, ts)
plt.xlabel("Day")
plt.ylabel("Price")
plt.show()

which gives the plot enter image description here

Hiho
  • 643
  • 4
  • 8
  • Thank you. Could you check my answer and give me feedback? It seems the result from my code seems more likes price volatility. – ah bon Mar 31 '20 at 08:42
  • 1
    Hmh this line `stock_price = np.exp(sample.cumsum())` is def not quite right, you are using both np.exp and np.cumsum and neither of those is in the actual equation you posted above. – Hiho Apr 01 '20 at 16:36
0

My trial, not sure if it's correct, welcome to give me some comments.

import numpy as np
import math
np.random.seed(2020)

mu = 0
sigma = 1
dt = 0.01
p0 = 100
A = 0.1
w = 100
N = 1000
for t in np.linspace(0, 1, 1000):
    X = np.random.normal(mu * dt, sigma* np.sqrt(dt), N)
    X = np.cumsum(X)
    pt = p0 * (1+ A*np.sin(w*t + 0.5*X))
#     print(pt)
plt.plot(pt)
plt.xlabel("Day")
plt.ylabel("Price")
plt.title("Simulated Stock price")
plt.show()

Out:

enter image description here

ah bon
  • 9,293
  • 12
  • 65
  • 148