def _calculate_free_cov_msm( internal_estimates, internal_jacobian, internal_moments_cov, internal_weights, converter, method, n_samples, bounds_handling, seed, ): if method == "optimal": internal_cov = cov_optimal(internal_jacobian, internal_weights) else: internal_cov = cov_robust(internal_jacobian, internal_weights, internal_moments_cov) rng = get_rng(seed) free_cov = transform_covariance( internal_params=internal_estimates, internal_cov=internal_cov, converter=converter, n_samples=n_samples, rng=rng, bounds_handling=bounds_handling, ) return free_cov
def test_transform_covariance_invalid_bounds(): rng = get_rng(seed=1234) internal_cov = np.eye(2) converter = FakeConverter() internal_params = FakeInternalParams(lower_bounds=np.ones(2), upper_bounds=np.ones(2)) with pytest.raises(ValueError): transform_covariance( internal_params=internal_params, internal_cov=internal_cov, converter=converter, rng=rng, n_samples=10, bounds_handling="raise", )
def test_transform_covariance_no_bounds(): internal_cov = np.eye(2) converter = FakeConverter() internal_params = FakeInternalParams() got = transform_covariance( internal_params=internal_params, internal_cov=internal_cov, converter=converter, rng=get_rng(seed=5687), n_samples=100, bounds_handling="ignore", ) expected_sample = get_rng(seed=5687).multivariate_normal( np.arange(2), np.eye(2), 100) expected = np.cov(expected_sample, rowvar=False) aaae(got, expected)
def test_transform_covariance_with_clipping(): rng = get_rng(seed=1234) internal_cov = np.eye(2) converter = FakeConverter() internal_params = FakeInternalParams(lower_bounds=np.ones(2), upper_bounds=np.ones(2)) got = transform_covariance( internal_params=internal_params, internal_cov=internal_cov, converter=converter, rng=rng, n_samples=100, bounds_handling="clip", ) expected = np.zeros((2, 2)) aaae(got, expected)
def _calculate_free_cov_ml( method, internal_estimates, converter, internal_jacobian, internal_hessian, n_samples, design_info, bounds_handling, seed, ): if method == "jacobian": int_cov = cov_jacobian(internal_jacobian) elif method == "hessian": int_cov = cov_hessian(internal_hessian) elif method == "robust": int_cov = cov_robust(jac=internal_jacobian, hess=internal_hessian) elif method == "cluster_robust": int_cov = cov_cluster_robust(jac=internal_jacobian, hess=internal_hessian, design_info=design_info) elif method == "strata_robust": int_cov = cov_strata_robust(jac=internal_jacobian, hess=internal_hessian, design_info=design_info) rng = get_rng(seed) free_cov = transform_covariance( internal_params=internal_estimates, internal_cov=int_cov, converter=converter, rng=rng, n_samples=n_samples, bounds_handling=bounds_handling, ) return free_cov
jac=int_jac, hess=int_hess, design_info=design_info) if "strata_robust" in cov_cases: int_covs["cov_strata_robust"] = cov_strata_robust( jac=int_jac, hess=int_hess, design_info=design_info) # ================================================================================== # Calculate all available external covs and summaries # ================================================================================== covs = {} summaries = {} for case in cov_cases: cov = transform_covariance( params=estimates, internal_cov=int_covs[f"cov_{case}"], constraints=constraints, n_samples=n_samples, bounds_handling=bounds_handling, ) summary = calculate_inference_quantities( params=estimates, free_cov=cov, ci_level=ci_level, ) covs[f"cov_{case}"] = cov summaries[f"summary_{case}"] = summary # ================================================================================== # Calculate external jac and hess (if no transforming constraints) # ==================================================================================
) if cov_type == "jacobian": numdiff_options = numdiff_options.copy() numdiff_options["key"] = "contributions" internal_jac = first_derivative( internal_loglike, internal_params, **numdiff_options, ) internal_cov = cov_jacobian(internal_jac) else: raise NotImplementedError( "Hessian calculation is not yet implemented.") # transform internal covariance matrix free_cov = transform_covariance( params=params, internal_cov=internal_cov, constraints=constraints, n_samples=n_samples, bounds_handling=bounds_handling, ) # calculate quantities inference_quantities = calculate_inference_quantities(params=params, free_cov=free_cov) return inference_quantities, free_cov