quantdata.sanos.sanos_tools#
SANOS#
- class quantdata.sanos.sanos_tools.SanosPricer(smoothness, expiries, variances, xstrikes, xdensities, min_half_spread=0.0001, min_dlv=0.01, max_dlv=5.0, _eps_price=1e-06, _eps_density=1e-07, _eps_dlv_dens_pos=0.0001, _eps_dlv_dens_sum1=0.0001, _eps_dlv_dens_int1=0.0001, _eps_dlv_diff=0.001)[source]#
Bases:
objectArbitrage-Free Option pricer based on DLV/SANOS.
- __call__(expiries, strikes, *, and_vols=False)[source]#
Price options at
strikeswith expiriesexpiries.If
and_volsisTruethis function returns call prices and their implied volatilities; ifFalse, it will return only call prices.- Parameters:
The expiry or expiries to price at.
If
expiriesis a float, all strikes are evaluated at that expiry.If
expiriesis a 1D array of shape(N,), thenstrikesmust also be a 1D array of shape(N,), and the function will price each strike at their respective expiry.If
expiriesis a 2D array of shape(M,1), thenstrikesmust be a 2D array of shape(1,N), and the function will price each strike at each expiry, returning an array of shape(M,N).
strikes (
ndarray) – The strikes to price at. The shape of this array must be compatible with the shape ofexpiriesas described above.and_vols (
bool) – Whether to return implied volatilities in addition to call prices. IfTrue, the function returns a tuple of (call prices, implied volatilities). IfFalse, it returns only call prices.
- compute_market_fit(expiries, strikes, bids, asks, *, masks=None, and_vols=True)[source]#
Compute standardized fitting error stats for the given market data.
- Returns:
Errors – A dictionary-lik object with the following fields: *
fits: list of fitted prices for the input strikes, per expiry. *fit_errs: list of fitting errors for the input strikes, per expiry, in price, where the error is defined as the distance to the bid/ask spread in units of half-spread, i.e.max( 0, fit - ask ) + max( 0, bid - fit) / max( 0.5*(ask-bid), min_half_spread ). *fit_vols: list of fitted vols for the input strikes, per expiry, ifand_volsisTrue. *pcnt_fit: the percentage of strikes for which the fit is outside the bid/ask spread, i.e.sum( fit_errs > 0 ) / len(fit_errs). *median_fit_err: the median fitting error among strikes for which the fit is outside the bid/ask spread, i.e.median( fit_errs[fit_errs>0.] ). *mean_fit_err: the mean fitting error among strikes for which the fit is outside the bid/ask spread, i.e.mean( fit_errs[fit_errs>0.] ).- Return type:
PrettyObject
- dlvsqs(*, min_dlv=0.0, max_dlv=None, on_all_strikes=False)[source]#
Compute squared discrete local volatilities.
- Parameters:
min_dlv (
float) – Minimum discrete local volatility. If the computed DLV is below this threshold, it will be set to this value. Must be positive and less thanmax_dlv.max_dlv (
float|None) – Maximum discrete local volatility. If the computed DLV is above this threshold, it will be set to this value. Must be positive and greater thanmin_dlv.on_all_strikes (
bool) – If True, the function will return DLV^2 values for all strikes instrikes, not justxstrikes[:,1:-1]. The DLVs at the boundaries will be set to zero.
- class quantdata.sanos.sanos_tools.SanosLPSolver(timeout_seconds=300.0, _debug_verbose_cvxpy=None)[source]#
Bases:
object- __call__(objective, constraints, variables, *, message_context=None, for_expiry=False, is_fallback=False, verbose=<cdxcore.verbose.Context object>)[source]#
Solve a problem with fall back.
Returns
(success, status_str, is_fallback)wheresuccessis a boolean indicating whether the problem was solved successfully,status_stris the status string returned by the solver, andis_fallbackis a boolean indicating whether the fallback solver was used.
- quantdata.sanos.sanos_tools.repair_density_nonnegative(p, x, *, target_sum=1.0, target_xmoment=1.0, floor=0.0, tol=None, max_iter=64)[source]#
Project p onto:
y >= floor sum(y) = target_sum x @ y = target_xmoment
For floor=0 this gives a nonnegative density.
This is the L2-closest feasible y to p.
- quantdata.sanos.sanos_tools.warn_and_fix_density(x, p, eps, message)[source]#
Wrapper around fixing and warning
- quantdata.sanos.sanos_tools.make_variances(expiries, strikes, prices, vols=None, message_context=None)[source]#
Make variances for interpolation
If ‘vols’ is provided, this function checks that variances are increassing. If ‘vols’ are not provided, this function computes ATM vols by interpolating the input ‘prices’ at strike 1, and then computes variances from the ATM vols, and finally makes sure that variances are increasing by flooring forward variances to a small positive value if needed.
Returns an array of variances, for
expiries.