This is an excellent and challenging question! Combining FFT for option pricing within an AMPL optimization problem using IPOPT, especially with the requirement for precise derivatives, touches on advanced topics in numerical methods, scientific computing, and optimization software integration.
Here's a breakdown of how to achieve this, focusing on the "precise derivatives" requirement for IPOPT:
The Core Challenge: Precise Derivatives for IPOPT
IPOPT (Interior Point OPTimizer) is a non-linear solver that relies heavily on accurate first (and sometimes second) derivatives of the objective function and constraints. If you use finite differences for derivatives, IPOPT will likely struggle with convergence, precision, and speed.
FFT-based option pricing methods (like Carr-Madan or Lewis) are numerical algorithms involving sums and transformations. Directly differentiating the FFT algorithm itself is analytically cumbersome, if not impossible, in a practical sense.
The Solution: Instead of differentiating the numerical FFT algorithm, we differentiate the underlying analytical integral formula for the option price with respect to the model parameters. Then, we use the FFT algorithm again (or a modified version) to compute the numerical approximation of these derivative integrals.
This means your external FFT function won't just return prices; it will return prices and their corresponding derivatives with respect to each model parameter you want to calibrate.
Step-by-Step Implementation
Let's assume you're calibrating model parameters θ=(θ1,θ2,…,θM) (e.g., ν,σ for Variance Gamma). You have N observed option prices Ckobs at strikes Kk and maturity T. Your objective will likely be a sum of squared errors:
minθ∑k=1N
To solve this with IPOPT, you need to provide CFFT(Kk,T;θ) and its derivatives for all and .
1. Develop the External C/C++/Fortran Library (The Core)
This is where the heavy lifting happens. You'll need a compiled shared library (.dll on Windows, .so on Linux/macOS).
A. Characteristic Function and its Derivatives:
First, define your model's characteristic function ϕ(u;θ) (e.g., for Variance Gamma) and its partial derivatives with respect to each parameter θj:
ϕ(u;θ
B. Option Pricing Integrand (Carr-Madan/Lewis-like):
The generalized pricing formula involves an integral, which FFT approximates. For a European Call Option:
C(k)=S0e−αk
where is related to the characteristic function and damping factor .
The integrand typically looks like: (adjust for specific formulas like Carr-Madan, Lewis, etc.).
Let be this modified integrand for the option price.
C. Derivative of the Integrand:
Now, compute the partial derivative of f(ν;θ) with respect to each parameter θj:
This will involve the chain rule and the derivatives of your characteristic function from step A. For example, if then .
D. FFT Implementation (e.g., using FFTW):
- Discretization: Choose appropriate ranges and number of points for ν (frequency domain) and k (log-strike domain). This is crucial for accuracy and avoiding aliasing.
- FFT for Prices: Apply FFT to the sampled f(ν;θ) values to get option prices C(k).
- FFT for Derivatives: Apply FFT to the sampled values to get the derivatives of option prices .
E. Caching/Memoization (CRITICAL for performance):
AMPL's solver will repeatedly call your external functions. Recalculating the entire FFT for every single call (even if parameters haven't changed much) will be prohibitively slow.
Implement a caching mechanism in your C library:
- Store the last set of model parameters used for the FFT calculation.
- Store the computed arrays of prices and their derivatives.
- When an external function is called, check if the input parameters are (within a tolerance) the same as the cached ones. If so, return values from the cache. Otherwise, re-run the full FFT calculation, update the cache, and then return.
- Use
static variables or a singleton pattern in C++ to manage this global cache.
F. External Function Signatures (for AMPL):
For AMPL to work well with external functions, it prefers scalar inputs and outputs for its function declarations. To handle arrays of prices and derivatives efficiently for IPOPT, the standard AMPL approach for precise derivatives is to provide a function for the value and separate functions for its derivatives.
Let's assume you have M parameters (param1, param2, ...) and N_strikes.
Your C library will export functions like:
// Example for one strike (idx), and its parameters
double get_fft_price_at_idx(int strike_idx, double S0, double T, double alpha, double param1, double param2, /* ... other params */);
double get_fft_deriv_param1_at_idx(int strike_idx, double S0, double T, double alpha, double param1, double param2, /* ... other params */);
double get_fft_deriv_param2_at_idx(int strike_idx, double S0, double T, double alpha, double param1, double param2, /* ... other params */);
// ... and so on for other parameters
Crucially, the first time any of these functions is called with a new set of parameters, the internal cache (from Step E) should trigger a full FFT calculation for ALL strikes and ALL derivatives. Subsequent calls will read from the cache.
Compilation: Compile your C/C++/Fortran code into a shared library:
gcc -shared -o my_fft_lib.so my_fft_source.c -lfftw3 -lm (Linux)
cl /LD my_fft_source.c /Femy_fft_lib.dll -lfftw3 -lm (Windows)
(Make sure to link against FFTW3 library if you use it).
2. Design the AMPL Model (.mod file)
# Model Parameters (e.g., for Variance Gamma)
# These are the variables IPOPT will optimize
var vg_sigma >= 0.001; # Volatility
var vg_nu >= 0.001; # Kurtosis/Jump intensity
var vg_theta; # Skewness/Drift (can be negative)
# Fixed Market/Model Parameters
param S0; # Current Spot Price
param T; # Time to Maturity (in years)
param r; # Risk-free rate
param q; # Dividend yield
param alpha_damping; # Damping parameter for Carr-Madan/Lewis (e.g., 1.5)
# Observed Data
param N_strikes;
set STRIKES = 1..N_strikes;
param ObservedStrikes {STRIKES};
param ObservedPrices {STRIKES};
# Declare External Functions
# These tell AMPL that these functions are defined in an external library
# and their derivatives will be provided (or handled by the library).
# The 'symbolic' keyword is crucial.
# Function to get the FFT-calculated option price for a given strike index
# Note: strike_idx allows the C function to know which pre-calculated strike to return.
# The parameters S0, T, alpha_damping, vg_sigma, vg_nu, vg_theta are passed to the C function
# so it can perform the calculation or check its cache.
function get_fft_price(strike_idx, S0, T, alpha_damping, vg_sigma, vg_nu, vg_theta) symbolic;
# Functions to get the precise derivatives (for IPOPT)
# The '_jacobian_' prefix followed by the function name, and then '_p_parameter_name'
# tells AMPL that this function returns the partial derivative of 'get_fft_price'
# with respect to 'parameter_name'.
function _jacobian_get_fft_price(strike_idx, S0, T, alpha_damping, vg_sigma, vg_nu, vg_theta) _p_vg_sigma symbolic;
function _jacobian_get_fft_price(strike_idx, S0, T, alpha_damping, vg_sigma, vg_nu, vg_theta) _p_vg_nu symbolic;
function _jacobian_get_fft_price(strike_idx, S0, T, alpha_damping, vg_sigma, vg_nu, vg_theta) _p_vg_theta symbolic;
# Objective Function: Minimize sum of squared errors
minimize CalibrationError:
sum {k in STRIKES}
(get_fft_price(k, S0, T, alpha_damping, vg_sigma, vg_nu, vg_theta) - ObservedPrices[k])^2;
# Optional: Constraints on parameters (e.g., typical ranges for VG)
# subject to SigmaRange: vg_sigma >= 0.001 and vg_sigma <= 1.0;
# subject to NuRange: vg_nu >= 0.001 and vg_nu <= 10.0;
# subject to ThetaRange: vg_theta >= -1.0 and vg_theta <= 1.0;
3. Prepare the Data (.dat file)
# Data for your model
param S0 := 100;
param T := 0.25; # 3 months
param r := 0.01;
param q := 0.00;
param alpha_damping := 1.5; # A typical value for Carr-Madan
# Observed option prices and strikes
param N_strikes := 5;
param ObservedStrikes :=
[1] 90
[2] 95
[3] 100
[4] 105
[5] 110;
param ObservedPrices :=
[1] 11.00
[2] 7.00
[3] 3.50
[4] 1.50
[5] 0.50;
# Initial guesses for the parameters
# These are crucial for non-linear solvers
var vg_sigma := 0.2;
var vg_nu := 0.5;
var vg_theta := -0.1;
4. Run AMPL (.run file)
# Load the external library
option functions_table "my_fft_lib.dll"; # Or "my_fft_lib.so" on Linux/macOS
# Load the model and data
model calibration_model.mod;
data calibration_data.dat;
# Set initial parameter values (can also be in .dat file as above)
# let vg_sigma := 0.2;
# let vg_nu := 0.5;
# let vg_theta := -0.1;
# Choose the IPOPT solver
option solver ipopt;
# Set IPOPT options (optional, but often helpful)
# option ipopt_options 'tol=1e-6 max_iter=1000 print_level=5';
# Solve the optimization problem
solve;
# Display results
display vg_sigma, vg_nu, vg_theta;
display CalibrationError;
Important Considerations and Caveats:
- Compiler and Linker Settings: Ensure your C/C++ library is compiled correctly as a shared library (
.dll or .so) and that the FFTW library (or whatever FFT library you use) is properly linked.
- AMPL C-API: You'll need to understand how AMPL's C-API works for external functions. The
amplfunc.h header will be necessary. For each function you declare in AMPL, you'll have a corresponding C function that takes an arginfo *ai pointer and returns a real value. You'll use GetDVal(ai, k) to retrieve input arguments.
- Error Handling in C: Your C functions should handle potential errors (e.g., invalid input parameters for the characteristic function, division by zero). Returning
NAN (Not a Number) for invalid calculations can signal issues to AMPL/IPOPT.
- Numerical Stability of FFT:
- Damping Factor (α): This is critical for convergence of the integral and should be chosen carefully (e.g., α=1.5 to 2.0 for many characteristic functions).
- Number of Points: Use a power of 2 for FFT points (e.g., 2^10 = 1024, 2^12 = 4096). More points improve accuracy but increase computation time.
This approach ensures that IPOPT receives the precise derivatives it needs for robust and efficient optimization, while leveraging the speed of FFT for calculating option prices and their sensitivities.