Skip to content

Instantly share code, notes, and snippets.

View marcosan93's full-sized avatar

Marco Santos marcosan93

View GitHub Profile
# Making a comparison DF
compare = pd.DataFrame()
compare['actual'] = y_test[col_name].reset_index(
drop=True
)
compare['preds'] = preds
# Check to see if the predictions at least point in the correct direction
def transformData(df, days=1):
"""
Transforming data into X variables for training. Uses percent change and
multiplies the percentage by 100 rounded to 2 decimal places.
"""
# Transforming data
new_df = df.pct_change(
days
).apply(
lambda x: round(x*100, 2)
def getCryptoPrice(api_key, ticker="BTC-USD", n_days=1000):
# Time periods
now = datetime.now()
# How far back to retrieve
ago = now - timedelta(days=n_days)
# Getting the price history for the crypto
df = get_eod_data(
def matchMan(men_df, women_df, ratings, new_man_answers, num_sim=10):
"""
This function will return the most likely compatible women based on a few given
dataframes for a new male user. Will use the top N similar users' compatibility
ratings to find the potentially most compatible women.
"""
# First need to replace the DF answers with their numerical values
men_df = men_df.apply(lambda x: x.cat.codes)
# Creating a Dataset of men and women
men = pd.DataFrame()
women = pd.DataFrame()
# Number of users
num = 1000
# Dating profile questions for each
qs = ['Q1', 'Q2', 'Q3', 'Q4', 'Q5']
def fbpBacktest(df, short=True):
"""
Performs the final backtest using log returns and the positions function.
Returns the performance.
"""
# Getting positions
df['positions'] = df.apply(lambda x: get_prophet_positions(x, short=short), axis=1)
# Compensating for lookahead bias
df['positions'] = df['positions'].shift(1)
def get_prophet_positions(df, short=True):
"""
For these positions, buy when actual value is above the upper bound and short
when actual value is below lower bound. Otherwise do nothing.
"""
if df['Open'] >= df['yhat_upper']:
return 1
elif df['Open'] <= df['yhat_lower'] and short:
return -1
else:
def runningFBP(ticker, window=730, ma_period=5, days_to_train=365, forecast_period=10):
"""
Runs the facebook prophet model over the provided ticker. Trains with last N days given
by days_to_train. Forecast N days into the future based on given forecast_period. Moving average
is applied to the dataset based on given ma_period. Returns the root mean squared error and a DF
of the actual values and the predicted values for the same day.
"""
# Getting and Formatting Data
df = getData(ticker, window=window, ma_period=ma_period)
def visFBP(df, forecast):
"""
Given two dataframes: before training df and a forecast df, returns
a visual chart of the predicted values and actual values.
"""
# Visual DF
vis_df = df[['ds','Open']].append(forecast).rename(
columns={'yhat': 'Prediction',
'yhat_upper': "Predicted High",
'yhat_lower': "Predicted Low"}
def fbpTrainPredict(df, forecast_period):
"""
Uses FB Prophet and fits to a appropriately formatted DF. Makes a prediction N days into
the future based on given forecast period. Returns predicted values as a DF.
"""
# Setting up prophet
m = Prophet(
daily_seasonality=True,
yearly_seasonality=True,
weekly_seasonality=True