Spaces:
Sleeping
Sleeping
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| import pandas as pd | |
| def simulate_prices_alt(mu, sigma, n): | |
| # mu, sigma, n = 7.0, 3.0, 1000 # mean and standard deviation | |
| s = np.random.normal(mu, sigma, n) | |
| return s | |
| def client_simulate_prices_alt(): | |
| s = simulate_prices_alt(7.0, 3.0, 50) # mean and standard deviation, number of samples | |
| def simulate_prices(n, k, mu, sigma, delta_t, S0): | |
| ## https://medium.com/@MachineLearningYearning/how-to-simulate-stock-prices-452042862989 | |
| r = (mu*delta_t + sigma * np.sqrt(delta_t) * np.random.normal(size=(n, k))) | |
| p = np.cumprod(r+1, axis=0) * S0 # compute cumulative returns, and multiply by initial price to calculate prices | |
| return r, p | |
| def client_simulate_prices(): | |
| # # Simulate stock prices using a geometric Brownian motion model | |
| # This is a simple model for stock prices, where the price follows a random walk with | |
| # time parameters | |
| T = .5 # simulate half a year | |
| delta_t = 1.0/252.0 # each day is 1/252 of a trading year | |
| n = int(T / delta_t) # total number of samples | |
| S0 = 10 # initial price of stock | |
| mu = 1 # drift | |
| sigma = 1 # volatility | |
| k = 1 # number of realizations | |
| r, p = simulate_prices(n, k, mu, sigma, delta_t, S0) | |
| print(p[:,:100]) | |
| plt.figure() | |
| plt.plot(p[:,:100]) | |
| plt.title(f'Simulated Stock Prices\n{1} Realizations') | |
| plt.show() | |
| def client_test_simulate_prices(): | |
| df = pd.read_csv("my_data1.csv") | |
| print(df.head()) | |
| print(df.describe()) | |
| client_simulate_prices() | |