Skip to content

Commit

Permalink
Merge pull request #30 from AndrewRook/model_validation_image
Browse files Browse the repository at this point in the history
Model validation image made
  • Loading branch information
AndrewRook authored Aug 17, 2016
2 parents 504e9cb + d2d368c commit 643f1f5
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 12 deletions.
Binary file modified doc/source/_static/validation_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 8 additions & 3 deletions doc/source/dev.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ For Developers
This section of the documentation covers things that will be useful for those already contributing to NFLWin.

.. note::
Unless stated otherwise assume that all filepaths given in this section start at the root directory for the repo.

Unless stated otherwise assume that all filepaths given in this section start at the root directory for the repo.

Testing Documentation
------------------------------------------
Expand All @@ -24,7 +23,13 @@ NFLWin comes with a pre-trained model, but if the code generating that model is
$ python make_default_model.py

.. note::
This script hardcodes in the seasons to use for training and testing samples. After each season those will likely need to be updated to use the most up-to-date data.
This script hardcodes in the seasons to use for training and
testing samples. After each season those will likely need to be
updated to use the most up-to-date data.

.. note::
This script requires ``matplotlib`` in order to run, as it produces a
validation plot for the documentation.

Cutting a New Release
----------------------------------
Expand Down
8 changes: 5 additions & 3 deletions doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ Once data is loaded, using the model to predict WP is easy:
>>> standard_model.predict_wp(plays)
array([ 0.58300397, 0.64321796, 0.18195466])
Current Default Model
---------------------

.. image:: _static/validation_plot.png

Why NFLWin?
--------------
Put simply, there are no other options: while WP models have been
Expand All @@ -101,6 +106,3 @@ Resources
Developer Documentation <dev.rst>
Full API Documentation <modules.rst>

* :ref:`Full API Documentation <modindex>`
* :ref:`Search NFLWin's Documentation <search>`

5 changes: 2 additions & 3 deletions doc/source/model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,8 @@ correctly. The ratio of these two KDEs is the actual WP measured
from the test data set at a given *predicted* WP. While all of this is
measured in :meth:`~nflwin.model.WPModel.validate_model`, you can plot
it for yourself by calling the
:meth:`~nflwin.model.WPModel.plot_validation` method:

.. image:: _static/validation_plot.png
:meth:`~nflwin.model.WPModel.plot_validation` method, which will
generate a plot like that shown on the home page.

From there NFLWin computes both the maximum deviation at any given
percentage and the total area between the estimated WP from the model
Expand Down
29 changes: 27 additions & 2 deletions make_default_model.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,49 @@
"""A simple script to create, train, validate, and save the default model"""
from __future__ import division, print_function

import datetime as dt
import time
import os

from nflwin import model

def main():
start = time.time()
win_probability_model = model.WPModel()
win_probability_model.train_model(training_seasons=[2009, 2010, 2011, 2012, 2013, 2014])

training_seasons = [2009, 2010, 2011, 2012, 2013, 2014]
validation_seasons = [2015]
season_types = ["Regular", "Postseason"]

win_probability_model.train_model(training_seasons=training_seasons,
training_season_types=season_types)
print("Took {0:.2f}s to build model".format(time.time() - start))

start = time.time()
max_deviation, residual_area = win_probability_model.validate_model(validation_seasons=[2015])
max_deviation, residual_area = win_probability_model.validate_model(validation_seasons=validation_seasons,
validation_season_types=season_types)
print("Took {0:.2f}s to validate model, with a max residual of {1:.2f} and a residual area of {2:.2f}"
.format(time.time() - start, max_deviation, residual_area))

win_probability_model.save_model()

ax = win_probability_model.plot_validation(label="max deviation={0:.2f}, \n"
"residual total area={1:.2f}"
"".format(max_deviation, residual_area))
curr_datetime = dt.datetime.now()
ax.set_title("Model Generated At: " + curr_datetime.strftime("%Y-%m-%d %H:%M:%S"))
ax.legend(loc="lower right", fontsize=10)
ax.text(0.02, 0.98, ("Data from: {0:s}\n"
"Training season(s): {1:s}\n"
"Validation season(s): {2:s}"
"".format(", ".join(season_types),
", ".join(str(year) for year in training_seasons),
", ".join(str(year) for year in validation_seasons))),
ha="left", va="top", fontsize=10, transform=ax.transAxes)

this_filepath = os.path.dirname(os.path.abspath(__file__))
save_filepath = os.path.join(this_filepath, "doc", "source", "_static", "validation_plot.png")
ax.figure.savefig(save_filepath)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion nflwin/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def plot_validation(self, axis=None, **kwargs):
import matplotlib.pyplot as plt
if axis is None:
axis = plt.figure().add_subplot(111)
axis.plot([0, 1], [0, 1], ls="--", lw=2, color="black")
axis.plot([0, 100], [0, 100], ls="--", lw=2, color="black")
axis.set_xlabel("Predicted WP")
axis.set_ylabel("Actual WP")
axis.plot(self.sample_probabilities,
Expand Down

0 comments on commit 643f1f5

Please sign in to comment.