From f92f933b5d1ff5a5b1c9ea9567f4dd2338b381ea Mon Sep 17 00:00:00 2001 From: Masimba007 Date: Fri, 28 Jul 2023 07:17:27 +0200 Subject: [PATCH 1/4] ML Approach example Fix --- docs/source/ml_approach/filters.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/source/ml_approach/filters.rst b/docs/source/ml_approach/filters.rst index 93ee66db..9d907a67 100644 --- a/docs/source/ml_approach/filters.rst +++ b/docs/source/ml_approach/filters.rst @@ -84,7 +84,9 @@ Example from arbitragelab.ml_approach.filters import ThresholdFilter # Getting the dataframe with time series of asset returns - data = pd.read_csv('X_FILE_PATH.csv', index_col=0, parse_dates = [0]) + url = "https://raw.githubusercontent.com/hudson-and-thames/example-data/main/arbitrage_lab_data/CL%3DF_NG%3DF_data.csv" + data = pd.read_csv(url, index_col=0, parse_dates = [0]) + data['spread'] = (data['CL=F'] - data['NG=F']) # Calculating spread returns and std dev. spread_series = data['spread'] From 42ce692dcdbd04499785222b3085ff3f5ea42a91 Mon Sep 17 00:00:00 2001 From: Masimba007 Date: Fri, 28 Jul 2023 07:38:03 +0200 Subject: [PATCH 2/4] Filters doctest fix --- docs/source/ml_approach/filters.rst | 80 +++++++++++++++-------------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/docs/source/ml_approach/filters.rst b/docs/source/ml_approach/filters.rst index 9d907a67..7ddabec0 100644 --- a/docs/source/ml_approach/filters.rst +++ b/docs/source/ml_approach/filters.rst @@ -77,30 +77,30 @@ Implementation Example ******* -.. code-block:: +.. doctest:: - # Importing packages - import pandas as pd - from arbitragelab.ml_approach.filters import ThresholdFilter + >>> import pandas as pd + >>> from arbitragelab.ml_approach.filters import ThresholdFilter - # Getting the dataframe with time series of asset returns - url = "https://raw.githubusercontent.com/hudson-and-thames/example-data/main/arbitrage_lab_data/CL%3DF_NG%3DF_data.csv" - data = pd.read_csv(url, index_col=0, parse_dates = [0]) - data['spread'] = (data['CL=F'] - data['NG=F']) + >>> # Getting the dataframe with time series of asset returns + >>> url = "https://raw.githubusercontent.com/hudson-and-thames/example-data/main/arbitrage_lab_data/CL%3DF_NG%3DF_data.csv" + >>> data = pd.read_csv(url, index_col=0, parse_dates = [0]) + >>> data['spread'] = (data['CL=F'] - data['NG=F']) - # Calculating spread returns and std dev. - spread_series = data['spread'] - spread_diff_series = spread_series.diff() - spread_diff_std = spread_diff_series.std() + >>> # Calculating spread returns and std dev. + >>> spread_series = data['spread'] + >>> spread_diff_series = spread_series.diff() + >>> spread_diff_std = spread_diff_series.std() - # Initializing ThresholdFilter with 2 std dev band for buying and selling triggers. - thres_filter = ThresholdFilter(buy_threshold=-spread_diff_std*2, - sell_threshold=spread_diff_std*2) + >>> # Initializing ThresholdFilter with 2 std dev band for buying and selling triggers. + >>> thres_filter = ThresholdFilter(buy_threshold=-spread_diff_std*2, + ... sell_threshold=spread_diff_std*2) - std_events = thres_filter.fit_transform(spread_diff_series) + >>> std_events = thres_filter.fit_transform(spread_diff_series) - # Plotting results - thres_filter.plot() + >>> # Plotting results + >>> thres_filter.plot()# doctest: +ELLIPSIS + (...) Asymmetric Threshold Filter ########################### @@ -119,35 +119,37 @@ being coefficients estimated from the TAR model. Example ******* -.. code-block:: +.. doctest:: - # Importing packages - import pandas as pd - from arbitragelab.ml_approach.filters import ThresholdFilter + >>> import pandas as pd + >>> from arbitragelab.ml_approach.filters import ThresholdFilter - # Getting the dataframe with time series of asset returns - data = pd.read_csv('X_FILE_PATH.csv', index_col=0, parse_dates = [0]) + >>> # Getting the dataframe with time series of asset returns + >>> url = "https://raw.githubusercontent.com/hudson-and-thames/example-data/main/arbitrage_lab_data/CL%3DF_NG%3DF_data.csv" + >>> data = pd.read_csv(url, index_col=0, parse_dates = [0]) + >>> data['spread'] = (data['CL=F'] - data['NG=F']) - # Calculate spread returns and std dev. - spread_series = data['spread'] - spread_diff_series = spread_series.diff() - spread_diff_std = spread_diff_series.std() + >>> # Calculate spread returns and std dev. + >>> spread_series = data['spread'] + >>> spread_diff_series = spread_series.diff() + >>> spread_diff_std = spread_diff_series.std() - # Initialize the TAR asymmetry coefficients. - p_1 = -0.012957 - p_2 = -0.038508 + >>> # Initialize the TAR asymmetry coefficients. + >>> p_1 = -0.012957 + >>> p_2 = -0.038508 - buy_thresh = abs(p_1) * spread_diff_std*2 - sell_thresh = - abs(p_2) * spread_diff_std*2 + >>> buy_thresh = abs(p_1) * spread_diff_std*2 + >>> sell_thresh = - abs(p_2) * spread_diff_std*2 - # Initialize ThresholdFilter with asymmetric buying and selling triggers. - asym_thres_filter = ThresholdFilter(buy_threshold=buy_thresh, - sell_threshold=sell_thresh) + >>> # Initialize ThresholdFilter with asymmetric buying and selling triggers. + >>> asym_thres_filter = ThresholdFilter(buy_threshold=buy_thresh, + ... sell_threshold=sell_thresh) - std_events = asym_thres_filter.fit_transform(spread_diff_series) + >>> std_events = asym_thres_filter.fit_transform(spread_diff_series) - # Plotting results - asym_thres_filter.plot() + >>> # Plotting results + >>> asym_thres_filter.plot()# doctest: +ELLIPSIS + (...) Correlation Filter ################## From 3ea77a83cb8950f54612740d35a70d3207cd8b18 Mon Sep 17 00:00:00 2001 From: Masimba007 Date: Fri, 28 Jul 2023 07:51:00 +0200 Subject: [PATCH 3/4] Doctest filters examples fix --- docs/source/ml_approach/filters.rst | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/docs/source/ml_approach/filters.rst b/docs/source/ml_approach/filters.rst index 7ddabec0..8c95469b 100644 --- a/docs/source/ml_approach/filters.rst +++ b/docs/source/ml_approach/filters.rst @@ -281,22 +281,23 @@ Implementation Example ******* -.. code-block:: +.. doctest:: - # Importing packages - import pandas as pd - from arbitragelab.ml_approach.filters import VolatilityFilter + >>> import pandas as pd + >>> from arbitragelab.ml_approach.filters import VolatilityFilter - # Getting the dataframe with time series of asset returns - data = pd.read_csv('X_FILE_PATH.csv', index_col=0, parse_dates = [0]) - spread_series = data['spread'] + >>> # Getting the dataframe with time series of asset returns + >>> url = "https://raw.githubusercontent.com/hudson-and-thames/example-data/main/arbitrage_lab_data/CL%3DF_NG%3DF_data.csv" + >>> data = pd.read_csv(url, index_col=0, parse_dates = [0]) + >>> data['spread'] = (data['CL=F'] - # Initialize VolatilityFilter a 30 period lookback parameter. - vol_filter = VolatilityFilter(lookback=30) - vol_events = vol_filter.fit_transform(spread_series) + >>> # Initialize VolatilityFilter a 30 period lookback parameter. + >>> vol_filter = VolatilityFilter(lookback=30) + >>> vol_events = vol_filter.fit_transform(spread_series) - # Plotting results - vol_filter.plot() + >>> # Plotting results + >>> vol_filter.plot() # doctest: +ELLIPSIS + (...) Presentation Slides ################### From 8a1e4dd466e70427dd0a9ca5a16a840bd7285f2b Mon Sep 17 00:00:00 2001 From: Masimba007 Date: Fri, 28 Jul 2023 08:00:26 +0200 Subject: [PATCH 4/4] Doctest filters example fix --- docs/source/ml_approach/filters.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/source/ml_approach/filters.rst b/docs/source/ml_approach/filters.rst index 8c95469b..d13d3d76 100644 --- a/docs/source/ml_approach/filters.rst +++ b/docs/source/ml_approach/filters.rst @@ -289,7 +289,8 @@ Example >>> # Getting the dataframe with time series of asset returns >>> url = "https://raw.githubusercontent.com/hudson-and-thames/example-data/main/arbitrage_lab_data/CL%3DF_NG%3DF_data.csv" >>> data = pd.read_csv(url, index_col=0, parse_dates = [0]) - >>> data['spread'] = (data['CL=F'] + >>> data['spread'] = (data['CL=F'] - data['NG=F']) + >>> spread_series = data['spread'] >>> # Initialize VolatilityFilter a 30 period lookback parameter. >>> vol_filter = VolatilityFilter(lookback=30)