Skip to main content

Bootstrap Average Hierarchical Clustering for filtering covariance matrices

Project description

BAHC

BAHC (Bootstrap Aggregated 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.filtered_matrix)

Methods

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

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').

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, N, T, epsilon=1e-10)

Generates noise for the given dimensions.

  • N (int): Number of rows.
  • T (int): Number of columns.
  • 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bahc-2.0.2.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

bahc-2.0.2-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file bahc-2.0.2.tar.gz.

File metadata

  • Download URL: bahc-2.0.2.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for bahc-2.0.2.tar.gz
Algorithm Hash digest
SHA256 8b8d4c494938a6fca8d8d62b67c39d6fd14761bdc8a34f4b3ad5bd2185aff6ab
MD5 af7f93d238bad7f4882b65b0c5e96d49
BLAKE2b-256 b9181cb7382e598ac9acb2ab6dba90a6516165950c7bb56364e77849e61f1031

See more details on using hashes here.

File details

Details for the file bahc-2.0.2-py3-none-any.whl.

File metadata

  • Download URL: bahc-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for bahc-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cfa46c23a1a71f48994d5d0e4b50e52c55b3e9a718641971f2082c3e74feb2ad
MD5 57bc69057929779967f6f9db89dbae85
BLAKE2b-256 2758af860b0f5505fe88f4fbecefd7258f5effe3bbcc73159b6db2007d6c0277

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page