# Michael Greenberg

# suite_quality.py

from scripts.learning import *
from scripts.predictions import *
from classes.lines import *

def test_suite():
	years = range(2009,2014)

	clf = model_from_years(years) 

	lines = Lines()
	lines.lines_from_dataset('2014_nfl_lines.csv')
	season = Season(2014)
	season.record_performances_for_week(1)

	predictions_correct = 0
	line_bets_correct = 0

	for week in range(2,18):
		print "week {0}".format(week)
		week_games = 0	
		week_predictions_correct = 0
		week_line_bets_correct = 0
		for game in nflgame.games(2014, week = week):
			week_games += 1
			(home_performance, away_performance) = performances_for_game(game)
			home_team, away_team = season.teams[game.home], season.teams[game.away]
			predicted_home_team_parameters = predict_statistics(home_team, away_team, season, True)
			home_team_prediction = clf.predict(predicted_home_team_parameters)
			predicted_away_team_parameters = predict_statistics(away_team, home_team, season, False)
			away_team_prediction = clf.predict(predicted_away_team_parameters)
			line = lines.line_for_game(game)
			print game.home, "Prediction: ", (home_team_prediction - away_team_prediction)
			if correct_prediction(home_team_prediction - away_team_prediction, game):
				print "Actual: ", net_score(game, True), " O"
				predictions_correct += 1
				week_predictions_correct += 1
			else:
				print "Actual: ", net_score(game, True), " X"
			home_net_score = net_score(game, True)
			if correct_line_bet(home_team_prediction - away_team_prediction, line, home_net_score):
				print "Line: ", line.favorite, line.line, "O"
				line_bets_correct += 1
				week_line_bets_correct += 1
			else:
				print "Line: ", line.favorite, line.line, "X"
		season.record_performances_for_week(week)
		print "week {0} results: {1} win/loss correct, {2} line_bet correct".format(week, float(week_predictions_correct)/week_games, float(week_line_bets_correct)/week_games)
	return (float(predictions_correct)/len(nflgame.games(2014)), float(line_bets_correct)/len(nflgame.games(2014)))

if __name__ == '__main__':

	fraction_correct, fraction_line = test_suite()

	print fraction_correct, fraction_line