Spaces:
Sleeping
Sleeping
Commit ·
cb8b17a
1
Parent(s): 90e728c
gerador adicionado
Browse files- src/generator.py +48 -0
- src/streamlit_app.py +1 -1
src/generator.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def simulate_prices_alt(mu, sigma, n):
|
| 9 |
+
# mu, sigma, n = 7.0, 3.0, 1000 # mean and standard deviation
|
| 10 |
+
s = np.random.normal(mu, sigma, n)
|
| 11 |
+
return s
|
| 12 |
+
|
| 13 |
+
def client_simulate_prices_alt():
|
| 14 |
+
s = simulate_prices_alt(7.0, 3.0, 50) # mean and standard deviation, number of samples
|
| 15 |
+
|
| 16 |
+
def simulate_prices(n, k, mu, sigma, delta_t, S0):
|
| 17 |
+
## https://medium.com/@MachineLearningYearning/how-to-simulate-stock-prices-452042862989
|
| 18 |
+
r = (mu*delta_t + sigma * np.sqrt(delta_t) * np.random.normal(size=(n, k)))
|
| 19 |
+
p = np.cumprod(r+1, axis=0) * S0 # compute cumulative returns, and multiply by initial price to calculate prices
|
| 20 |
+
return r, p
|
| 21 |
+
|
| 22 |
+
def client_simulate_prices():
|
| 23 |
+
# # Simulate stock prices using a geometric Brownian motion model
|
| 24 |
+
# This is a simple model for stock prices, where the price follows a random walk with
|
| 25 |
+
|
| 26 |
+
# time parameters
|
| 27 |
+
T = .5 # simulate half a year
|
| 28 |
+
delta_t = 1.0/252.0 # each day is 1/252 of a trading year
|
| 29 |
+
n = int(T / delta_t) # total number of samples
|
| 30 |
+
S0 = 10 # initial price of stock
|
| 31 |
+
mu = 1 # drift
|
| 32 |
+
sigma = 1 # volatility
|
| 33 |
+
k = 1 # number of realizations
|
| 34 |
+
|
| 35 |
+
r, p = simulate_prices(n, k, mu, sigma, delta_t, S0)
|
| 36 |
+
|
| 37 |
+
print(p[:,:100])
|
| 38 |
+
plt.figure()
|
| 39 |
+
plt.plot(p[:,:100])
|
| 40 |
+
plt.title(f'Simulated Stock Prices\n{1} Realizations')
|
| 41 |
+
plt.show()
|
| 42 |
+
|
| 43 |
+
def client_test_simulate_prices():
|
| 44 |
+
df = pd.read_csv("my_data1.csv")
|
| 45 |
+
print(df.head())
|
| 46 |
+
print(df.describe())
|
| 47 |
+
|
| 48 |
+
client_simulate_prices()
|
src/streamlit_app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
-
|
| 4 |
|
| 5 |
#data = np.random.randint(1, 100, size=50)
|
| 6 |
#df = pd.DataFrame(data, columns=['random_numbers'])
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
|
| 5 |
#data = np.random.randint(1, 100, size=50)
|
| 6 |
#df = pd.DataFrame(data, columns=['random_numbers'])
|