<jupyter_start><jupyter_text>Simple example with yahoo finance grabbing microsoft prices from yahoo finance api documentation. There are gaps in the index so we are just resetting the index to allow fitting. Better to fill gaps or do something else!<jupyter_code>import yfinance as yf
msft = yf.Ticker("MSFT")

# get historical market data
hist = msft.history(period="max")

current_df = hist.iloc[-900:-100].reset_index()
future_df = hist.iloc[-100:].reset_index()

y_train = current_df['High']
X_train = current_df[['Volume', 'Stock Splits']]

y_test = future_df['High']
X_test = future_df[['Volume', 'Stock Splits']]<jupyter_output><empty_output><jupyter_text>First we will do a simple linear regression:<jupyter_code>boosted_model = tb.ThymeBoost(verbose=0)
output = boosted_model.fit(y_train,
                           trend_estimator='linear',
                           global_cost='mse',
                           fit_type='global',
                           )
predicted_output = boosted_model.predict(output, 100)
boosted_model.plot_results(output, predicted_output)<jupyter_output><empty_output><jupyter_text>Nothing too fancy!Next let's try out a polynomial regression. Just pass the polynomial order to 'poly':<jupyter_code>boosted_model = tb.ThymeBoost(verbose=0)
output = boosted_model.fit(y_train,
                           trend_estimator='linear',
                           poly=2,
                           global_cost='mse',
                           fit_type='global',
                           )
predicted_output = boosted_model.predict(output, 100)
boosted_model.plot_results(output, predicted_output)<jupyter_output><empty_output><jupyter_text>And now for a changepoint, simply change the fit_type to local!<jupyter_code>boosted_model = tb.ThymeBoost(verbose=0)
output = boosted_model.fit(y_train,
                           trend_estimator='linear',
                           poly=1,
                           global_cost='mse',
                           fit_type='local',
                           )
predicted_output = boosted_model.predict(output, 100)
boosted_model.plot_results(output, predicted_output)<jupyter_output><empty_output><jupyter_text>There are several considerations when doing changepoint detection, looks like here we are reacting too much at the end point. Let's try changing the cost function from 'mse' to a cost which takes into account the overall complexity of the model. 'maicc' (Modified AICC will get the job done):<jupyter_code>boosted_model = tb.ThymeBoost(verbose=0)
output = boosted_model.fit(y_train,
                           trend_estimator='linear',
                           poly=1,
                           global_cost='maicc',
                           fit_type='local',
                           )
predicted_output = boosted_model.predict(output, 100)
boosted_model.plot_results(output, predicted_output)<jupyter_output><empty_output><jupyter_text>Another aspect of changepoint detection involves the number of 'splits' and how we propose splits. By default ThymeBoost will propose 10 splits based on the gradient, we can change this when building the class:<jupyter_code>boosted_model = tb.ThymeBoost(verbose=0,
                             approximate_splits=True,
                              n_split_proposals=50,
                             split_strategy='gradient')
output = boosted_model.fit(y_train,
                           trend_estimator='linear',
                           poly=1,
                           global_cost='maicc',
                           fit_type='local',
                           )
predicted_output = boosted_model.predict(output, 100)
boosted_model.plot_results(output, predicted_output)<jupyter_output><empty_output><jupyter_text>By increasing the number of proposals it looks like we can get more reasonable results. This obviously comes at the cost of more computations. An alternative splitting strategy is based on a histogram algorithm.<jupyter_code>boosted_model = tb.ThymeBoost(verbose=0,
                             approximate_splits=True,
                             split_strategy='histogram')
output = boosted_model.fit(y_train,
                           trend_estimator='linear',
                           poly=1,
                           global_cost='maicc',
                           fit_type='local',
                           )
predicted_output = boosted_model.predict(output, 100)
boosted_model.plot_results(output, predicted_output)<jupyter_output><empty_output><jupyter_text>Here we propose each split based on a histogram algorithm, this will ignore the split_proposal argument. Looks similar to a high number of proposals and using a gradient strategy but they can be drastically different! Finally, if we pass approximate_splits=False it will exhaustively try out every data point to split the data on and ignore split_strategy and n_split_proposals.<jupyter_code>boosted_model = tb.ThymeBoost(verbose=0,
                             approximate_splits=False)
output = boosted_model.fit(y_train,
                           trend_estimator='linear',
                           poly=1,
                           global_cost='maicc',
                           fit_type='local',
                           )
predicted_output = boosted_model.predict(output, 100)
boosted_model.plot_results(output, predicted_output)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07616753150481838, 'fraction_numerical': 0.008710155670867309, 'mean_word_length': 2.509102730819246, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '363198', 'license': 'MIT', 'n_tokens_mistral': 1508, 'n_tokens_neox': 1451, 'n_words': 356, 'path': 'Examples/Linear Changepoint Model.ipynb', 'repo_name': 'tblume1992/ThymeBoost_v0', 'size': '5396'}