Skip to content

Boiler-Quant/finmath

Repository files navigation

finmath

finmath is a high-performance financial mathematics library written in C++ with Python bindings using pybind11. The library includes various functions for calculating compound interest, option pricing (including Black-Scholes and Binomial Tree models), and time series analysis.

Key Features:

  • Cross-platform SIMD optimizations (ARM NEON, x86 AVX/SSE) for 2-4x speedups
  • Zero-copy NumPy integration for efficient memory usage
  • 34-308x faster than pure Python/NumPy implementations
  • Production-ready with comprehensive tests

Installation

Prerequisites

  • C++17 or later
  • Python 3.6 or later
  • pybind11 (for Python bindings)
  • CMake 3.10 or later (for building the library)

Build Instructions

  1. Clone the repository:

    git clone https://github.com/Boiler-Quant/finmath.git
    cd finmath
  2. Create a build directory:

    mkdir build
    cd build
  3. Run CMake:

    cmake ..
  4. Build the library:

    make
  5. Install the Python bindings (optional):

    pip install .

This will build the finmath library and create the Python bindings so that you can use finmath directly in Python.

Usage

Python

After installing the finmath library, you can import and use it in Python:

import finmath

# Example: Calculate compound interest
principal = 1000
rate = 5
time = 10
frequency = 4
result = finmath.compound_interest(principal, rate, time, frequency)
print(f"Compound Interest: {result}")

# Example: Calculate option price using Black-Scholes model
option_price = finmath.black_scholes(finmath.OptionType.CALL, 95, 100, 1, 0.05, 0.2)
print(f"Black-Scholes Option Price: {option_price}")

# Example: Calculate rolling volatility over a 22-day window
prices = [100, 101, 102, 100, 99, 98, 100, 102, 103, 104, 105]  # Example price series
vol = finmath.rolling_volatility(prices, 22)
print(f"Rolling Volatility: {vol}")

C++

If you want to use the library directly in C++:

#include "finmath/InterestAndAnnuities/compound_interest.h"
#include "finmath/OptionPricing/black_scholes.h"
#include "finmath/TimeSeries/rolling_volatility.h"
#include <iostream>
#include <vector>

int main() {
    double principal = 1000;
    double rate = 5;
    int time = 10;
    int frequency = 4;

    double compound_result = compound_interest(principal, rate, time, frequency);
    std::cout << "Compound Interest: " << compound_result << std::endl;

    std::vector<double> prices = {100, 101, 102, 100, 99, 98, 100, 102, 103, 104, 105};
    auto vol = rolling_volatility(prices, 22);
    std::cout << "Rolling Volatility: " << vol.back() << std::endl;

    return 0;
}

Performance

The library uses SIMD optimizations and zero-copy NumPy integration for maximum performance. See demos/ for benchmark comparisons.

Example with NumPy (zero-copy):

import finmath
import numpy as np

# Zero-copy NumPy arrays for best performance
prices = np.array([100, 101, 102, 103, 104, 105])
sma = finmath.simple_moving_average_simd(prices, window_size=3)
rsi = finmath.smoothed_rsi_simd(prices, window_size=14)

Testing

Run the test suite:

cd build
ctest

Contributing

We welcome contributions to finmath! To contribute:

  1. Fork the repository.

  2. Create a new branch for your feature or bug fix:

    git checkout -b feature/my-new-feature
    
  3. Commit your changes:

    git commit -m "Add some feature"
    
  4. Push the branch to your fork:

    git push origin feature/my-new-feature
    
  5. Open a Pull Request on the original repository.

License

This project is licensed under the AGPL-3.0 License - see the LICENSE file for details.

Acknowledgments

  • Inspired by the gs_quant library for financial time series analysis.
  • Thanks to pybind11 for making it easy to integrate C++ and Python.

About

Python Financial Math Library Written in C and C++

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published