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, 'scipy.stats.linregress', 'linregress', False, 'from scipy.stats import linregress\n'), (36, 'matplotlib.pyplot.xlabel', 'plt.xlabel', True, 'import matplotlib.pyplot as plt\n'), (37, 'matplotlib.pyplot.ylabel', 'plt.ylabel', True, 'import matplotlib.pyplot as plt\n'), (38, 'matplotlib.pyplot.title', 'plt.title', True, 'import matplotlib.pyplot as plt\n'), (41, 'matplotlib.pyplot.scatter', 'plt.scatter', True, 'import matplotlib.pyplot as plt\n'), (43, 'matplotlib.pyplot.show', 'plt.show', True, 'import matplotlib.pyplot as plt\n'), (9, 'json.load', 'json.load', False, 'import json\n')]