File size: 1,536 Bytes
cb8b17a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

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