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()
[((654, 670), 'scipy.stats.linregress', 'linregress', (['X', 'y'], {}), '(X, y)\n', (664, 670), False, 'from scipy.stats import linregress\n'), ((737, 770), 'matplotlib.pyplot.xlabel', 'plt.xlabel', (['"""Free Throw Attempts"""'], {}), "('Free Throw Attempts')\n", (747, 770), True, 'import matplotlib.pyplot as plt\n'), ((771, 803), 'matplotlib.pyplot.ylabel', 'plt.ylabel', (['"""Point Differential"""'], {}), "('Point Differential')\n", (781, 803), True, 'import matplotlib.pyplot as plt\n'), ((804, 842), 'matplotlib.pyplot.title', 'plt.title', (['"""FTA vs Point Differential"""'], {}), "('FTA vs Point Differential')\n", (813, 842), True, 'import matplotlib.pyplot as plt\n'), ((917, 934), 'matplotlib.pyplot.scatter', 'plt.scatter', (['X', 'y'], {}), '(X, y)\n', (928, 934), True, 'import matplotlib.pyplot as plt\n'), ((936, 946), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (944, 946), True, 'import matplotlib.pyplot as plt\n'), ((201, 213), 'json.load', 'json.load', (['f'], {}), '(f)\n', (210, 213), False, 'import json\n')]