def set_constraint(self, parameter, constraint_string=None, constraint_dict=None): r"""Set the constraint on a parameter/observable by specifying a string or a dictionary. If several constraints (e.g. several types of uncertainty) are given, the total constraint will be the convolution of the individual distributions. Existing constraints will be removed. Arguments: - parameter: parameter string (or tuple) - constraint_string: string specifying the constraint that can be e.g. of the form `'1.55(3)(1)'` or `'4.0±0.1'`. - constraint_dict: dictionary or list of several dictionaries of the form `{'distribution': 'distribution_name', 'arg1': val1, ...}`, where 'distribution_name' is a string name associated to each probability distribution (see `flavio.statistics.probability.class_from_string`) and `'arg1'`, `val1` are argument/value pairs of the arguments of the distribution class's constructor (e.g.`central_value`, `standard_deviation` for a normal distribution). `constraint_string` and `constraint_dict` must not be present simultaneously. """ if constraint_string is not None and constraint_dict is not None: raise ValueError("constraint_string and constraint_dict cannot" " be used at the same time.") if constraint_string is not None: pds = constraints_from_string(constraint_string) elif constraint_dict is not None: pds = dict2dist(constraint_dict) else: raise TypeError("Either constraint_string or constraint_dict have" " to be specified.") combined_pd = convolve_distributions(pds) self.add_constraint([parameter], combined_pd)
def set_constraint(self, parameter, constraint_string): """Set the constraints on a parameter/observable by specifying a string that can be e.g. of the form 1.55(3)(1) or 4.0±0.1. Existing constraints will be removed.""" pds = constraints_from_string(constraint_string) combined_pd = convolve_distributions(pds) self.add_constraint([parameter], combined_pd)
def set_constraint(self, parameter, constraint_string=None, constraint_dict=None): r"""Set the constraint on a parameter/observable by specifying a string or a dictionary. If several constraints (e.g. several types of uncertainty) are given, the total constraint will be the convolution of the individual distributions. Existing constraints will be removed. Arguments: - parameter: parameter string (or tuple) - constraint_string: string specifying the constraint that can be e.g. of the form `'1.55(3)(1)'` or `'4.0±0.1'`. - constraint_dict: dictionary or list of several dictionaries of the form `{'distribution': 'distribution_name', 'arg1': val1, ...}`, where 'distribution_name' is a string name associated to each probability distribution (see `flavio.statistics.probability.class_from_string`) and `'arg1'`, `val1` are argument/value pairs of the arguments of the distribution class's constructor (e.g.`central_value`, `standard_deviation` for a normal distribution). `constraint_string` and `constraint_dict` must not be present simultaneously. """ if constraint_string is not None and constraint_dict is not None: raise ValueError("constraint_string and constraint_dict cannot" " be used at the same time.") if constraint_string is not None: pds = constraints_from_string(constraint_string) elif constraint_dict is not None: pds = dict2dist(constraint_dict) else: raise TypeError("Either constraint_string or constraint_dict have" " to be specified.") combined_pd = convolve_distributions(pds) self.add_constraint([parameter], combined_pd)
def set_constraint(self, parameter, constraint_string): """Set the constraints on a parameter/observable by specifying a string that can be e.g. of the form 1.55(3)(1) or 4.0±0.1. Existing constraints will be removed.""" pds = constraints_from_string(constraint_string) combined_pd = combine_distributions(pds) self.add_constraint([parameter], combined_pd)
def _load(obj): """Read measurements from a YAML stream or file.""" measurements = yaml.load(obj) for m_name, m_data in measurements.items(): m = Measurement(m_name) for arg in ['inspire', 'hepdata', 'experiment', 'url']: if arg in m_data: setattr(m, arg, m_data[arg]) if 'correlation' not in m_data: if isinstance(m_data['values'], list): for value_dict in m_data['values']: # if "value" is a list, it contains the values of observable # arguments (like q^2) args = Observable.get_instance(value_dict['name']).arguments args_num = [value_dict[a] for a in args] constraints = constraints_from_string(value_dict['value']) error_dict = errors_from_string(value_dict['value']) central_value = error_dict['central_value'] squared_error = 0. for sym_err in error_dict['symmetric_errors']: squared_error += sym_err**2 for asym_err in error_dict['asymmetric_errors']: squared_error += asym_err[0]*asym_err[1] args_num.insert(0, value_dict['name']) obs_tuple = tuple(args_num) m.add_constraint([obs_tuple], probability.NormalDistribution(central_value, sqrt(squared_error))) else: # otherwise, 'values' is a dict just containing name: constraint_string for obs, value in m_data['values'].items(): constraints = constraints_from_string(value) for constraint in constraints: m.add_constraint([obs], constraint) else: observables = [] central_values = [] errors = [] if isinstance(m_data['values'], list): for value_dict in m_data['values']: # if "value" is a list, it contains the values of observable # arguments (like q^2) args = Observable.get_instance(value_dict['name']).arguments args_num = [value_dict[a] for a in args] error_dict = errors_from_string(value_dict['value']) args_num.insert(0, value_dict['name']) obs_tuple = tuple(args_num) observables.append(obs_tuple) central_values.append(error_dict['central_value']) squared_error = 0. for sym_err in error_dict['symmetric_errors']: squared_error += sym_err**2 for asym_err in error_dict['asymmetric_errors']: squared_error += asym_err[0]*asym_err[1] errors.append(sqrt(squared_error)) else: # otherwise, 'values' is a dict just containing name: constraint_string for obs, value in m_data['values'].items(): observables.append(obs) error_dict = errors_from_string(value) central_values.append(error_dict['central_value']) squared_error = 0. for sym_err in error_dict['symmetric_errors']: squared_error += sym_err**2 for asym_err in error_dict['asymmetric_errors']: squared_error += asym_err[0]*asym_err[1] errors.append(sqrt(squared_error)) correlation = _fix_correlation_matrix(m_data['correlation'], len(observables)) covariance = np.outer(np.asarray(errors), np.asarray(errors))*correlation if not np.all(np.linalg.eigvals(covariance) > 0): # if the covariance matrix is not positive definite, try a dirty trick: # multiply all the correlations by 0.99. n_dim = len(correlation) correlation = (correlation - np.eye(n_dim))*0.99 + np.eye(n_dim) covariance = np.outer(np.asarray(errors), np.asarray(errors))*correlation # if it still isn't positive definite, give up. assert np.all(np.linalg.eigvals(covariance) > 0), "The covariance matrix is not positive definite!" + str(covariance) m.add_constraint(observables, probability.MultivariateNormalDistribution(central_values, covariance))