-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomwalk.py
69 lines (57 loc) · 1.96 KB
/
randomwalk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# %pip install numpy
#%pip install matplotlib
import numpy as np
import matplotlib.pyplot as plt
# Parameters
num_periods = 252 # Assume 252 trading days in a year
initial_price = 1000 # Initial stock price
# ばらつき=標準偏差
volatility = 0.02 # Daily volatility
# # Generate random walk
# prices = [initial_price]
# for _ in range(num_periods):
# prices.append(prices[-1] + prices[-1] * volatility * np.random.randn())
# # Plot the results
# plt.figure(figsize=(10,6))
# plt.plot(prices)
# plt.title("Random Walk Simulation of Stock Price")
# plt.xlabel("Days")
# plt.ylabel("Stock Price")
# plt.grid(True)
# plt.show()
def simulate_geometric_brownian_motion(num_periods, initial_price, mu, sigma, num_trials):
dt = 1 # Time increment (1 day)
all_prices = []
for _ in range(num_trials):
prices = [initial_price]
for _ in range(num_periods):
dS = prices[-1] * (mu * dt + sigma * np.sqrt(dt) * np.random.randn())
prices.append(prices[-1] + dS)
all_prices.append(prices)
return all_prices
# Parameters
mu = 0.0005 # Expected daily return (approximate)
# # Parameters
num_trials = 10 # Number of random walk simulations
# Simulate geometric brownian motion
all_prices = simulate_geometric_brownian_motion(num_periods, initial_price, mu, volatility, num_trials)
# Plot the results
plt.figure(figsize=(10,6))
for prices in all_prices:
plt.plot(prices, alpha=0.6)
plt.title(f"Geometric Brownian Motion Simulations of Stock Price ({num_trials} Trials)")
plt.xlabel("Days")
plt.ylabel("Stock Price")
plt.grid(True)
plt.show()
# # Simulate random walks
# all_prices = simulate_random_walk(num_periods, initial_price, volatility, num_trials)
# # Plot the results
# plt.figure(figsize=(10,6))
# for prices in all_prices:
# plt.plot(prices, alpha=0.6)
# plt.title(f"Random Walk Simulations of Stock Price ({num_trials} Trials)")
# plt.xlabel("Days")
# plt.ylabel("Stock Price")
# plt.grid(True)
# plt.show()