import json
import matplotlib.pyplot as plt
from pprint import pprint
import numpy as np
from scipy.stats import linregress
from util.stats import *

with open('data/game_stats.json', 'r') as f:
	df = json.load(f)

X, y = [], []
for match, stats in df.items():
	home, away = stats['home'], stats['away']
	if home['mp'] != away['mp'] != '240': continue
	try:
		ft_dif = float(home['fta'])  - float(away['fta'])
		pt_dif = float(home['pts']) - float(away['pts'])
		if abs(pt_dif) > 10: continue
	except:
		continue

	X.append(ft_dif)
	y.append(pt_dif)

c = 0
for f, p in zip(X, y):
	if f * p > 0:
		c += 1

print(c / len(X))

slope, intercept, r, p, std = linregress(X, y)
f = lambda x: x*slope + intercept
fit_y = [f(min(X)), f(max(X))]

plt.xlabel('Free Throw Attempts')
plt.ylabel('Point Differential')
plt.title('FTA vs Point Differential')
print(correlation(X, y))
plt.plot([min(X), max(X)], fit_y, color = 'red')
plt.scatter(X, y)

plt.show()
[((32, 30, 32, 46), 'scipy.stats.linregress', 'linregress', ({(32, 41, 32, 42): 'X', (32, 44, 32, 45): 'y'}, {}), '(X, y)', False, 'from scipy.stats import linregress\n'), ((36, 0, 36, 33), 'matplotlib.pyplot.xlabel', 'plt.xlabel', ({(36, 11, 36, 32): '"""Free Throw Attempts"""'}, {}), "('Free Throw Attempts')", True, 'import matplotlib.pyplot as plt\n'), ((37, 0, 37, 32), 'matplotlib.pyplot.ylabel', 'plt.ylabel', ({(37, 11, 37, 31): '"""Point Differential"""'}, {}), "('Point Differential')", True, 'import matplotlib.pyplot as plt\n'), ((38, 0, 38, 38), 'matplotlib.pyplot.title', 'plt.title', ({(38, 10, 38, 37): '"""FTA vs Point Differential"""'}, {}), "('FTA vs Point Differential')", True, 'import matplotlib.pyplot as plt\n'), ((41, 0, 41, 17), 'matplotlib.pyplot.scatter', 'plt.scatter', ({(41, 12, 41, 13): 'X', (41, 15, 41, 16): 'y'}, {}), '(X, y)', True, 'import matplotlib.pyplot as plt\n'), ((43, 0, 43, 10), 'matplotlib.pyplot.show', 'plt.show', ({}, {}), '()', True, 'import matplotlib.pyplot as plt\n'), ((9, 6, 9, 18), 'json.load', 'json.load', ({(9, 16, 9, 17): 'f'}, {}), '(f)', False, 'import json\n')]