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: object

Arbitrage-Free Option pricer based on DLV/SANOS.

min_half_spread: float = 0.0001#
min_dlv: float = 0.01#
max_dlv: float = 5.0#
property T: int#
__call__(expiries, strikes, *, and_vols=False)[source]#

Price options at strikes with expiries expiries.

If and_vols is True this function returns call prices and their implied volatilities; if False, it will return only call prices.

Parameters:
  • expiries (float | ndarray) –

    The expiry or expiries to price at.

    • If expiries is a float, all strikes are evaluated at that expiry.

    • If expiries is a 1D array of shape (N,), then strikes must also be a 1D array of shape (N,), and the function will price each strike at their respective expiry.

    • If expiries is a 2D array of shape (M,1), then strikes must 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 of expiries as described above.

  • and_vols (bool) – Whether to return implied volatilities in addition to call prices. If True, the function returns a tuple of (call prices, implied volatilities). If False, it returns only call prices.

model_vols(expiries)[source]#

Interpolates the model variances, scaled by smoothness

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, if and_vols is True. * 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 than max_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 than min_dlv.

  • on_all_strikes (bool) – If True, the function will return DLV^2 values for all strikes in strikes, not just xstrikes[:,1:-1]. The DLVs at the boundaries will be set to zero.

densities_from_dlvsqs(dlvsqs)[source]#

Compute densities from discrete local volatilities.

This is the inverse operation to dlvsqs(), and will return the original densities if the input dlvsqs are the same as those computed by dlvsqs() (up to numerical precision).

class quantdata.sanos.sanos_tools.SanosLPSolver(timeout_seconds=300.0, _debug_verbose_cvxpy=None)[source]#

Bases: object

timeout_seconds: float = 300.0#
__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) where success is a boolean indicating whether the problem was solved successfully, status_str is the status string returned by the solver, and is_fallback is 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.