### Install Quantflow Source: https://github.com/quantmind/quantflow/blob/main/docs/index.md Install the Quantflow library using pip. This command installs the base package. ```bash pip install quantflow ``` -------------------------------- ### Install quantflow with data extra Source: https://github.com/quantmind/quantflow/blob/main/docs/api/data/index.md Install the quantflow library with the optional 'data' extra to enable data fetching capabilities. ```bash pip install quantflow[data] ``` -------------------------------- ### Install Development Dependencies Source: https://github.com/quantmind/quantflow/blob/main/docs/contributing.md Install the necessary development dependencies using the provided Makefile. ```bash make install-dev ``` -------------------------------- ### Install Quantflow with ML Dependencies Source: https://github.com/quantmind/quantflow/blob/main/docs/index.md Install Quantflow with optional dependencies for training the Deep Implied Volatility model. This command includes the 'ml' extra. ```bash pip install quantflow[ml] ``` -------------------------------- ### Two-Factor BNS Calibration Example Source: https://github.com/quantmind/quantflow/blob/main/docs/tutorials/bns_calibration.md Demonstrates how to calibrate the two-factor BNS model using provided data. It initializes the model with specific parameters and fits it to implied volatility data. ```python import pandas as pd from quantflow.options.calibration.bns import BNS2Calibration from quantflow.options.calibration.calibration import Calibration # Load implied volatility data data = pd.read_csv("../data/vol_surface.csv") # Initialize BNS2Calibration calibrator = BNS2Calibration( data, v01=0.01, v02=0.01, theta=0.5, beta=0.5, kappa2=0.1, kappa_delta=0.5, rho1=-0.5, rho2=-0.5, w=0.5, ) # Perform calibration calibrator.fit() # Print calibrated parameters print(calibrator.params) ``` -------------------------------- ### Import necessary libraries Source: https://github.com/quantmind/quantflow/blob/main/frontend/src/yield-curve.md Imports required modules for API calls, plotting, and data manipulation. Ensure these libraries are installed and accessible. ```javascript import {fetchJson} from "./lib/api.js"; import * as Plot from "npm:@observablehq/plot"; import * as d3 from "npm:d3"; ``` -------------------------------- ### Initialize Inputs for Double Exponential Sampling Source: https://github.com/quantmind/quantflow/blob/main/frontend/src/sampling.md Sets up input ranges for the logarithm of the asymmetry parameter (kappa) and the number of samples. These inputs control the simulation parameters. ```javascript const deLogKappaInput = Inputs.range([-2, 2], {step: 0.1, value: 0.1, label: "log(κ) asymmetry"}); const deLogKappa = Generators.input(deLogKappaInput); const deSamplesInput = Inputs.range([100, 10000], {step: 100, value: 1000, label: "Samples"}); const deSamples = Generators.input(deSamplesInput); ``` -------------------------------- ### Invert Wiener Process Characteristic Function using FFT Source: https://github.com/quantmind/quantflow/blob/main/docs/theory/inversion.md This example demonstrates how to invert the characteristic function of the Wiener process to obtain the standard normal distribution using the Fast Fourier Transform (FFT). It requires the 'fft.py' script to be available. ```python --8<-- "docs/examples/fft.py" ``` -------------------------------- ### Initialize and use Deribit client Source: https://github.com/quantmind/quantflow/blob/main/docs/api/data/index.md Initialize the Deribit client as an asynchronous context manager and retrieve a volatility surface loader. Ensure the client is used within an async context. ```python from quantflow.data.deribit import Deribit async with Deribit() as cli: loader = await cli.volatility_surface_loader("btc") ``` -------------------------------- ### Clone the Quantflow Repository Source: https://github.com/quantmind/quantflow/blob/main/docs/contributing.md Clone the official quantflow repository to your local machine to begin development. ```bash git clone git@github.com:quantmind/quantflow.git ``` -------------------------------- ### Define Default and Input Rates Source: https://github.com/quantmind/quantflow/blob/main/frontend/src/yield-curve.md Sets up the initial default interest rates and creates a mutable state variable `inputRates` to hold the rates that can be modified by the user. ```javascript const defaultRates = [ {ttm: 1/365, rate: 0.043}, {ttm: 7/365, rate: 0.043}, {ttm: 1/12, rate: 0.044}, {ttm: 0.25, rate: 0.045}, {ttm: 0.5, rate: 0.046}, {ttm: 1, rate: 0.047}, {ttm: 2, rate: 0.043}, {ttm: 3, rate: 0.041}, {ttm: 5, rate: 0.040}, {ttm: 7, rate: 0.041}, {ttm: 10, rate: 0.043}, {ttm: 20, rate: 0.048}, {ttm: 30, rate: 0.049} ]; const inputRates = Mutable(defaultRates); const setInputRates = (v) => inputRates.value = v; ``` -------------------------------- ### Gaussian (Vasicek) Process Input Controls Source: https://github.com/quantmind/quantflow/blob/main/frontend/src/sampling.md Sets up interactive input controls for the Gaussian (Vasicek) process simulation, including mean reversion speed (kappa), number of samples, and an option for antithetic variates. ```javascript const gKappaInput = Inputs.range([0.1, 5], {step: 0.1, value: 1.0, label: "kappa"}); const gKappa = Generators.input(gKappaInput); const gSamplesInput = Inputs.range([100, 10000], {step: 100, value: 1000, label: "Samples"}); const gSamples = Generators.input(gSamplesInput); const gAntitheticInput = Inputs.toggle({label: "Antithetic variates", value: true}); const gAntithetic = Generators.input(gAntitheticInput); ``` -------------------------------- ### Select Pricing Method Source: https://github.com/quantmind/quantflow/blob/main/docs/tutorials/pricing_method_comparison.md Instantiate an OptionPricer with a specified pricing method. Requires importing OptionPricer, OptionPricingMethod, and the relevant model (e.g., Heston). ```python from quantflow.options.pricer import OptionPricer, OptionPricingMethod from quantflow.sp.heston import Heston model = Heston.create(vol=0.2) pricer = OptionPricer(model=model, method=OptionPricingMethod.COS) result = pricer.maturity(1.0) ``` -------------------------------- ### Run Tests Source: https://github.com/quantmind/quantflow/blob/main/docs/contributing.md Execute the test suite to ensure the codebase is functioning correctly. ```bash make tests ``` -------------------------------- ### Vasicek Process Kappa Input Source: https://github.com/quantmind/quantflow/blob/main/frontend/src/hurst.md Creates an input range for the kappa (mean reversion) parameter for the Vasicek process simulation. Higher kappa indicates stronger mean reversion. ```javascript const kappaInput = Inputs.range([1, 500], {step: 1, value: 10, label: "Kappa (mean reversion)"}); const kappa = Generators.input(kappaInput); ``` -------------------------------- ### Display Gaussian Input Controls Source: https://github.com/quantmind/quantflow/blob/main/frontend/src/sampling.md Renders the input controls for the Gaussian process simulation in a flexible layout. ```javascript display(html`
${formatDate(refDate)} · Spot: ${spotMid ? d3.format(",.0f")(spotMid) : "N/A"} USD · ${enriched.length} options across ${maturities.length} maturities
`); ``` -------------------------------- ### Fetch Vasicek Process Data Source: https://github.com/quantmind/quantflow/blob/main/frontend/src/hurst.md Fetches Vasicek process data from the API based on the selected kappa value. A short delay is included before fetching. ```javascript await new Promise(r => setTimeout(r, 300)); const vasicek = await fetchJson(`/.api/hurst-vasicek?kappa=${kappa}`); ``` -------------------------------- ### Fetch Poisson Sampling Data Source: https://github.com/quantmind/quantflow/blob/main/frontend/src/sampling.md Fetches simulation data for the Poisson process from the API, using the current values of intensity and number of samples. A short delay is introduced before fetching. ```javascript await new Promise(r => setTimeout(r, 300)); const poissonData = await fetchJson(`/.api/poisson-sampling?intensity=${pIntensity}&samples=${pSamples}`); ``` -------------------------------- ### Black-Scholes Option Pricing Output Source: https://github.com/quantmind/quantflow/blob/main/docs/tutorials/option_pricing.md Displays the output of the Black-Scholes option pricing, including price, delta, gamma, and implied Black volatility. ```json --8<-- "docs/examples/output/wiener_volatility_pricer.out" ``` -------------------------------- ### BNS2 Calibration Output Source: https://github.com/quantmind/quantflow/blob/main/docs/tutorials/bns_calibration.md Displays the calibrated parameters for the two-factor BNS model, including initial variance, decay rates, and leverages. ```text v01: 0.01 v02: 0.01 theta: 0.5 beta: 0.5 kappa2: 0.1 kappa_delta: 0.5 rho1: -0.5 rho2: -0.5 w: 0.5 ``` -------------------------------- ### CIR PDF Comparison and Characteristic Function Analysis Source: https://github.com/quantmind/quantflow/blob/main/docs/tutorials/cir.md This snippet, included via a "code include" directive, likely contains Python code for comparing the analytical PDF of a CIR process with one derived from its characteristic function, and potentially visualizes the characteristic function itself. It is intended for detailed analysis of the CIR model's behavior over different time horizons. ```python --8<-- "docs/examples/cir_pdf_comparison.py" ``` -------------------------------- ### VolSurfaceLoader Source: https://github.com/quantmind/quantflow/blob/main/docs/api/options/vol_surface.md Base class or specific implementation for loading volatility surfaces. ```APIDOC ## VolSurfaceLoader ### Description Abstract or concrete class responsible for the process of loading volatility surface data. ### Class quantflow.options.surface.VolSurfaceLoader ``` -------------------------------- ### Calibrate and Plot BNS Smile Source: https://github.com/quantmind/quantflow/blob/main/docs/tutorials/bns_calibration.md This Python script calibrates the BNS volatility model to an implied volatility surface and plots the resulting calibrated smile. It requires the quantflow library and uses a specific output file for calibration results. ```python import matplotlib.pyplot as plt from quantflow.options.calibration.bns import BNSCalibration from quantflow.options.calibration.base import VolModelCalibration # Load implied volatility surface data # For demonstration, assume surface_data is loaded from a file or generated # surface_data = ... # Instantiate BNSCalibration calibrator = BNSCalibration() # Perform calibration calibrator.fit(surface_data) # Print calibration results to a file calibrator.save("docs/examples/output/vol_surface_bns_calibration.out") # Plot the calibrated smile fig, ax = plt.subplots() calibrator.plot(ax=ax) plt.show() ``` -------------------------------- ### GenericVolSurfaceLoader Source: https://github.com/quantmind/quantflow/blob/main/docs/api/options/vol_surface.md A generic loader for volatility surfaces, capable of handling various input formats. ```APIDOC ## GenericVolSurfaceLoader ### Description Provides a generic interface for loading volatility surfaces from different data sources or formats. ### Class quantflow.options.surface.GenericVolSurfaceLoader ``` -------------------------------- ### Plot Vasicek Process Path Source: https://github.com/quantmind/quantflow/blob/main/frontend/src/hurst.md Visualizes the simulated Vasicek process path over time. This plot helps to observe the mean-reverting behavior. ```javascript const vasicekPath = vasicek.dates.map((d, i) => ({date: new Date(d), value: vasicek.values[i]})); display(Plot.plot({ width: 800, height: 350, marginLeft: 60, marginBottom: 50, style: {background: "transparent"}, x: {label: "Time", type: "utc"}, y: {label: "Value"}, marks: [ Plot.line(vasicekPath, {x: "date", y: "value", stroke: "var(--theme-foreground-focus)", strokeWidth: 1.5}), ] })); ``` -------------------------------- ### Plot Gaussian Process Simulation vs. Analytical Distribution Source: https://github.com/quantmind/quantflow/blob/main/frontend/src/sampling.md Visualizes the simulated and analytical probability densities for the Gaussian process using Plot.js. It also displays the Kolmogorov-Smirnov test statistic and p-value if available. ```javascript const gBarData = gaussianData.x.map((x, i) => ({x, y: gaussianData.simulation[i], series: "Simulation"})); const gLineData = gaussianData.x.map((x, i) => ({x, y: gaussianData.analytical[i], series: "Analytical"})); const gBinWidth = gaussianData.x[1] - gaussianData.x[0]; display(Plot.plot({ width: 800, height: 450, marginLeft: 60, marginBottom: 50, style: {background: "transparent"}, x: {label: "Value"}, y: {label: "Density"}, color: {legend: true, domain: ["Simulation", "Analytical"], range: ["var(--theme-foreground-muted)", "var(--theme-foreground-focus)"]}, marks: [ Plot.rectY(gBarData, {x1: d => d.x - gBinWidth / 2, x2: d => d.x + gBinWidth / 2, y: "y", fill: "series", fillOpacity: 0.6, tip: true}), Plot.line(gLineData, {x: "x", y: "y", stroke: "series", strokeWidth: 2}), ] })); if (gaussianData.ks_statistic != null) { const ksColor = gaussianData.ks_pvalue < 0.05 ? "var(--theme-red)" : "var(--theme-green)"; display(html`KS statistic: ${gaussianData.ks_statistic.toFixed(4)} | p-value: ${gaussianData.ks_pvalue.toFixed(4)}
`); } ``` -------------------------------- ### OptionInput Source: https://github.com/quantmind/quantflow/blob/main/docs/api/options/vol_surface.md A general input for option-related data. ```APIDOC ## OptionInput ### Description Generic input class for option-specific data used in surface calculations. ### Class quantflow.options.inputs.OptionInput ``` -------------------------------- ### Define interactive input controls Source: https://github.com/quantmind/quantflow/blob/main/frontend/src/heston-vol-surface.md Sets up various input controls (select, range) for model parameters like volatility, correlation, and jump characteristics. These controls allow users to dynamically adjust the model inputs. ```javascript const modelInput = Inputs.select(new Map([["Jump Diffusion", "jd"], ["Heston with Jumps", "hj"]]), {label: "Model", value: "jd"}); const modelVal = Generators.input(modelInput); const volInput = Inputs.range([0.1, 0.8], {step: 0.01, value: 0.4, label: "Long term volatility"}); const volVal = Generators.input(volInput); const sigmaInput = Inputs.range([0.1, 2], {step: 0.1, value: 0.5, label: "Vol of vol"}); const sigmaVal = Generators.input(sigmaInput); const kappaInput = Inputs.range([0.1, 2], {step: 0.1, value: 0.5, label: "Variance mean reversion"}); const kappaVal = Generators.input(kappaInput); const rhoInput = Inputs.range([-0.6, 0.6], {step: 0.1, value: 0, label: "Correlation"}); const rhoVal = Generators.input(rhoInput); const rInput = Inputs.range([0.6, 1.6], {step: 0.1, value: 1.0, label: "Initial vol ratio"}); const rVal = Generators.input(rInput); const jfInput = Inputs.range([0.1, 0.9], {step: 0.05, value: 0.5, label: "Jump fraction"}); const jfVal = Generators.input(jfInput); const jiInput = Inputs.range([10, 100], {step: 5, value: 10, label: "Jump intensity"}); const jiVal = Generators.input(jiInput); const jaInput = Inputs.range([-2, 2], {step: 0.1, value: 0, label: "Jump asymmetry"}); const jaVal = Generators.input(jaInput); ``` -------------------------------- ### OptionPrices Source: https://github.com/quantmind/quantflow/blob/main/docs/api/options/vol_surface.md Container for multiple option prices. ```APIDOC ## OptionPrices ### Description Holds a collection of OptionPrice objects. ### Class quantflow.options.surface.OptionPrices ```