MCDropout

Defined in fynance.models.uncertainty

class MCDropout(model, n_samples=50, seed=0)[source]

Bases: object

Epistemic-uncertainty proxy via Monte Carlo Dropout at inference.

Wraps one dropout-bearing torch.nn.Module (typically a BaseNeuralNet subclass such as MultiLayerPerceptron built with drop > 0) and approximates a Bayesian posterior by keeping dropout active at inference and averaging n_samples stochastic forward passes (Gal & Ghahramani, 2016, “Dropout as a Bayesian Approximation”). The spread across those passes — predict_std — is the epistemic-uncertainty proxy.

A plain model.eval() freezes dropout too (no stochasticity left to sample from). predict / predict_std instead first put the whole model in eval mode (so batch-norm and similar layers behave as at inference) and then explicitly switch back to train mode only the ``torch.nn.Dropout*`` submodules, so dropout masks are still resampled on every forward pass while everything else stays deterministic. The model’s original mode is restored before returning (or raising).

Conforms to the SignalModel protocol (fit/predict).

Parameters:
modeltorch.nn.Module

A SignalModel-conforming net (fit(X, y) / predict(X)) with at least one torch.nn.Dropout* submodule. A model with no dropout submodule still works but is pointless (every one of the n_samples passes is then identical, so predict_std is ~0): a UserWarning is raised at construction in that case.

n_samplesint

Number of stochastic forward passes averaged by predict / predict_std. Default 50.

seedint

Seed (torch.manual_seed) applied before drawing the n_samples dropout masks, so both methods are reproducible for a given instance.

See also

fynance.models.uncertainty.DeepEnsemble

Notes

Increasing n_samples reduces the variance of the mean estimate returned by predict (the mean of n i.i.d. stochastic passes has variance \(\sigma^2 / n\) by the law of large numbers) but does not shrink predict_std itself, which converges to the model’s intrinsic dropout-induced spread rather than to zero.

Examples

>>> import numpy as np
>>> import torch
>>> from fynance.models.mlp import MultiLayerPerceptron
>>> from fynance.models.uncertainty import MCDropout
>>> rng = np.random.default_rng(0)
>>> X = rng.standard_normal((40, 2)).astype(np.float32)
>>> y = np.sin(X[:, :1]).astype(np.float32)
>>> net = MultiLayerPerceptron(2, 1, layers=[8], activation=torch.nn.Tanh,
...                            drop=0.2)
>>> _ = net.set_optimizer(torch.nn.MSELoss, torch.optim.Adam, lr=1e-2)
>>> mc = MCDropout(net, n_samples=10, seed=0).fit(X, y)
>>> mc.predict(X).shape
(40,)
>>> bool((mc.predict_std(X) >= 0).all())
True
fit(X, y)[source]

Fit the wrapped model normally (dropout trains as usual).

Parameters:
X, yarray-like

Training data forwarded to the wrapped model’s fit(X, y).

Returns:
MCDropout

self.

predict(X)[source]

Mean over n_samples stochastic forward passes, shape (T,).

predict_std(X)[source]

Std over n_samples stochastic forward passes — epistemic proxy.