Skip to main content

BAHC

BAHC (Bootstrap Average Hierarchical Clustering) is a Python package for filtering covariance matrices using hierarchical clustering and bootstrap aggregation.

Installation

To install the package, you can use pip:

pip install bahc

Alternatively, you can clone the repository and install the dependencies manually:

git clone https://github.com/yourusername/bahc.git
cd bahc
pip install -r requirements.txt

Usage

Here is an example of how to use the BAHC class:

import numpy as np
from bahc import BAHC

# Generate random data
data = np.random.normal(0, 1, size=(10, 100))

# Create an instance of BAHC and filter the data
bahc_instance = BAHC(data, K=[1, 2, 3], Nboot=100, method='near', filter_type='correlation')

# Print the filtered matrix
print(bahc_instance.filter_matrix())

Methods

BAHC.__init__(self, data, K=1, Nboot=100, method='near', filter_type='covariance', seed=None)

Initializes the BAHC class and performs the filtering.

  • data (ndarray): Data matrix (N x T).
  • K (int or list): Recursion order.
  • Nboot (int): Number of bootstraps.
  • method (str): Regularization method ('no-neg' or 'near').
  • filter_type (str): Type of filtering ('correlation' or 'covariance').
  • seed (int): Random seed.

BAHC.filter_matrix(self)

Filters the matrix using k-BAHC.

  • Returns: Filtered matrix (N x N). If K is a list, returns a list of matrices.

BAHC.nearest_positive_semidefinite(self, matrix, n_iter=100, eig_tol=1e-6, conv_tol=1e-8)

Finds the nearest positive semidefinite matrix.

  • matrix (ndarray): Input matrix.
  • n_iter (int): Maximum number of iterations.
  • eig_tol (float): Eigenvalue tolerance.
  • conv_tol (float): Convergence tolerance.
  • Returns: Nearest positive semidefinite matrix.

BAHC._higher_order_covariance(self, C, K)

Computes higher order covariance matrices.

  • C (ndarray): Input covariance matrix.
  • K (list): List of recursion orders.
  • Yields: Higher order covariance matrices.

BAHC._generate_noise(self, epsilon=1e-10)

Generates noise for the given dimensions.

  • epsilon (float): Standard deviation of the noise.
  • Returns: Generated noise matrix.

BAHC._remove_negative_eigenvalues(self, matrix)

Removes negative eigenvalues from the matrix.

  • matrix (ndarray): Input matrix.
  • Returns: Matrix with non-negative eigenvalues.

References

  • Bongiorno, C., & Challet, D. (2021). Covariance matrix filtering with bootstrapped hierarchies. PLoS ONE, 16(1), e0245092.
  • Bongiorno, C., & Challet, D. (2022). Reactive global minimum variance portfolios with k-BAHC covariance cleaning. The European Journal of Finance, 28(13-15), 1344-1360.

Please cite the above papers if you use this code.

Release files for bahc 2.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bahc 2.0.3
File Size Uploaded
bahc-2.0.3.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bahc 2.0.3
File Interpreter ABI Platform
bahc-2.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 9.4 kB

Release files / bahc-2.0.3.tar.gz

Download URL bahc-2.0.3.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
2ae84164fcc31d6a0320d20ad17ffc872dd512ceca88a38b0304efc4de764ff6
BLAKE2b-256 checksum
How to use checksums
031a7b07d01a86de4f6b5d465098ffe0809ea5e21fec7e1403e9a57dc19fb956
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.3

Release files / bahc-2.0.3-py3-none-any.whl

Download URL bahc-2.0.3-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
34035824dbf33d76d67d2abb7af9f28b6b99e7189d253d068345aef3a03cceb3
BLAKE2b-256 checksum
How to use checksums
97518185766c730f8a5f95f28aa9218cafa904427d9f628553e19ff3ca2b2e78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

2.0.3 This release

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.9

1 release file

1.8

1 release file

1.7

1 release file

1.6

1 release file

1.5

1 release file

1.4

1 release file

1.3

1 release file

1.2

1 release file

1.1

1 release file

1.0

1 release file

0.9

1 release file

0.8

1 release file

0.7

1 release file

0.6.1

1 release file

0.6

1 release file

0.5

1 release file

0.4

1 release file

0.3

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page