# Provide the inline code necessary for loading any required libraries

import matplotlib.pyplot as plt
%matplotlib inline

"""
Explanation: MLB Modern Era Salary Analysis
Lahman’s Baseball Database contains complete batting and pitching statistics from 1871 to 2013, plus fielding statistics, standings, team stats, managerial records, post-season data, and more.
Objective
One of the topics covered in Lahman's Baseball Database is MLB annual salaries. This notebook provides a sample analysis that explores trends in player salaries. Several questions are addressed:

What is the average salary increase per league since 1985?
What is the average salary increase per league since 1985?
Can we predict future average salary increase per league?

Prepare Environment
Bootstrap notebook with necessary notebook and library dependencies.
Prerequisites
This notebook requires the installation of the following software dependencies:
!pip install statsmodels
End of explanation
"""


import pandas as pd
df_mlb_Salaries = pd.read_csv('/resources/mlb_Salaries_2011.csv').dropna()
df_mlb_Salaries.describe()

df_mlb_Salaries.plot()

df_mlb_Salaries.tail()

"""
Explanation: Load the Data

Visit the Lahman’s Baseball Database and download the latest data. After unzipping the package, upload the Salaries.csv file to your workbench.
Import the latest MLB Salary data and take a peak at the dataset.

<div class="alert" style="border: 1px solid #aaa; background: radial-gradient(ellipse at center, #ffffff 50%, #eee 100%);">
<div class="row">
    <div class="col-sm-1"><img src="https://knowledgeanyhow.org/static/images/favicon_32x32.png" style="margin-top: -6px"/></div>
    <div class="col-sm-11">In IBM Knowledge Anyhow Workbench, you can drag/drop the file on your workbench browser tab to simplify the uploading process.</div>
</div>
</div>
End of explanation
"""


american_league = df_mlb_Salaries.query('lgID == "AL"')
national_league = df_mlb_Salaries.query('lgID == "NL"')

"""
Explanation: Observations
Based on the 2013 data:

Between 1985 and 2013 there are 23,956 Salary records.
The average annual Salary across all of MLB for the last 28 years is 1.8M USD.
The largest annual Salary across all of MLB for the last 28 years is 33M USD.

Question 1: Average Salary Per League
What is the average salary increase per league since 1985?
Data Munging
Step A: Partition the data by league
End of explanation
"""


american_league_avg_annual_salary = american_league.groupby(['yearID']).mean()[['salary']]
national_league_avg_annual_salary = national_league.groupby(['yearID']).mean()[['salary']]

"""
Explanation: Step B: Create a pivot table for salaries per year per league
End of explanation
"""


al = pd.Series(american_league_avg_annual_salary.salary, name="American League")
nl = pd.Series(national_league_avg_annual_salary.salary, name="National League")

df_league_salary_history = pd.concat([al, nl], axis=1)
df_league_salary_history.head()

"""
Explanation: Step C: Create a Dataframe depicting average annual salaries per league per year.
End of explanation
"""


df_league_salary_history.describe()

df_league_salary_history.plot()
plt.rcParams['xtick.major.pad']='10'
plt.title('MLB Annual Salary Averages')
plt.xlabel('Year')
plt.ylabel('Average Salary')
yvalues = american_league_avg_annual_salary.index
plt.xticks(yvalues, rotation='vertical')
plt.show()

"""
Explanation: Data Exploration
Plot our results and compare annual league salary averages.
End of explanation
"""


def annual_salary_delta(previous_year_salary, current_year_salary):
    '''Compute annual salary delta.'''
    return current_year_salary - previous_year_salary

def annual_league_salary_increases(salaries):
    '''Return list of annual salary deltas for a series of league salaries.'''
    salincperyr = []
    for idx, val in enumerate(salaries):
        if idx == 0:
            salincperyr.append(0)
        elif idx == len(salaries)-1:
            salincperyr.append(0)
        else:
            salincperyr.append(annual_salary_delta(salaries.iloc[idx-1],
                                               salaries.iloc[idx]))
    return salincperyr

"""
Explanation: Observations

Between 1985 and 2013 the American League has paid an average of 200K USD more than the National League.
In 2004, the annual salaries per league diverged with a greater degree of seperation.

<div class="alert alert-info">In 2004, [Texas traded A-Rod to the NY Yankees](http://sports.espn.go.com/mlb/news/story?id=1735937). This transaction could have triggered the abnormal divergence.
</div>

Question 2: Average Annual Salary Increases Per League
What is the average salary increase per league since 1985?
Data Munging
Step A: Define Helper Methods
End of explanation
"""


tablecols = []
tablecols.append(yvalues)
tablecols.append(df_league_salary_history["American League"].tolist())
tablecols.append(df_league_salary_history["National League"].tolist())
tablecols.append(annual_league_salary_increases(df_league_salary_history["American League"]))
tablecols.append(annual_league_salary_increases(df_league_salary_history["National League"]))
df_league_salary_detailed_history = pd.DataFrame(tablecols).T
df_league_salary_detailed_history.columns = ['Year','alAvgSalary', 'nlAvgSalary','alAnnualSalaryIncrease', 'nlAnnualSalaryIncrease']
df_league_salary_detailed_history.set_index(df_league_salary_detailed_history['Year'])
df_league_salary_detailed_history

"""
Explanation: Step B: Extend Dataset
Compute Salary increases per year.
End of explanation
"""


x = df_league_salary_detailed_history['Year']
plt.plot(x,df_league_salary_detailed_history["alAnnualSalaryIncrease"], label="American League")
plt.plot(x,df_league_salary_detailed_history["nlAnnualSalaryIncrease"], label="National League")
plt.title('MLB Year-to-Year Salary Deltas')
plt.xlabel('Year')
plt.ylabel('Average Salary')
xvalues = american_league_avg_annual_salary.index
plt.xticks(xvalues, rotation='vertical')
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.show()

"""
Explanation: Explore Results
Plot comparison between annual league salary increases per year.
End of explanation
"""


def create_season_opener_datetime_index(date_series):
    '''Return a series of datetime index objects for a series of date strings'''
    import datetime
    import pandas as pd
    dates = []
    for i in date_series:
        d = datetime.date(i,4,1)
        dates.append(d)
    return pd.DatetimeIndex(dates)

"""
Explanation: Observations

Between 1985 and 2002 the leagues on average tended to offer similiar annual salary increases.
After 2002 it would seem that the leagues were more reactionary as they tend to handle increases in opposite manners.

Question 3: Future Predictions
Can we predict future average salary increase per league?
Data Munging
Step A: Define Helper Methods
End of explanation
"""


season_opener_dates = create_season_opener_datetime_index(df_league_salary_detailed_history['Year'])
df_al_annual_salary_inc = pd.DataFrame(
                                df_league_salary_detailed_history["alAnnualSalaryIncrease"].tolist(),
                                index=season_opener_dates,
                                columns=["alAnnualSalaryIncrease"]
                            )
df_nl_annual_salary_inc = pd.DataFrame(
                                df_league_salary_detailed_history["nlAnnualSalaryIncrease"].tolist(),
                                index=season_opener_dates,
                                columns=["nlAnnualSalaryIncrease"]
                            )
print df_al_annual_salary_inc.head()
print df_nl_annual_salary_inc.head()

"""
Explanation: Step B: Perform ETL
We have several data structures we need to transform in order to perform predictive analytics. Most notably is the need to have our data structures indexed using a datetime object. Our original data contains only the year for a season but our algorithms require an actual date so we will assume April 1st for each year as the season opener.
End of explanation
"""


import statsmodels.api as sm
al_ar_model = sm.tsa.AR(df_al_annual_salary_inc)
al_ar_model_res = al_ar_model.fit()
print al_ar_model_res.params
nl_ar_model = sm.tsa.AR(df_nl_annual_salary_inc)
nl_ar_model_res = nl_ar_model.fit()
print nl_ar_model_res.params

predict_al_increases = al_ar_model_res.predict(start='1995-04-01', end='2025-01-01', dynamic=True)
predict_nl_increases = nl_ar_model_res.predict(start='1995-04-01', end='2025-01-01', dynamic=True)

"""
Explanation: Step C: Create Prediction Models
End of explanation
"""


plt.plot(predict_al_increases.index,predict_al_increases, label="American League")
plt.plot(predict_nl_increases.index,predict_nl_increases, label="National League")
plt.rcParams['xtick.major.pad']='15'
plt.title('MLB Predictive Annual Averages Salary Increase')
plt.xlabel('Year')
plt.ylabel('Average Salary Increases')
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.show()

"""
Explanation: Explore Results
Step A: Plot comparison between annual league growth predictions.
End of explanation
"""


data_to_compare = {'AL Prediction': predict_al_increases,
                   'AL Actual': df_al_annual_salary_inc.alAnnualSalaryIncrease,
                   'NL Prediction': predict_nl_increases,
                   'NL Actual': df_nl_annual_salary_inc.nlAnnualSalaryIncrease
                }
df_annual_growth_predictive_analysis = pd.DataFrame(data_to_compare)
ax = df_annual_growth_predictive_analysis.plot()
ax.set_ylabel('Average Salary Increases')
plt.rcParams['xtick.major.pad']='15'
plt.title('MLB Annual Salary Growth Predictive Analysis')
plt.xlabel('Year')
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.show()

"""
Explanation: Step B: Plot comparison between annual league salary increases per year and the predicted growth for each league.
End of explanation
"""


