torchvinecopulib

repository·main·Indexed 42 days ago

https://github.com/ty-cheng/torchvinecopulib

A PyTorch-based library for fitting and sampling vine copulas, providing vectorized implementations of C, D, and R-Vine models with GPU acceleration. Version 1.1.2 supports full-sampling, quantile-regression, and conditional-sampling, featuring optimized runtimes for high-dimensional simulations and visualization tools for vine structures.

Tokens
9.1K
Snippets
35
Records
39
Agent score
53%

What's inside torchvinecopulib

  1. Overview of torchvinecopulib

    main

    torchvinecopulib is a Python library for fitting and sampling vine copulas using PyTorch. It supports C/D/R-Vine full-sampling, quantile-regression, and conditional-sampling within a single package.

    Key features include:

    • Flexible sampling orders for advanced users.
    • Vectorized tensor computation with GPU (device='cuda') support.
    • Optimized runtimes for high-dimensional simulations.
    • IO and visualization support.
    • Pure Python implementation inspired by pyvinecopulib.
  2. Manage dependencies with uv (Recommended)

    main

    For optimal dependency management, it is recommended to use uv. After cloning the repository, navigate to the project root and follow these steps to set up a virtual environment and sync dependencies based on your hardware requirements.

    Note on CUDA support:

    • Use --extra cpu for default CPU support.
    • Use --extra cu126 for CUDA 12.6 support.
    • Use --extra cu128 for CUDA 12.8 support.
    • Use --extra examples to install additional dependencies required for the Jupyter notebooks in the ./examples/ folder.
    # From inside the project root folder
    # Create and activate local virtual environment
    uv venv .venv
    source .venv/bin/activate
    
    # Sync dependencies with CPU support (default)
    uv sync --extra cpu
    
    # Or for CUDA 12.6 or 12.8 support (depends on your CUDA version)
    uv sync --extra cu126
    
    # Additionally, to install additional dependencies for the examples
    uv sync --extra examples
  3. Convert pyvinecopulib models to torchvinecopulib

    main

    If you have a fitted model from pyvinecopulib (pvc), you can transfer its structure (the vine matrix) to a torchvinecopulib (tvc) model. This is useful for leveraging GPU acceleration or integrating with PyTorch.

    Workflow:

    1. Convert data to pseudo-observations using pvc.to_pseudo_obs().
    2. Fit a pvc.Vinecop using pvc.Vinecop.from_data().
    3. Extract the matrix from the pvc.Vinecop object.
    4. Initialize a tvc.VineCop and pass the matrix to the .fit() method.
    import pyvinecopulib as pvc
    import torchvinecopulib as tvc
    
    # 1. Fit PVC model
    controls = pvc.FitControlsVinecop(family_set=[pvc.BicopFamily.tll])
    vine_pvc = pvc.Vinecop.from_data(u_cls, controls=controls)
    
    # 2. Extract matrix and convert to torch format
    # Subtract 1 because PVC uses 1-based indexing for the matrix
    tvc_mat = torch.tensor(
        np.array(vine_pvc.matrix, dtype=np.int32),
        dtype=torch.int32,
        device=DEVICE
    ).rot90(k=3) - 1
    
    # 3. Fit TVC model using the PVC structure
    vine_tvc = tvc.VineCop(
        num_dim=u_cls.shape[1],
        is_cop_scale=True,
        num_step_grid=30,
    ).to(DEVICE)
    
    vine_tvc.fit(
        obs=u_cls_tensor,
        matrix=tvc_mat,
        is_dissmann=False,
        mtd_kde="tll",
    )
  4. Integrate torchvinecopulib into a PyTorch Lightning module

    main

    You can use torchvinecopulib.VineCop within a pytorch_lightning.LightningModule to regularize the latent space of an autoencoder. By adding the log-likelihood of the latent representations (calculated via the vine copula) to the loss function, you can encourage the latent space to follow a specific dependency structure.

    Key steps:

    1. Define a set_vine method to inject a fitted tvc.VineCop into your module.
    2. In training_step, calculate the loss as: MSE_loss(reconstruction, original) - vine.log_pdf(latent).mean().
    3. Ensure the vine_type matches the object type (e.g., tvc.VineCop for tvc type).
    import torchvinecopulib as tvc
    import pytorch_lightning as pl
    
    class LitAutoencoder(pl.LightningModule):
        def __init__(self, use_vine=False, vine_type="tvc", ...):
            super().__init__()
            self.use_vine = use_vine
            self.vine_type = vine_type
            self.vine = None
    
        def set_vine(self, vine):
            if self.vine_type == "tvc":
                if not isinstance(vine, tvc.VineCop):
                    raise ValueError("Vine must be of type tvc.VineCop for tvc.")
            self.vine = vine
    
        def training_step(self, batch, batch_idx):
            x, _ = batch
            z = self.encoder(x)
            x_hat = self.decoder(z)
            
            if self.use_vine:
                # Regularize latent space using the vine copula log-pdf
                loss = F.mse_loss(x_hat, x) - self.vine.log_pdf(z).mean()
            else:
                loss = F.mse_loss(x_hat, x)
            return loss
  5. Configure PyTorch for CUDA support

    main

    If you need to ensure PyTorch is correctly configured for CUDA (e.g., version 12.6), use the following command. After installation, verify CUDA availability using the provided Python one-liner.

    pip install torch --index-url https://download.pytorch.org/whl/cu126 --force-reinstall
    # check cuda availability
    python -c "import torch; print(torch.cuda.is_available())"
  6. Install torchvinecopulib via pip

    main

    You can install the package and its core dependency torch directly from PyPI using pip.

    To install with CUDA support, ensure you follow the specific PyTorch installation instructions for your CUDA version (e.g., cu126) to ensure compatibility.

    pip install torchvinecopulib torch
  7. Install torchvinecopulib from local distribution files

    main

    If you have the .whl or .tar.gz files from the ./dist/ directory, you can install them directly using pip. Ensure you use the exact filename available in your local directory.

    # inside project root folder
    pip install ./dist/torchvinecopulib-1.1.0-py3-none-any.whl
    # or
    pip install ./dist/torchvinecopulib-1.1.0.tar.gz
  8. Install torchvinecopulib and dependencies

    main

    To set up the environment for using torchvinecopulib, it is recommended to use uv for dependency management. You can create a conda environment and then use uv pip to install the package with CUDA support.

    # Create and activate environment
    mamba create -n torchvinecopulib python=3.12 -y
    mamba activate torchvinecopulib
    
    # Install uv
    mamba install -c conda-forge uv
    
    # Install torchvinecopulib with CUDA 12.4 support
    uv pip install -e ".[cu124]"
    
    # Install additional training dependencies
    uv pip install lightning torchvision ipykernel seaborn
    mamba create -n torchvinecopulib python=3.12 -y
    mamba activate torchvinecopulib
    mamba install -c conda-forge uv
    uv pip install -e ".[cu124]"
    uv pip install lightning torchvision ipykernel seaborn
  9. Install dependencies using uv

    main

    The project uses uv for dependency management. You can install dependencies for either CPU-only environments or CUDA-enabled environments using the following commands from the project root folder.

    For CPU only:

    uv sync --extra cpu -U

    For CUDA support (e.g., CUDA 12.6):

    uv sync --extra cu126 -U
    # cpu only
    uv sync --extra cpu -U
    # or with cuda
    uv sync --extra cu126 -U