def test_fit_nan(self): da = self.da.copy() da[0, 0, 0] = np.nan out_nan = generic.fit(da, "lognorm") out_censor = generic.fit(da[1:], "lognorm") np.testing.assert_array_equal(out_nan.values[:, 0, 0], out_censor.values[:, 0, 0])
def test_fit(self): p = generic.fit(self.da, "lognorm") assert p.dims[0] == "dparams" assert p.get_axis_num("dparams") == 0 p0 = lognorm.fit(self.da.values[:, 0, 0]) np.testing.assert_array_equal(p[:, 0, 0], p0) # Check that we can reuse the parameters with scipy distributions cdf = lognorm.cdf(0.99, *p.values) assert cdf.shape == (self.nx, self.ny) assert p.attrs["estimator"] == "Maximum likelihood"
def test_pwm_fit(self, dist): """Test that the fitted parameters match parameters used to generate a random sample.""" pytest.importorskip("lmoments3") n = 500 dc = generic.get_dist(dist) par = self.params[dist] da = xr.DataArray( dc(**par).rvs(size=n), dims=("time", ), coords={"time": xr.cftime_range("1980-01-01", periods=n)}, ) out = generic.fit(da, dist=dist, method="PWM").compute() # Check that values are identical to lmoments3's output dict l3dc = generic.get_lm3_dist(dist) expected = l3dc.lmom_fit(da.values) for key, val in expected.items(): np.testing.assert_array_equal(out.sel(dparams=key), val, 1)
def test_synth(self): mu = 23 sigma = 2 n = 10000 per = 0.9 d = norm(loc=mu, scale=sigma) r = xr.DataArray( d.rvs(n), dims=("time", ), coords={"time": xr.cftime_range(start="1980-01-01", periods=n)}, attrs={"history": "Mosquito bytes per minute"}, ) expected = d.ppf(per) p = generic.fit(r, dist="norm") q = generic.parametric_quantile(p=p, q=per) np.testing.assert_array_almost_equal(q, expected, 1) assert "quantile" in q.coords
def test_dims_order(self): da = self.da.transpose() p = generic.fit(da) assert p.dims[-1] == "dparams"
def test_empty(self): da = self.da.copy() da[:, 0, 0] = np.nan out = generic.fit(da, "lognorm").values assert np.isnan(out[:, 0, 0]).all()
def test_genextreme_fit(self): """Check ML fit with a series that leads to poor values without good initial conditions.""" p = generic.fit(self.genextreme, "genextreme") np.testing.assert_allclose(p, (0.20949, 297.954091, 75.7911863), 1e-5)
def test_weibull_min_fit(self): """Check ML fit with a series that leads to poor values without good initial conditions.""" p = generic.fit(self.weibull_min, "weibull_min") np.testing.assert_allclose(p, (1.7760067, -322.092552, 4355.262679), 1e-5)