check_conforms

Defined in fynance.core

check_conforms(obj, protocol, *, T=64, N=3, seed=0)[source]

Smoke-run protocol’s methods on obj with seeded synthetic data.

Designs one canonical smoke input per protocol from the actual method set in fynance.core.protocols:

  • FeatureTransform: fit(X) then transform(X) on a (T, N) array; transform must return a numpy.ndarray whose leading (time) axis has length T.

  • SignalModel: fit(X, y) then predict(X); predict must return a numpy.ndarray of shape (T,).

  • Allocator: __call__ on a (T, N) return/covariance-like matrix; must return a numpy.ndarray of shape (N,) (one weight per asset).

  • CostModel: __call__ on a (T, N) weight path; must return a numpy.ndarray of shape (T,) (one cost per step).

  • Metric: __call__ on a (T,) return curve; must return a plain scalar (int/float/numpy scalar), not an array.

  • DataSource: not smoke-run. Its only method, load(*args, **kwargs), is an open-ended I/O port (the one boundary protocol that touches the outside world) – there is no synthetic input this function can fabricate that would exercise a real adapter. Always raises ValueError pointing at calling load() directly on a concrete instance instead.

Any other object passed as protocol (not one of the six seams above) also raises ValueError: there is no registered smoke recipe for it.

Parameters:
objobject

Candidate instance to check.

protocoltype

One of the typing.Protocol classes from fynance.core.protocols.

Tint, optional

Synthetic time length. Default is 64.

Nint, optional

Synthetic asset/feature count. Default is 3.

seedint, optional

Seed of the synthetic data generator. Default is 0.

Returns:
None

Nothing on success.

Raises:
AssertionError

If obj does not structurally conform to protocol, or one of its methods returns a value of the wrong type/shape/dtype. The message names the offending method and the expected vs. actual type/shape.

ValueError

If protocol is DataSource (I/O port, see above), or is not one of the protocols this function knows how to smoke-test.

Examples

>>> import numpy as np
>>> from fynance.core import FeatureTransform
>>> from fynance.core.checks import check_conforms
>>> class ZScore:
...     def fit(self, X):
...         self.mean_ = X.mean(axis=0)
...         self.std_ = X.std(axis=0)
...         return self
...     def transform(self, X):
...         return (X - self.mean_) / self.std_
>>> check_conforms(ZScore(), FeatureTransform)
>>> class BrokenTransform(ZScore):
...     def transform(self, X):
...         return list(X.mean(axis=0))
>>> try:
...     check_conforms(BrokenTransform(), FeatureTransform)
... except AssertionError as e:
...     print(str(e))
transform() returned list, expected numpy.ndarray