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
- class quantdata.dysanos.grid.torch_simulator.GaussianSpotImportanceConfig(*, spot_std_multiplier=8.0, base_probability=0.1, target_action_count=3)[source]#
Bases:
objectPath-level proposal which broadens selected future log-spot marginals.
A positive
base_probabilitycomponent retains the original Gaussian path law. Every other mixture component changes one future log-spotsmarginal 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.
- 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:
objectOne 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:
objectOne-step deterministic-drift fit diagnostics.
- 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:
objectdh_t = (U0 - k h_t)dt + alpha’ dU sqrt{dt}
- fit_quality: AR1FitQuality | 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- 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.
- hist_sample_xstates(index, num_steps, *, num_samples=0, bandwidth=0.0, replay=True)[source]#
Generate
num_samplesofnum_stepsdays of extended state scenarios starting in the state ofindexand 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. Ifreplay=Trueandbandwidth=0this 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 includesindex; so ifnum_steps=1, the returned state is just the state atindex.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)ifnum_samples>0else(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_samplesofnum_steps-day log normalized spot/state scenarios starting in the state ofindex.- Parameters:
index (
int) – The index of the starting state in the time series.num_steps (
int) – The number of steps to simulate. This includesindex; so ifnum_steps=1, the returned state is just the state atindex.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_samplespaths on a grid with spacingdt.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 thanmax_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, sonum_steps=1returns 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 astime_y.index (
int) – Starting-state cut-off. Ifrandom_start=False, start exactly atxstate[index]. Ifrandom_start=True, sample starting states fromxstate[:index+1]. Withindex=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 toindex.max_internal_dt (
float|None) – Maximum internal simulation step used whenbandwidth < 1.Noneuses the mean historical steppcaxar1.mean_dt. In full normal mode (bandwidth == 1), the exact Gaussian OU transition is sampled directly overdtand 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 withindex;random_starthas no effect when it is supplied.
- Returns:
Result – Shape
(num_samples, num_steps, num_xstate)whennum_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 coordinatesis log-spot and the remaining coordinateshare 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. Thusnum_alphacounts surface factors, while the returned joint model has one additional spot factor.