API

Galaxy Power Theory

class pyRSD.rsdfit.theory.GalaxyPowerTheory(param_file, model=None, extra_param_file=None, kmin=None, kmax=None)

A class representing a theory for computing a redshift-space galaxy power spectrum.

It handles the dependencies between model parameters and the evaluation of the model itself.

check(return_errors=False)

Check the values of all parameters. Here, check means that each parameter is within its bounds and the prior is not infinity

If return_errors = True, return the error messages as well

dlnprior

Return the derivative of the log prior for all “free” parameters

evaluate_grad_lnlike(theta, data, pool=None, epsilon=0.0001, numerical=False, theory_decorator={})

Evaluate the gradient.

free_fiducial

Return an array of the fiducial free parameters

get_kmu_pairs(data)

Compute the flattened k and mu values needed to evaluate the theory prediction, given the transfer functions defined by data.

This also computes the slices needed to recover the pairs for individual transfer functions.

inverse_scale(theta)

Inverse scale the free parameters, using the priors to define the scaling transformation

lnprior

Return the log prior for all “free” parameters as the sum of the priors of each individual parameter

lnprior_constrained

Return the log prior for constrained parameters as the sum of the priors of each individual parameter

lnprior_free

Return the log prior for free parameters as the sum of the priors of each individual parameter

model_callable(data, theory_decorator={})

Return the flattened theory prediction corresponding to the statistics in the data object.

Parameters:

data : PowerData

the data class, which tells the theory which statistics to compute and what basis to evaluate the theory in, e.g., multipoles, wedges, window-convoled, etc

theory_decorator : dict, optional

dictionary of decorators to apply to the theory predictions for individual data statistics

ndim

Returns the number of free parameters, i.e., the dimension of the theory

pkmu_gradient

Return the P(k,mu) gradient class

preserve(theta)

Context manager that preserves the state of the model upon exiting the context by first saving and then restoring it

scale(theta)

Scale the (unscaled) free parameters, using the priors to define the scaling transformation

scale_gradient(grad)

Scale the gradient with respect to the unscaled free parameters, using the priors to define the scaling transformation

This returns df / dxprime where xprime is the scaled param vector

set_free_parameters(theta)

Given an array of values theta, set the free parameters of attr:fit_params

Returns:

valid_model : bool

return True/False flag indicating if we were able to successfully set the free parameters and update the model

Notes

If any free parameter values are outside their bounds, the model will not be updated and False will be returned

to_file(filename, mode='w')

Save the parameters of this theory in a file

Parameter

class pyRSD.rsdfit.parameters.Parameter(name=None, value=None, vary=False, min=None, max=None, expr=None, **kwargs)

A subclass of lmfit.parameter.Parameter to represent a generic parameter.

The added functionality is largely the ability to associate a prior with the parameter. Currently, the prior can either be a uniform or normal distribution.

Parameters:

name : str

The string name of the parameter. Must be supplied

description : str, optional

The string giving the parameter description

value : object, optional

The value. This can be a float, in which case the other attributes have meaning; otherwise the class just stores the object value. Default is Parameter.fiducial

fiducial : object, optional

A fiducial value to store for this parameter. Default is None.

vary : bool, optional

Whether to vary this parameter. Default is False.

min : float, optional

The minimum allowed limit for this parameter, for use in excluding “disallowed” parameter spaces. Default is None

min : float, optional

The maximum allowed limit for this parameter, for use in excluding “disallowed” parameter spaces. Default is None

expr : {str, callable}, optional

If a str, this gives a mathematical expression used to constrain the value during the fit. Otherwise, the function will be called to evaluate the parameter’s value. Default is None

prior_name : {‘uniform’, ‘normal’}, optional

Use either a uniform or normal prior for this parameter. Default is no prior.

lower, upper : floats, optional

The bounds of the uniform prior to use

mu, sigma : floats, optional

The mean and std deviation of the normal prior to use

analytic

If True, use an analytic approximation for Uniform priors and the min/max bounds

bounded

Parameter is bounded if either min or max is defined

constrained

Parameter is constrained if the Parameter.expr == None

description

The parameter description

dlnprior

Return the derivative of the log of the prior, which is either a uniform or normal prior. If the current value is outside Parameter.min or Parameter.max, return numpy.inf

dtype

The data type of the parameter

expr

Return the mathematical expression used to constrain the value during the fit.

fiducial

A fiducial value associated with this parameter

get_value_from_prior(size=1)

Get random values from the prior, of size size

has_fiducial

Whether the parameter has a fiducial value defined

has_prior

Whether the parameter has a prior defined

lnprior

Return log of the prior, which is either a uniform or normal prior. If the current value is outside Parameter.min or Parameter.max, return numpy.inf

loc

The loc of the parameter as determined from the prior

lower

The lower limit of the uniform prior

max

The maximum allowed value (exclusive)

max_bound

The distribution representing the minimum bound

min

The minimum allowed value (inclusive)

min_bound

The distribution representing the minimum bound

mu

The mean value of the normal prior

output_value

Explicit value for output – defaults to value

prior

The prior distribution

prior_name

The name of the prior distribution

scale

The scale of the parameter as determined from the prior

scale_gradient(val)

Return scaling factor for gradient.

Parameters:

val: float

Numerical Parameter value.

Returns:

float

Scaling factor for gradient the according to Minuit-style transformation.

set(value=None, vary=None, min=None, max=None, expr=None, brute_step=None)

Set or update Parameter attributes.

Parameters:

value : float, optional

Numerical Parameter value.

vary : bool, optional

Whether the Parameter is varied during a fit.

min : float, optional

Lower bound for value. To remove a lower bound you must use -numpy.inf.

max : float, optional

Upper bound for value. To remove an upper bound you must use numpy.inf.

expr : str, optional

Mathematical expression used to constrain the value during the fit. To remove a constraint you must supply an empty string.

brute_step : float, optional

Step size for grid points in the brute method. To remove the step size you must use 0.

Notes

Each argument to set() has a default value of None, which will leave the current value for the attribute unchanged. Thus, to lift a lower or upper bound, passing in None will not work. Instead, you must set these to -numpy.inf or numpy.inf, as with:

par.set(min=None)        # leaves lower bound unchanged
par.set(min=-numpy.inf)  # removes lower bound

Similarly, to clear an expression, pass a blank string, (not None!) as with:

par.set(expr=None)  # leaves expression unchanged
par.set(expr='')    # removes expression

Explicitly setting a value or setting vary=True will also clear the expression.

Finally, to clear the brute_step size, pass 0, not None:

par.set(brute_step=None)  # leaves brute_step unchanged
par.set(brute_step=0)     # removes brute_step
set_expr_eval(evaluator)

Set expression evaluator instance.

setup_bounds()

Set up Minuit-style internal/external parameter transformation of min/max bounds.

As a side-effect, this also defines the self.from_internal method used to re-calculate self.value from the internal value, applying the inverse Minuit-style transformation. This method should be called prior to passing a Parameter to the user-defined objective function.

This code borrows heavily from JJ Helmus’ leastsqbound.py

Returns:

_val : float

The internal value for parameter from self.value (which holds the external, user-expected value). This internal value should actually be used in a fit.

sigma

The standard deviation of the normal prior

to_dict(output=False)

Convert the Parameter object to a dictionary

update(**kwargs)

Update the attributes of the Parameter

upper

The upper limit of the uniform prior

value

Return the numerical value of the Parameter, with bounds applied.

within_bounds(x=None)

Returns True if the specified value is within the (min, max) bounds and if the prior is uniform, within the lower/upper values of the prior

ParameterSet

class pyRSD.rsdfit.parameters.ParameterSet(*args, **kwargs)

A subclass of lmfit.parameter.Parameters that adds the ability to update values based on constraints in place

add(name, **kwargs)

Add a Parameter with name to the ParameterSet with the specified value.

add_many(*parlist)

Add many parameters, using a sequence of tuples.

Parameters:

parlist : sequence of tuple or Parameter

A sequence of tuples, or a sequence of Parameter instances. If it is a sequence of tuples, then each tuple must contain at least the name. The order in each tuple must be (name, value, vary, min, max, expr, brute_step).

Examples

>>>  params = Parameters()
# add with tuples: (NAME VALUE VARY MIN  MAX  EXPR  BRUTE_STEP)
>>> params.add_many(('amp', 10, True, None, None, None, None),
...                 ('cen', 4, True, 0.0, None, None, None),
...                 ('wid', 1, False, None, None, None, None),
...                 ('frac', 0.5))
# add a sequence of Parameters
>>> f = Parameter('par_f', 100)
>>> g = Parameter('par_g', 2.)
>>> params.add_many(f, g)
clear() → None. Remove all items from od.
constrained

Return a list of the constrained Parameter objects.

constrained_dtype

The list of ‘constrained’ data types for the structured array

constrained_names

Return the constrained parameter names. Constrained means that Parameter.constrained = True

constrained_values

Return the constrained parameter values.

constraint_derivative(name, wrt, theta=None)

Return the constraint derivative at the specified values of free parameters. If theta is not specified, the current values are used.

This returns d`name` / d`wrt`

Parameters:

name : str

the name of the constrained derivative to take the derivative of

wrt : str

the name of the free parameter to the derivative with respect to

theta : array_like, optional

the array of free parameters to evaluate the derivative at

Returns:

grad : float

the value of the gradient

copy()

Parameters.copy() should always be a deepcopy.

delayed_asteval()

Context manager to handle delaying asteval evaluation

dump(fp, **kws)

Write JSON representation of Parameters to a file-like object.

Parameters:

fp : file-like object

An open and .write()-supporting file-like object.

**kws : optional

Keyword arguments that are passed to dumps().

Returns:

None or int

Return value from fp.write(). None for Python 2.7 and the number of characters written in Python 3.

See also

dump, load, json.dump

dumps(**kws)

Represent Parameters as a JSON string.

Parameters:

**kws : optional

Keyword arguments that are passed to json.dumps().

Returns:

str

JSON string representation of Parameters.

See also

dump, loads, load, json.dumps

free

Return a list of the free Parameter objects. Free means that Parameter.vary = True and Parameter.constrained = False

free_dtype

The list of ‘free’ data types for the structured array

free_names

Return the free parameter names. Free means that Parameter.vary = True and Parameter.constrained = False

free_values

Return the free parameter values. Free means that Parameter.vary = True and Parameter.constrained = False

classmethod from_file(filename, tags=[])

Read a file and return a collections.defaultdict with the keys given by tags and the values given by a lmfit.Parameters object

Parameters:

filename : str

the name of the file to read parameters from

tags : list, optional

list of any parameter tags to specifically seach for

fromkeys(S[, v]) → New ordered dictionary with keys from S.

If not specified, the value defaults to None.

get(name, default=None)

Mirrors the dict.get() method behavior, but returns the parameter values

items() → a set-like object providing a view on D's items
keys() → a set-like object providing a view on D's keys
load(fp, **kws)

Load JSON representation of Parameters from a file-like object.

Parameters:

fp : file-like object

An open and .read()-supporting file-like object.

**kws : optional

Keyword arguments that are passed to loads().

Returns:

Parameters

Updated Parameters loaded from fp.

See also

dump, loads, json.load

loads(s, **kws)

Load Parameters from a JSON string.

Parameters:

**kws : optional

Keyword arguments that are passed to json.loads().

Returns:

Parameters

Updated Parameters from the JSON string.

See also

dump, dumps, load, json.loads

Notes

Current Parameters will be cleared before loading the data from the JSON string.

locs

The locs of the free parameters

move_to_end()

Move an existing element to the end (or beginning if last==False).

Raises KeyError if the element does not exist. When last=True, acts like a fast version of self[key]=self.pop(key).

pop(k[, d]) → v, remove specified key and return the corresponding

value. If key is not found, d is returned if given, otherwise KeyError is raised.

popitem($self, /, last=True)

Remove and return a (key, value) pair from the dictionary.

Pairs are returned in LIFO order if last is true or FIFO order if false.

prepare_params()

Prepare the parameters by parsing the dependencies. We initialize the ast classes in order to evaluate any constrained parameters

pretty_print(oneline=False, colwidth=8, precision=4, fmt='g', columns=['value', 'min', 'max', 'stderr', 'vary', 'expr', 'brute_step'])

Pretty-print of parameters data.

Parameters:

oneline : bool, optional

If True prints a one-line parameters representation (default is False).

colwidth : int, optional

Column width for all columns specified in columns.

precision : int, optional

Number of digits to be printed after floating point.

fmt : {‘g’, ‘e’, ‘f’}, optional

Single-character numeric formatter. Valid values are: ‘f’ floating point, ‘g’ floating point and exponential, or ‘e’ exponential.

columns : list of str, optional

List of Parameter attribute names to print.

pretty_repr(oneline=False)

Return a pretty representation of a Parameters class.

Parameters:

oneline : bool, optional

If True prints a one-line parameters representation (default is False).

Returns:

s: str

Parameters representation.

register_function(name, function)

Register a function in the symtable of the asteval attribute

scales

The scales of the free parameters

setdefault(k[, d]) → od.get(k,d), also set od[k]=d if k not in od
to_dict()

Convert the parameter set to a dictionary using (name, value) as the (key, value) pairs

to_file(filename, prefix=None, mode='w', header_name=None, footer=False, as_dict=True)

Output the ParameterSet to a file, using the mode specified. Optionally, add a header and/or footer to make it look nice.

If as_dict = True, output the parameter as a dictionary, otherwise just output the value

Parameters:

filename : str

the name of the file to write

prefix : str, optional

include a prefix when writing out parameter keys

mode : str, optional

the file mode, i.e., ‘w’ to write, ‘a’ to append

header_name : str, optional

write out a comment header first

footer : bool, optional

whether to write out a footer comment

as_dict : bool, optional

write out Parameter objects as a dictionary, instead of just the value

update([E, ]**F) → None. Update D from dict/iterable E and F.

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

update_constraints()

Update all constrained parameters, checking that dependencies are evaluated as needed.

update_fiducial()

Update the fiducial values of the constrained parameters

update_param(*name, **kwargs)

Update the Parameter specified by name with the keyword arguments provided

update_values(**kwargs)

Update the values of all parameters, checking that dependencies are evaluated as needed.

Parameters:

*args : tuple of str

the parameter names for which we want to update the parameter constraints

**kwargs :

update the values of these parameters, and then update the constraints then depend on these parameters, only if the value of the parameters changed

values() → an object providing a view on D's values
valuesdict()

Return an ordered dictionary of parameter values.

Returns:

OrderedDict

An ordered dictionary of name:value pairs for each Parameter.