Skip to content

Instantly share code, notes, and snippets.

@talolard
Created January 14, 2019 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save talolard/c1b6f4677c00dc6f9fc1ab24d79a54d5 to your computer and use it in GitHub Desktop.
Save talolard/c1b6f4677c00dc6f9fc1ab24d79a54d5 to your computer and use it in GitHub Desktop.
How to make a market that is complex but learnable
import numpy as np
def make_stock(length=100, num_stocks=2):
alpha = 0.9
k = 2
cov = np.random.normal(0, 5, [num_stocks, num_stocks])
cov = cov.dot(cov.T) # This is a positive semidefinite matrix, e.g. a covariance matrix
A = np.random.multivariate_normal(np.zeros(num_stocks), cov, size=[length]) # sample noise, with covariance
B = np.random.multivariate_normal(np.zeros(num_stocks), cov, size=[length]) # sample another noise, with covariance
bs = [np.zeros(shape=num_stocks)] #
ps = [np.zeros(shape=num_stocks)] # The prices
for a, b in zip(A, B):
bv = alpha * bs[-1] + b # calculate some trend
bs.append(bv)
pv = ps[-1] + bs[-2] + k * a # Previosu price + previous trend factor, plus some noise
ps.append(pv)
# ps = [0]
# for a,b,common in zip(A,BB,commonNoise):
# ps.append(ps[-1]+b+k*a+2*common)
# P = np.array(ps)
# P = np.exp(P/(P.max()-P.min()))
ps = np.array(ps).T # reshape it so that its [length,stocks]
R = ps.max(1) - ps.min(1) # Scale factor
prices = np.exp(ps.T / (R)) *np.random.uniform(10,250,num_stocks) # Normalize, exponantiate then make the prices more varied
return prices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment