def calculate_moments(self, storage: SampleStorageHDF, qspec: QuantitySpec): """ Calculate moments of given quantity for all times. :param storage: Sample HDF storage :param qspec: quantity given by QuantitySpec :return: moments means estimates, their variances; tuple of 2 np.arrays of length 3 """ n_moments = 3 means = [] vars = [] for time_id in range(len(qspec.times)): true_domain = QuantityEstimate.estimate_domain(storage, qspec, time_id, quantile=0.01) # moments_fn = moments.Legendre(n_moments, true_domain) # moments_fn = moments.Monomial(n_moments, true_domain) # compute mean in real values (without transform to ref domain) # mean_moment_fn = moments.Monomial.factory(2, domain=true_domain, ref_domain=true_domain, safe_eval=False) # q_estimator = QuantityEstimate(sample_storage=storage, sim_steps=self.step_range, # qspec=qspec, time_id=time_id) # m, v = q_estimator.estimate_moments(mean_moment_fn) # # # The first moment is in any case 1 and its variance is 0 # assert m[0] == 1 # assert v[0] == 0 # compute variance in real values (center by computed mean) # mean_moment_fn = moments.Monomial.factory(3, center=m[1]) mean_moment_fn = moments.Monomial.factory(3, domain=true_domain, ref_domain=true_domain, safe_eval=False) q_estimator = QuantityEstimate(sample_storage=storage, sim_steps=self.step_range, qspec=qspec, time_id=time_id) mm, vv = q_estimator.estimate_moments(mean_moment_fn) # The first moment is in any case 1 and its variance is 0 assert np.isclose(mm[0], 1, atol=1e-10) assert vv[0] == 0 # assert np.isclose(mm[1], 0, atol=1e-10) # means.append([1, m[1], mm[2]]) # vars.append([0, v[1], vv[2]]) means.append(mm) vars.append(vv) # print("t = ", qspec.times[time_id], " means ", mm[1], mm[2]) # print("t = ", qspec.times[time_id], " vars ", vv[1], vv[2]) return np.array(means), np.array(vars)
def set_moments(self, sample_storage): n_moments = 5 true_domain = QuantityEstimate.estimate_domain(sample_storage, quantile=0.01) return Legendre(n_moments, true_domain)