41 lines
1.7 KiB
Python
41 lines
1.7 KiB
Python
import numpy as np
|
|
import pandas as pd
|
|
|
|
from models.cacu.pim import Fit, ISpline, Mixture
|
|
|
|
|
|
def test_unestimable_covariances_never_produce_zero_width_intervals():
|
|
baseline = ISpline((0.0, 1.0, 2.0), 2)
|
|
params = np.zeros(1 + baseline.n_params)
|
|
X, Z = np.ones((1, 1)), np.empty((1, 0))
|
|
good = Fit(1, 0, baseline, params, np.eye(len(params)), 0, True)
|
|
assert np.isfinite(good.risk_with_variance(X, Z, [0, 1])[1]).all()
|
|
for covariance in (np.full((len(params), len(params)), np.nan),
|
|
-np.eye(len(params)), np.zeros((len(params), len(params)))):
|
|
broken = Fit(1, 0, baseline, params, covariance, 0, True)
|
|
assert np.isnan(broken.risk_with_variance(X, Z, [0, 1])[1]).all()
|
|
|
|
|
|
def test_frozen_parameter_only_invalidates_horizons_it_affects():
|
|
baseline = ISpline((0.0, 1.0, 2.0), 2)
|
|
params = np.zeros(1 + baseline.n_params)
|
|
covariance = np.eye(len(params))
|
|
covariance[1, :] = np.nan
|
|
covariance[:, 1] = np.nan
|
|
fitted = Fit(1, 0, baseline, params, covariance, 0, True)
|
|
variance = fitted.risk_with_variance(np.ones((1, 1)), np.empty((1, 0)), [0, 1])[1]
|
|
assert np.isfinite(variance[0, 0])
|
|
assert np.isnan(variance[0, 1])
|
|
|
|
|
|
def test_diagnostics_count_rows_without_intervals():
|
|
baseline = ISpline((0.0, 1.0, 2.0), 2)
|
|
params = np.zeros(1 + baseline.n_params)
|
|
model = Mixture((0, 1), (), (), {})
|
|
model.fits_ = {(): Fit(1, 0, baseline, params, -np.eye(len(params)), 0, True)}
|
|
model.seen_ = {(): {}}
|
|
report = model.variance_diagnostics(pd.DataFrame(index=range(2)))
|
|
assert report['strata'][0]['rows_without_interval'] == 2
|
|
assert report['strata'][0]['negative_variances']
|
|
assert report['strata'][0]['status'] == 'not_estimable'
|