Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ build
cmake-build-debug
venv
.idea
*.so
*.pyc
__pycache__/
59 changes: 0 additions & 59 deletions .vscode/settings.json

This file was deleted.

116 changes: 42 additions & 74 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.15)

project(finmath)

Expand All @@ -9,85 +9,53 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
# Add debugging symbols (set to Release for optimized performance if needed)
set(CMAKE_BUILD_TYPE Release)

# Enable SIMD optimizations based on architecture
if(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)|(amd64)|(AMD64)")
# x86/x86_64: Enable SSE2 (baseline) and detect AVX
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse2")

# Try to enable AVX if available
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-mavx" COMPILER_SUPPORTS_AVX)
if(COMPILER_SUPPORTS_AVX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mavx")
message(STATUS "AVX support enabled")
else()
message(STATUS "AVX not supported, using SSE2")
endif()
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64|ARM64)")
# ARM64: NEON is standard on ARMv8
message(STATUS "ARM NEON support enabled (ARMv8)")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm")
# ARM32: Try to enable NEON
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpu=neon")
message(STATUS "ARM NEON support enabled (ARMv7)")
else()
message(STATUS "Using scalar fallback (no SIMD)")
endif()

# Add include directories
include_directories(${PROJECT_SOURCE_DIR}/include)

# Source files
file(GLOB SOURCES "src/cpp/*/*.cpp")

# Create the main C++ library target with a unique name
add_library(finmath_library SHARED ${SOURCES}
"src/cpp/InterestAndAnnuities/simple_interest.cpp"
"include/finmath/InterestAndAnnuities/simple_interest.h"
"include/finmath/OptionPricing/options_pricing.h"
"include/finmath/OptionPricing/options_pricing_types.h"
"include/finmath/TimeSeries/rolling_volatility.h"
"include/finmath/TimeSeries/simple_moving_average.h"
"include/finmath/TimeSeries/rsi.h"
"include/finmath/TimeSeries/ema.h"
"include/finmath/TimeSeries/rolling_std_dev.h")

# Test executables
add_executable(black_scholes_test test/OptionPricing/black_scholes_test.cpp)
target_link_libraries(black_scholes_test finmath_library)

add_executable(binomial_option_pricing_test test/OptionPricing/binomial_option_pricing_test.cpp)
target_link_libraries(binomial_option_pricing_test finmath_library)

add_executable(compound_interest_test test/InterestAndAnnuities/compound_interest_test.cpp)
target_link_libraries(compound_interest_test finmath_library)

add_executable(rsi_test test/TimeSeries/rsi_test.cpp)
target_link_libraries(rsi_test finmath_library)

add_executable(rolling_std_dev_test test/TimeSeries/rolling_std_dev_test.cpp)
target_link_libraries(rolling_std_dev_test finmath_library)

add_executable(bellman_arbitrage_test test/GraphAlgos/bellman_arbitrage_test.cpp)
target_link_libraries(bellman_arbitrage_test finmath_library)
# Markov chain sources only (this branch)
set(SOURCES
"src/cpp/MarkovChains/markov_chain.cpp"
"src/cpp/finmath.cpp"
)

# Test runner
add_executable(run_all_tests test/test_runner.cpp)
# Create the main C++ library target
add_library(finmath_library SHARED ${SOURCES})

# Enable testing
enable_testing()

# Define individual tests
add_test(NAME BlackScholesTest COMMAND black_scholes_test)
add_test(NAME BinomialOptionPricingTest COMMAND binomial_option_pricing_test)
add_test(NAME CompoundInterestTest COMMAND compound_interest_test)
add_test(NAME RSITest COMMAND rsi_test)
add_test(NAME RollingStdDevTest COMMAND rolling_std_dev_test)
add_test(NAME BellmanArbitrageTest COMMAND bellman_arbitrage_test)

# Add a custom target to run all tests
add_custom_target(build_and_test
COMMAND ${CMAKE_COMMAND} --build . --target black_scholes_test
COMMAND ${CMAKE_COMMAND} --build . --target binomial_option_pricing_test
COMMAND ${CMAKE_COMMAND} --build . --target compound_interest_test
COMMAND ${CMAKE_COMMAND} --build . --target rsi_test
COMMAND ${CMAKE_COMMAND} --build . --target bellman_arbitrage_test
COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)

# Make 'build_and_test' the default target
add_custom_target(default ALL DEPENDS build_and_test)

# Add pybind11 for Python bindings
include(FetchContent)
FetchContent_Declare(
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11.git
GIT_TAG v2.10.0 # Use a stable version of pybind11
)
FetchContent_MakeAvailable(pybind11)

# Create the Python bindings target
pybind11_add_module(finmath_bindings src/python_bindings.cpp ${SOURCES})

# Set the output name of the bindings to 'finmath' to match your desired module name
set_target_properties(finmath_bindings PROPERTIES OUTPUT_NAME "finmath")
# Helper macro to add a test executable and link it
macro(add_cpp_test test_name source_file)
message(STATUS "Adding C++ test: ${test_name} from ${source_file}")
add_executable(${test_name}_executable ${source_file})
target_link_libraries(${test_name}_executable PRIVATE finmath_library)
add_test(NAME ${test_name} COMMAND ${test_name}_executable)
endmacro()

# Link the Python bindings target with the C++ library
target_link_libraries(finmath_bindings PRIVATE finmath_library)
# Markov chain test only
add_cpp_test(MarkovChainTest test/MarkovChains/C++/markov_chain_test.cpp)
37 changes: 22 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# 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. The goal is to provide a fast and reliable tool for financial calculations that can be used in Python.
**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

Expand All @@ -16,7 +22,7 @@
1. **Clone the repository**:

```bash
git clone https://github.com/prajwalshah19/finmath.git
git clone https://github.com/Boiler-Quant/finmath.git
cd finmath
```

Expand Down Expand Up @@ -102,26 +108,27 @@ int main() {
}
```

## Benchmarking
## Performance

You can compare the performance of `finmath` functions with other implementations (e.g., `gs_quant`) to see the speedup provided by the C++ implementations:
The library uses SIMD optimizations and zero-copy NumPy integration for maximum performance. See `demos/` for benchmark comparisons.

**Example with NumPy (zero-copy):**
```python
import timeit
import gs_quant.timeseries as ts
import functions
import finmath
import numpy as np

# Example data for 1000 days
prices = ts.generate_series(1000)

# Timing the finmath C++ implementation of rolling volatility
def test_cpp_implementation():
return finmath.rolling_volatility(prices.tolist(), 22)
# 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)
```

cpp_time = timeit.timeit(test_cpp_implementation, number=100)
print(f"C++ implementation time: {cpp_time:.6f} seconds")
## Testing

Run the test suite:
```bash
cd build
ctest
```

## Contributing
Expand Down
52 changes: 0 additions & 52 deletions benchmark/README.md

This file was deleted.

Loading