This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def calculate_model_accuracy_metrics(actual, predicted): | |
""" | |
Output model accuracy metrics, comparing predicted values | |
to actual values. | |
Arguments: | |
actual: list. Time series of actual values. | |
predicted: list. Time series of predicted values | |
Outputs: | |
Forecast bias metrics, mean absolute error, mean squared error, | |
and root mean squared error in the console |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def calculate_model_accuracy_metrics(actual, predicted): | |
""" | |
Output model accuracy metrics, comparing predicted values | |
to actual values. | |
Arguments: | |
actual: list. Time series of actual values. | |
predicted: list. Time series of predicted values | |
Outputs: | |
Forecast bias metrics, mean absolute error, mean squared error, | |
and root mean squared error in the console |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Convert the dataframe to a numpy array | |
master_array=np.array(master_df[['Electricity_Price_Transformed_Differenced', | |
'Nat_Gas_Price_MCF_Transformed_Differenced']].dropna()) | |
#Generate a training and test set for building the model: 95/5 split | |
training_set = master_array[:int(0.95*(len(master_array)))] | |
test_set = master_array[int(0.95*(len(master_array))):] | |
#Fit to a VAR model | |
model = VAR(endog=training_set) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def augmented_dickey_fuller_statistics(time_series): | |
""" | |
Run the augmented Dickey-Fuller test on a time series | |
to determine if it's stationary. | |
Arguments: | |
time_series: series. Time series that we want to test | |
Outputs: | |
Test statistics for the Augmented Dickey Fuller test in | |
the console | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Transform the columns using natural log | |
master_df['Electricity_Price_Transformed']=np.log(master_df['Electricity_Price']) | |
master_df['Nat_Gas_Price_MCF_Transformed']=np.log(master_df['Nat_Gas_Price_MCF']) | |
#Difference the data by 1 month | |
n=1 | |
master_df['Electricity_Price_Transformed_Differenced'] = master_df['Electricity_Price_Transformed'] - master_df['Electricity_Price_Transformed'].shift(n) | |
master_df['Nat_Gas_Price_MCF_Transformed_Differenced'] = master_df['Nat_Gas_Price_MCF_Transformed'] - master_df['Nat_Gas_Price_MCF_Transformed'].shift(n) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Pull in natural gas time series data | |
series_ID='NG.N3035TX3.M' | |
nat_gas_df=retrieve_time_series(api, series_ID) | |
nat_gas_df.reset_index(level=0, inplace=True) | |
#Rename the columns | |
nat_gas_df.rename(columns={'index':'Date', | |
nat_gas_df.columns[1]:'Nat_Gas_Price_MCF'}, | |
inplace=True) | |
#Convert the Date column into a date object | |
nat_gas_df['Date']=pd.to_datetime(nat_gas_df['Date']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Pull in natural gas time series data | |
series_ID='NG.N3035TX3.M' | |
nat_gas_df=retrieve_time_series(api, series_ID) | |
nat_gas_df.reset_index(level=0, inplace=True) | |
#Rename the columns | |
nat_gas_df.rename(columns={'index':'Date', | |
nat_gas_df.columns[1]:'Nat_Gas_Price_MCF'}, | |
inplace=True) | |
#Convert the Date column into a date object | |
nat_gas_df['Date']=pd.to_datetime(nat_gas_df['Date']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def retrieve_time_series(api, series_ID): | |
""" | |
Return the time series dataframe, based on API and unique Series ID | |
api: API that we're connected to | |
series_ID: string. Name of the series that we want to pull from the EIA API | |
""" | |
#Retrieve Data By Series ID | |
series_search = api.data_by_series(series=series_ID) | |
##Create a pandas dataframe from the retrieved time series | |
df = pd.DataFrame(series_search) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def decompose_time_series(series): | |
""" | |
Decompose a time series and plot it in the console | |
Arguments: | |
series: series. Time series that we want to decompose | |
Outputs: | |
Decomposition plot in the console | |
""" | |
result = seasonal_decompose(series, model='additive') | |
result.plot() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def retrieve_time_series(api, series_ID): | |
""" | |
Return the time series dataframe, based on API and unique Series ID | |
api: API that we're connected to | |
series_ID: string. Name of the series that we want to pull from the EIA API | |
""" | |
#Retrieve Data By Series ID | |
series_search = api.data_by_series(series=series_ID) | |
##Create a pandas dataframe from the retrieved time series | |
df = pd.DataFrame(series_search) |