Skip to content

Instantly share code, notes, and snippets.

View kperry2215's full-sized avatar

Kirsten Perry kperry2215

View GitHub Profile
#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'])
#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'])
#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)
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
"""
#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)
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
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
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
import eia
import pandas as pd
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
import eia
import pandas as pd
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