quantdata.dysanos.grid.torch_simulator#

class quantdata.dysanos.grid.torch_simulator.ARSamplerConfig(num_alpha=0.95, rescale_var=False, seed=113123231, use_matrix_exp=True, surface_first=True)[source]#

Bases: object

num_alpha: int | float | None = 0.95#
rescale_var: bool = False#
seed: int = 113123231#
use_matrix_exp: bool = True#
surface_first: bool = True#
property rng_current_state_uid: Generator#

Utility to mimic the same function in ARSampler, but for the config object.

class quantdata.dysanos.grid.torch_simulator.GaussianSpotImportanceConfig(*, spot_std_multiplier=8.0, base_probability=0.1, target_action_count=3)[source]#

Bases: object

Path-level proposal which broadens selected future log-spot marginals.

A positive base_probability component retains the original Gaussian path law. Every other mixture component changes one future log-spot s marginal to the same mean and a wider standard deviation. In the surface-first model the complete surface path is retained and the scalar conditional spot residual is bridged; the legacy joint model uses a full Gaussian state bridge.

spot_std_multiplier: float = 8.0#
base_probability: float = 0.1#
target_action_count: int = 3#
class quantdata.dysanos.grid.torch_simulator.GaussianSpotImportanceSample(*, paths, log_weights, proposal_components, target_steps, target_log_spot_std, reference_log_spot_sum, reference_log_spot_square_sum, reference_realized_variance_sum, reference_realized_variance_square_sum)[source]#

Bases: object

One exact Gaussian importance sample and direct-P control moments.

class quantdata.dysanos.grid.torch_simulator.AR1FitQuality(r2, r2_by_state, rmse, rmse_by_state, sample_mean, sample_std, fitted_mean, mean_error)[source]#

Bases: object

One-step deterministic-drift fit diagnostics.

summary_statistics()[source]#

Return scale-independent mean-fit statistics and log-spot details.

class quantdata.dysanos.grid.torch_simulator.AR1PCA(U0, alpha, dU, k, ndt, mean_dt, state_center, state_scale, vars_explained, var_explained, fit_quality=None, surface_first=False, num_surface_alpha=None, surface_var_explained=None, spot_surface_r2=None, spot_residual_std=None)[source]#

Bases: object

dh_t = (U0 - k h_t)dt + alpha’ dU sqrt{dt}

fit_quality: AR1FitQuality | None = None#
surface_first: bool = False#
num_surface_alpha: int | None = None#
surface_var_explained: float | None = None#
spot_surface_r2: float | None = None#
spot_residual_std: float | None = None#
class quantdata.dysanos.grid.torch_simulator.ARSampler(market, model, torch_data=None, *, surface_states=None, config=ARSamplerConfig(num_alpha=0.95, rescale_var=False, seed=113123231, use_matrix_exp=True, surface_first=True), verbose=<cdxcore.verbose.Context object>)[source]#

Bases: object

property num_alpha: int#
property num_xstate: int#
reset_rng()[source]#

Reset the random number generator to the original seed.

invariant_surface_distribution()[source]#

Return mean and covariance of the stationary ML-SANOS state block.

This distribution exists when the fitted surface generator has all eigenvalues in the open right half-plane. Log-spot is deliberately excluded because it has a unit root in the surface-first model.

sample_invariant_surface_states(num_samples)[source]#

Draw Gaussian states from the invariant surface distribution.

property rng_current_state_uid: Generator#

Identifies current ‘seed’. Used for caching samples

report_system()[source]#
hist_sample_xstates(index, num_steps, *, num_samples=0, bandwidth=0.0, replay=True)[source]#

Generate num_samples of num_steps days of extended state scenarios starting in the state of index and using the PCA factors from the historical data. The PCA factors can be replayed or sampled, and noise can be added to the PCA factors. If replay=True and bandwidth=0 this replays historic factor loadings.

Parameters:
  • index (int) – The index of the starting state in the time series.

  • num_steps (int) – The number of steps to simulate. This includes index; so if num_steps=1, the returned state is just the state at index.

  • num_samples (int) – The number of independent samples to generate. Set to zero to generate one sample, and return a matrix of shape (num_steps, num_xstate) instead of (num_samples, num_steps, num_xstate).

  • bandwidth (float) – The bandwidth for the PCA noise. A value of 0 means no noise, while a value of 1 means full noise. Must be in [0,1].

  • replay (bool) – If True, the PCA factors are replayed from the historical data, instead of being sampled. That means with bandwidth=0, the historical path is generated.

Returns:

Result – Returns a matrix of form (num_samples, num_steps, num_xstate) if num_samples>0 else (num_steps, num_xstate).

Return type:

np.ndarray

hist_sample_log_nspot_states(index, num_steps, *, num_samples=0, bandwidth=0.0, replay=True)[source]#

Generate num_samples of num_steps-day log normalized spot/state scenarios starting in the state of index.

Parameters:
  • index (int) – The index of the starting state in the time series.

  • num_steps (int) – The number of steps to simulate. This includes index; so if num_steps=1, the returned state is just the state at index.

  • num_samples (int) – The number of independent samples to generate. Set to zero to generate one sample, and return a matrix of shape (num_steps, num_xstate) instead of (num_samples, num_steps, num_xstate).

  • bandwidth (float) – The bandwidth for the PCA noise. A value of 0 means no noise, while a value of 1 means full noise. Must be in [0,1].

  • replay (bool) – If True, the PCA factors are replayed from the historical data, instead of being sampled. That means with bandwidth=0, the historical path is generated.

Returns:

State – log-spot and states

Return type:

tuple[ np.ndarray, np.ndarray ]

free_sample_xstates(num_steps, num_samples=1, dt=0.003968253968253968, *, index=None, bandwidth=0.0, random_start=True, max_internal_dt=None, initial_state=None)[source]#

Generate num_samples paths on a grid with spacing dt.

The PCA innovation law is calibrated at approximately pcaxar1.mean_dt. To simulate a larger output step robustly, each interval is therefore split into internal substeps no larger than max_internal_dt. Every internal substep uses an independently sampled PCA loading and the exact affine matrix-exponential drift. Only the states on the requested output grid are returned.

Parameters:
  • num_steps (int) – Number of returned states. The initial state is included, so num_steps=1 returns only the starting state.

  • num_samples (int) – Number of independent paths. Set to zero to generate one path and remove the leading sample dimension from the result.

  • dt (float) – Time between returned states, in the same units as time_y.

  • index (int) – Starting-state cut-off. If random_start=False, start exactly at xstate[index]. If random_start=True, sample starting states from xstate[:index+1]. With index=None, use the full history.

  • bandwidth (float) – Gaussian smoothing of the empirical PCA loadings. Zero uses pure historical resampling; one uses a Gaussian with the same covariance.

  • random_start (bool) – Whether to sample starting states up to index.

  • max_internal_dt (float | None) – Maximum internal simulation step used when bandwidth < 1. None uses the mean historical step pcaxar1.mean_dt. In full normal mode (bandwidth == 1), the exact Gaussian OU transition is sampled directly over dt and this argument is ignored.

  • initial_state (ndarray | None) – Exact raw-coordinate state from which every path starts. This is useful for conditional Monte Carlo. It cannot be combined with index; random_start has no effect when it is supplied.

Returns:

Result – Shape (num_samples, num_steps, num_xstate) when num_samples>0; otherwise (num_steps, num_xstate).

Return type:

np.ndarray

sample_conditional_xstates_at_offsets(*, initial_state, step_offsets, num_samples, dt=0.003968253968253968)[source]#

Draw exact Gaussian states at selected offsets from one state.

This is the sparse-time conditional counterpart of free_sample_xstates(). It avoids constructing every intervening daily state when only option-expiry states are required.

importance_sample_xstates(*, num_steps, num_samples, dt, target_steps, config, proposal_rng)[source]#

Sample exact Gaussian paths under a log-spot tail proposal.

For the surface-first model, the original surface path is retained and only the spot residual conditional on that complete path is widened. For the legacy joint fit, the original draw supplies the Gaussian state residual conditional on the selected future log spot. In both cases the returned weight is the likelihood ratio of the original path law to the full proposal mixture, not merely to the selected component.

quantdata.dysanos.grid.torch_simulator.fit_xar1_pca(X, time_y, *, num_alpha=0.9, rescale_var=False, use_matrix_exp=False)[source]#

Fits $$

dX_t = ( U0 - k X_t) dt + sqrt{dt} * Y_t

$$ and $$

Y_t = alpha_t @ dU

$$

quantdata.dysanos.grid.torch_simulator.fit_surface_first_xar1_pca(X, time_y, *, num_alpha=0.9, rescale_var=False, use_matrix_exp=False)[source]#

Fit stationary surface dynamics before adding the log-spot driver.

The extended state is X=(s,h): the first coordinate s is log-spot and the remaining coordinates h are ML-SANOS implied-volatility parameters. The surface block is fitted without spot. Log-spot has a constant drift and no mean reversion. Its innovation is regressed on the retained surface PCA scores; one orthogonal residual spot factor completes the driver. Thus num_alpha counts surface factors, while the returned joint model has one additional spot factor.

quantdata.dysanos.grid.torch_simulator.plot_sampler(sampler)[source]#