Skip to content

Commit

Permalink
Merge pull request #82 from hudson-and-thames/develop
Browse files Browse the repository at this point in the history
Release for 0.8.0
  • Loading branch information
Michael Struwig authored Jun 27, 2023
2 parents f04fd53 + c63ae58 commit 3601490
Show file tree
Hide file tree
Showing 34 changed files with 455 additions and 374 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.7.0
current_version = 0.8.0
commit = True
tag = True
tag_name = {new_version}
Expand Down
15 changes: 8 additions & 7 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
version: 2.1

workflows:
version: 2
main:
jobs:
- run_tests

jobs:
run_tests:
machine:
image: ubuntu-2004:202010-01
resource_class: medium
docker:
- image: circleci/python:3.8
steps:
# Step 1: obtain repo from GitHub
- checkout
Expand Down Expand Up @@ -34,7 +39,3 @@ jobs:
- store_artifacts:
path: test-reports

workflows:
main:
jobs:
- run_tests
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ omit =
arbitragelab/util/segment.py
tests/*
/opt/conda/*
venv/*
# Ensure we exclude any files in .local
*/.local/*
*/tmp/*
Expand Down
11 changes: 6 additions & 5 deletions arbitragelab/cointegration_approach/johansen.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,13 @@ def fit(self, price_data: pd.DataFrame, dependent_variable: str = None, det_orde
hedge_ratios = cointegration_vectors.iloc[vector].to_dict()
for ticker, ratio in hedge_ratios.items():
if ticker != dependent_variable:
hedge_ratios[ticker] = -ratio / hedge_ratios[dependent_variable]
hedge_ratios[dependent_variable] = 1.0
# Set value to be list to make it easier to read into pandas DataFrame
hedge_ratios[ticker] = [-ratio / hedge_ratios[dependent_variable]]
# Set value to be list to make it easier to read into pandas DataFrame
hedge_ratios[dependent_variable] = [1.0]

# Add all to one dataframe
all_hedge_ratios = all_hedge_ratios.append(hedge_ratios, ignore_index=True)
all_hedge_ratios = all_hedge_ratios[price_data.columns]
# Concat together in one DataFrame
all_hedge_ratios = pd.concat([all_hedge_ratios, pd.DataFrame(hedge_ratios)])

self.hedge_ratios = all_hedge_ratios

Expand Down
2 changes: 1 addition & 1 deletion arbitragelab/cointegration_approach/minimum_profit.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def set_train_dataset(self, price_df: pd.DataFrame):
raise Exception("Data Format Error. Should only contain two price series.")

# Verify the index is indeed pd.DatetimeIndex
assert price_df.index.is_all_dates, "Index is not of pd.DatetimeIndex type."
assert isinstance(price_df.index, pd.DatetimeIndex), "Index is not of pd.DatetimeIndex type."

self.price_df = price_df

Expand Down
18 changes: 9 additions & 9 deletions arbitragelab/copula_approach/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

# pylint: disable = invalid-name, too-many-function-args
from abc import ABC, abstractmethod
from matplotlib.axes import Axes
from matplotlib.figure import Figure

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -202,7 +204,7 @@ def _get_param(self):
"""

@staticmethod
def _3d_surface_plot(x: np.array, y: np.array, z: np.array, bounds: list, title: str, **kwargs) -> plt.axis:
def _3d_surface_plot(x: np.array, y: np.array, z: np.array, bounds: list, title: str, **kwargs) -> Figure:
"""
Helper function to plot 3-d plot.
Expand All @@ -223,13 +225,12 @@ def _3d_surface_plot(x: np.array, y: np.array, z: np.array, bounds: list, title:
ax.set_ylim(bounds)
ax.plot_surface(x, y, z, **kwargs)
plt.title(title)
plt.show()

return ax
return fig

@staticmethod
def _2d_contour_plot(x: np.array, y: np.array, z: np.array, bounds: float, title: str,
levels: list, **kwargs) -> plt.axis:
levels: list, **kwargs) -> Figure:
"""
Helper function to plot 2-d contour plot.
Expand All @@ -243,15 +244,14 @@ def _2d_contour_plot(x: np.array, y: np.array, z: np.array, bounds: float, title
:return: (plt.axis) Axis object.
"""

plt.figure()
fig = plt.figure()
contour_plot = plt.contour(x, y, z, levels, colors='k', linewidths=1., linestyles=None, **kwargs)
plt.clabel(contour_plot, fontsize=8, inline=1)
plt.xlim(bounds)
plt.ylim(bounds)
plt.title(title)
plt.show()

return contour_plot
return fig

def plot_cdf(self, plot_type: str = '3d', grid_size: int = 50, levels: list = None, **kwargs) -> plt.axis:
"""
Expand Down Expand Up @@ -293,7 +293,7 @@ def plot_cdf(self, plot_type: str = '3d', grid_size: int = 50, levels: list = No

return ax

def plot_scatter(self, num_points: int = 100) -> plt.axis:
def plot_scatter(self, num_points: int = 100) -> Axes:
"""
Plot copula scatter plot of generated pseudo-observations.
Expand All @@ -307,7 +307,7 @@ def plot_scatter(self, num_points: int = 100) -> plt.axis:

return ax

def plot_pdf(self, plot_type: str = '3d', grid_size: int = 50, levels: list = None, **kwargs) -> plt.axis:
def plot_pdf(self, plot_type: str = '3d', grid_size: int = 50, levels: list = None, **kwargs) -> Figure:
"""
Plot either '3d' or 'contour' plot of copula PDF.
Expand Down
23 changes: 4 additions & 19 deletions arbitragelab/distance_approach/basic_distance_approach.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ def find_portfolios(data, pairs):
of the second element.
:param data: (pd.DataFrame) Dataframe with price series for elements.
:param pairs: (list) List of tuples with two elements to use for calculation.
:param pairs: (list) List of tuples with two str elements to use for calculation.
:return: (pd.DataFrame) Dataframe with pairs as columns and their portfolio
values as rows.
"""
Expand All @@ -504,20 +504,9 @@ def find_portfolios(data, pairs):

# Iterating through pairs
for pair in pairs:
# Getting two price series for elements in a pair
par = data[list(pair)]

# Difference between price series - a portfolio
par_diff = par.iloc[:, 0] - par.iloc[:, 1]

# Naming the portfolio
par_diff.name = str(pair)

# Adding portfolio series to dataframe
portfolios = portfolios.append(par_diff)

# Transposing to make portfolios as columns
portfolios = portfolios.transpose()
par_diff = data.loc[:, pair[0]] - data.loc[:, pair[1]]
portfolios[str(pair)] = par_diff

return portfolios

Expand Down Expand Up @@ -579,10 +568,6 @@ def signals(portfolios, variation, divergence):
portfolio['target_quantity'] = portfolio['long_units'] + portfolio['short_units']

# Adding target quantity to signals dataframe
signals = signals.append(portfolio['target_quantity'])

# Adjusting the final signals dataframe
signals = signals.transpose()
signals.columns = portfolios.columns
signals[str(pair)] = portfolio['target_quantity']

return signals
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ def _data_preprocess(self, price_data, risk_free, phase='train'):

else:
self.test_monthly_return = monthly_return
self.risk_free = self.risk_free.append(risk_free)
self.risk_free = pd.concat([self.risk_free, risk_free])

def _beta_pairs_formation(self, num_pairs, weight):
"""
Expand Down
28 changes: 7 additions & 21 deletions arbitragelab/ml_approach/neural_networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
logging.getLogger('tensorflow').setLevel(logging.ERROR)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'


# Importing needed packages
import tensorflow as tf
from keras.layers import Input, LSTM, Dense, Activation, Lambda
from keras.models import Model
import matplotlib.pyplot as plt

from arbitragelab.util import segment
Expand All @@ -27,12 +32,6 @@ def __init__(self):
Initializing variables.
"""

# Importing needed packages
import tensorflow as tf
from keras.models import Model
from keras.callbacks import History
from keras.layers import Input, LSTM, Dense, Activation, Lambda

self.fitted_model = None

def fit(self, *args, **kwargs):
Expand Down Expand Up @@ -109,10 +108,6 @@ def build(self):
:return: (Model) Resulting model.
"""

# Importing needed packages
from keras.models import Model
from keras.layers import Input, Dense

input_layer = Input((self.frame_size,))

hidden_layer = Dense(self.hidden_size,
Expand All @@ -130,6 +125,7 @@ def build(self):

return model


class RecurrentNeuralNetwork(BaseNeuralNetwork):
"""
Recurrent Neural Network implementation.
Expand Down Expand Up @@ -175,10 +171,6 @@ def build(self):
:return: (Model) Resulting model.
"""

# Importing needed packages
from keras.models import Model
from keras.layers import Input, LSTM, Dense

input_layer = Input(self.input_shape)

hidden_layer = LSTM(self.hidden_size, activation=self.hidden_layer_activation_function,
Expand All @@ -196,6 +188,7 @@ def build(self):

return model


class PiSigmaNeuralNetwork(BaseNeuralNetwork):
"""
Pi Sigma Neural Network implementation.
Expand Down Expand Up @@ -240,10 +233,6 @@ def build(self):
:return: (Model) Resulting model.
"""

# Importing needed packages
from keras.models import Model
from keras.layers import Input, Dense, Activation, Lambda

input_layer = Input((self.frame_size,))

second_sigma_layer = Dense(self.hidden_size,
Expand Down Expand Up @@ -272,9 +261,6 @@ def _pi_this(tensor):
:return: (tf.Tensor) Product of input tensor.
"""

# Importing needed packages
import tensorflow as tf

prod = tf.math.reduce_prod(tensor, keepdims=True, axis=1)

return prod
4 changes: 0 additions & 4 deletions arbitragelab/ml_approach/optics_dbscan_pairs_clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,6 @@ def plot_3d_scatter_plot(self, tsne_df: pd.DataFrame, no_of_classes: int,
# Set the chart title.
ax_object.set_title('Automatic Clustering\n' + method)

plt.show()

return ax_object

def plot_2d_scatter_plot(self, fig: Figure, tsne_df: pd.DataFrame, no_of_classes: int,
Expand Down Expand Up @@ -287,8 +285,6 @@ def plot_2d_scatter_plot(self, fig: Figure, tsne_df: pd.DataFrame, no_of_classes
# Set the chart title.
ax_object.set_title('Automatic Clustering\n' + method)

plt.show()

return ax_object

def plot_knee_plot(self) -> Axes:
Expand Down
Loading

0 comments on commit 3601490

Please sign in to comment.