import numpy as np
import pandas as pd


# I will use the fact that real data isn't perfect and have 
# somewhat larger tails on the gains distribution.
# I will make a chi-squared test to test for `gaussianity`.

def chi_test(dist_test, dist_target):
    # binning
    test = []
    target = []
    # make 6 groups
    si = dist_test.std()
    mu = dist_test.mean()
    mask = lambda x,i: ((x >= i*si) & (x < (i+1)*si) )
    test.append(dist_test[dist_test < -2*si].size)
    target.append(dist_target[dist_target < -2*si].size)
    for i in range(-2,2):
        c = len(list(filter(lambda x: mask(x,i), dist_test)))
        test.append(c)
        c = len(list(filter(lambda x: mask(x,i), dist_target)))
        target.append(c)
    test.append(dist_test[dist_test >= 2*si].size)
    target.append(dist_target[dist_target >= 2*si].size)
    return sum(map(lambda l: 0.2*(l[1] - l[0])**2 / l[0],list(zip(target, test))))


symbols = np.loadtxt("../utils/symbols", dtype=str)
n_sym = symbols.size

train_data = {}
test_data = {}

for s in map(lambda x: x[2:-1], symbols[:2]):
    data = pd.read_csv("../data/{}.csv".format(str(s)))
    temp = np.log(data.Close).diff().values[1:]
    temp = (temp - temp.mean())/temp.std() # normalize
    train_data[s] = temp

for s in map(lambda x: x[2:-1], symbols[2:]):
    data = pd.read_csv("../data/{}.csv".format(str(s)))
    temp = np.log(data.Close).diff().values[1:]
    temp = (temp - temp.mean())/temp.std() # normalize
    test_data[s] = temp

# generate gaussian noise
l = train_data["GOOG"].size
for i in range(n_sym): # for train
    train_data["noise_" + str(i)] = np.random.normal(0, 1, l)

test_data["noise_0"] = np.random.normal(0, 1, l)  # for test

train_keys = train_data.keys()
test_keys = test_data.keys()

st = []
for k in train_keys:
    st.append(chi_test(train_data[k], np.random.normal(0,1,l)))

lim = (max(st) - min(st))/2.0

for k in test_keys:
    chi = chi_test(test_data[k], np.random.normal(0,1,l)) 
    if chi > lim:
        print("Stock with symbol {0} is true (chi={1})".format(k, chi))
    else:
        print("Stock with symbol {0} is false (chi={1})".format(k, chi))

        