parameter - Optimization parameter definition¶
Parameter alias. |
|
A Parameter with a model-specific, calculated value. |
|
comparison operators |
|
Saved state for an unmodifiable value. |
|
Express inequality constraints between model elements |
|
Parameter expression |
|
A collection of parameter sets for a group of models. |
|
DEPRECATED |
|
Normal distribution (Gaussian) |
|
The set of operations that can be performed on parameter-like objects Parameter, Constant, Expression. |
|
Operators that can be used to construct Expressions |
|
A parameter is a container for a symbolic value. |
|
A parameter that depends on the model. |
|
Create an adaptor so that a model attribute can be treated as if it were a parameter. |
|
Parameter mixin allowing the value to be restricted to a prior probability. |
|
Uniform distribution with hard boundaries |
|
Uniform distribution with error-function PDF on boundaries |
|
User-defined functions. |
|
Values can be combined to form expressions Provide a suite of operators for creating parameter expressions. |
|
Saved state for a random variable in the model. |
|
Return the arc cosine (measured in in degrees) of x. |
|
Return the arc cosine (measured in in degrees) of x. |
|
Return the arc sine (measured in in degrees) of x. |
|
Return the arc tangent (measured in in degrees) of y/x. |
|
Return the arc tangent (measured in in degrees) of x. |
|
Return the arc sine (measured in in degrees) of x. |
|
Return the arc tangent (measured in in degrees) of y/x. |
|
Return the arc tangent (measured in in degrees) of x. |
|
make a copy of an object with parameters |
|
Return the cosine of x (measured in in degrees). |
|
Return the list of fittable parameters in no paraticular order. |
|
Format parameter set for printing. |
|
Convert a function into a delayed evaluator. |
|
make a list of <num> copies of an object with parameters |
|
Return the list of parameters (fitted or computed) that have prior probabilities associated with them. |
|
Set random values to the parameters in the parameter set, with values chosen according to the bounds. |
|
Return the sine of x (measured in in degrees). |
|
Return structure a with values substituted for all parameters. |
|
Return a stylized list of parameter names and values with range bars suitable for printing. |
|
Return the tangent of x (measured in in degrees). |
|
Return the unique set of parameters |
|
Return the list of fitted parameters in the model. |
Fitting parameter objects.
Parameters are a big part of the interface between the model and the fitting engine. By saving and retrieving values and ranges from the parameter, the fitting engine does not need to be aware of the structure of the model.
Users can also perform calculations with parameters, tying together different parts of the model, or different models.
- class bumps.parameter.Alias(obj, attr, p=None, name=None)[source]¶
Bases:
objectParameter alias.
Rather than modifying a model to contain a parameter slot, allow the parameter to exist outside the model. The resulting parameter will have the full parameter semantics, including the ability to replace a fixed value with a parameter expression.
- class bumps.parameter.Calculation(description: str = '', function: Callable = None)[source]¶
Bases:
ValueProtocolA Parameter with a model-specific, calculated value. The function used to calculate this value should be well-documented in the description field, e.g. Stack.thickness: description = “a sum of the thicknesses of all layers in the stack”
- property value¶
- class bumps.parameter.Comparisons(value)[source]¶
Bases:
Enumcomparison operators
- ge = '>='¶
- gt = '>'¶
- le = '<='¶
- lt = '<'¶
- class bumps.parameter.Constant(value: float, name: str | None = None, id: str = <factory>)[source]¶
Bases:
ValueProtocolSaved state for an unmodifiable value.
A constant is like a fixed parameter. You can’t change it’s value, set it equal to another parameter, or assign a prior distribution.
- class bumps.parameter.Constraint(a, b, op)[source]¶
Bases:
objectExpress inequality constraints between model elements
- a: Parameter | Expression | Calculation | float¶
- b: Parameter | Expression | Calculation | float¶
- fixed = True¶
- op: Comparisons¶
- property satisfied¶
- class bumps.parameter.Expression(op: str | Operators | UserFunction, args)[source]¶
Bases:
ValueProtocolParameter expression
- args: Sequence[Expression | Parameter | Calculation | float]¶
- property name¶
- op: Operators | UserFunction¶
- property value¶
- class bumps.parameter.FreeVariables(names=None, parametersets=None, **kw)[source]¶
Bases:
objectA collection of parameter sets for a group of models.
names is the set of model names.
The parameters themselves are specified as key=value pairs, with key being the attribute name which is used to retrieve the parameter set and value being a
Parametercontaining the parameter that is shared between the models.In order to evaluate the log likelihood of all models simultaneously, the fitting program will need to call set_model with the model index for each model in turn in order to substitute the values from the free variables into the model. This allows us to share a common sample across multiple data sets, with each dataset having its own values for some of the sample parameters. The alternative is to copy the entire sample structure, sharing references to common parameters and creating new parameters for each model for the free parameters. Setting up these copies was inconvenient.
Note that using FreeVariables within a fitproblem erases any caching that happens within the model. This means, for example, that the theory function will be calculated once for plotting and again for computing χ². To avoid unnecessary cache clearing, use bool(freevars) to test if there are parameters to substitute.
- parametersets: Dict[str, ParameterSet]¶
- class bumps.parameter.Function(op, *args, **kw)[source]¶
Bases:
ValueProtocolDEPRECATED
Delayed function evaluator.
f.value evaluates the function with the values of the parameter arguments at the time f.value is referenced rather than when the function was invoked.
- property value¶
- class bumps.parameter.Normal(std: float, mean: float)[source]¶
Bases:
objectNormal distribution (Gaussian)
- class bumps.parameter.OperatorMixin[source]¶
Bases:
objectThe set of operations that can be performed on parameter-like objects Parameter, Constant, Expression.
These include: +, -, , /, //, *, abs, float, int
Also, numpy math functions: sin, cos, tan, …
Much like abs(obj) => obj.__abs__(), np.sin(obj) => obj.sin()
- arccos()¶
- arccosh()¶
- arcsin()¶
- arcsinh()¶
- arctan()¶
- arctan2()¶
- arctanh()¶
- ceil()¶
- cos()¶
- cosh()¶
- degrees()¶
- exp()¶
- expm1()¶
- floor()¶
- log()¶
- log10()¶
- log1p()¶
- max()¶
- min()¶
- radians()¶
- rint()¶
- round()¶
- sin()¶
- sinh()¶
- sqrt()¶
- tan()¶
- tanh()¶
- trunc()¶
- class bumps.parameter.Operators(value)[source]¶
-
Operators that can be used to construct Expressions
- abs = 'abs'¶
- add = 'add'¶
- arccos = 'arccos'¶
- arccosh = 'arccosh'¶
- arcsin = 'arcsin'¶
- arcsinh = 'arcsinh'¶
- arctan = 'arctan'¶
- arctan2 = 'arctan2'¶
- arctanh = 'arctanh'¶
- ceil = 'ceil'¶
- cos = 'cos'¶
- cosh = 'cosh'¶
- degrees = 'degrees'¶
- exp = 'exp'¶
- expm1 = 'expm1'¶
- floor = 'floor'¶
- floordiv = 'floordiv'¶
- log = 'log'¶
- log10 = 'log10'¶
- log1p = 'log1p'¶
- max = 'max'¶
- min = 'min'¶
- mul = 'mul'¶
- neg = 'neg'¶
- pos = 'pos'¶
- pow = 'pow'¶
- radians = 'radians'¶
- rint = 'rint'¶
- round = 'round'¶
- sin = 'sin'¶
- sinh = 'sinh'¶
- sqrt = 'sqrt'¶
- sub = 'sub'¶
- tan = 'tan'¶
- tanh = 'tanh'¶
- truediv = 'truediv'¶
- trunc = 'trunc'¶
- class bumps.parameter.Parameter(value: float | Tuple[float, float] | None = None, slot: Variable | Expression | Parameter | Calculation | float | None = None, fixed: bool | None = None, name: str | None = None, id: str | None = None, limits: Tuple[float | Literal[None, '-inf'], float | Literal[None, 'inf']] | None = None, bounds: Tuple[float | Literal['-inf'], float | Literal['inf']] | None = None, distribution: Uniform | Normal | UniformSoftBounded = Uniform(), discrete: bool = False, tags: List[str] | None = None)[source]¶
Bases:
ValueProtocol,SupportsPriorA parameter is a container for a symbolic value.
Parameters have a prior probability, as set by a bounds constraint:
import numpy as np from scipy.stats.distributions import lognorm from bumps.parameter import Parameter
p = Parameter(3) p.pmp(10) # 3 +/- 10% uniform p.pmp(-5,10) # 3 in [2.85, 3.30] uniform p.pm(2) # 3 +/- 2 uniform p.pm(-1,2) # 3 in [2,5] uniform p.range(0,5) # 3 in [0,5] uniform p.dev(2) # 3 +/- 2 gaussian p.soft_range(2,5,2) # 3 in [2,5] uniform with gauss wings p.dev(2, limits=(0,6)) # 3 +/- 2 truncated gaussian p.pdf(lognorm(3, 1)) # lognormal centered on 3, width 1.
Parameters have hard limits on the possible values, dictated by the model. These bounds apply in addition to any other bounds.
Parameters can be constrained to be equal to another parameter or parameter expression:
a, b = Parameter(3), Parameter(4) p = Parameter(limits=(6, 10)) p.equals(a+b) assert p.nllf() == 0. # within the bounds a.value = 20 assert np.isinf(p.nllf()) # out of bounds
Constraints on the computed value follow from the constraints on the underlying parameters in addition to any hard limits on the parameter value given by the model.
Inputs
value can be a constant, a variable, an expression or a link to another parameter.
bounds are user-supplied limits on the parameter value within the model. If bounds are supplied then the parameter defaults to fittable.
distribution is one of Uniform, Normal or UniformSoftBounded classes
fixed is True if the parameter is fixed, even if bounds are supplied.
name is the label associated with the parameter in plots. The names need not be unique, but it will be confusing if there are duplicates. The name will usually correspond to the role of the parameter in the model. For models with sequences (e.g., layer numbers), try using a layer name (e.g., based on the material in the layer) rather than a layer number for parameters in that layer. This will make it easier for the user to associate the parameters displayed at the end of the the fit with the layer in the model. Also, when exploring the space of models, the parameter names will be preserved even if a new layer is introduced before the existing layers. That will allow the parameters from the previous fit to be easily used as a seed for the fit to the new model.
id must be a unique identifier associated with the parameter. This is used to link parameters on save and reload.
limits are hard limits on the parameter value within the model. Separate from the prior distribution on a random variable provided by the user, the hard limits are restrictions on the value imposed by the model. For example, the thickness of a layer must be zero or more.
discrete is True if the parameter value must be an integer.
tags are user-supplied labels that can be used to group parameters together for display or for setting values, and are used in the GUI for filtering parameters.
- static calculation(obj: Parameter | None, name: str, function: Callable[[], float]) Parameter[source]¶
Create a parameter to hold a value derived from the model. This can be used in parameter expressions, for example to constrain total thickness or to set the value in the next segment equal to the value at the end of a freeform segment.
Note that this function should be called in the __init__ or __post_init__ methods of the class where the parameter is defined, in order to bind the Calculation function to the newly created (or deserialized) Parameter before it is used.
If obj is a Parameter, use it - otherwise create a new Parameter obj with the given name.
Then create a Calculation object and attach the evaluator function to the Calculation, and put the Calculation in obj.slot
Returns obj: Parameter
- classmethod default(value: float | Tuple[float, float] | Expression | Parameter | Calculation, **kw) Parameter[source]¶
Create a new parameter with the value and kw attributes. If value is already a parameter or expression, set it to that value.
- dev(std, mean=None, limits=None, sigma=None, mu=None)[source]¶
Allow the parameter to vary according to a normal distribution, with deviations from the mean added to the overall cost function for the model.
If mean is None, then it defaults to the current parameter value.
If limits are provide, then use a truncated normal distribution.
Note: sigma and mu have been replaced by std and mean, but are left in for backward compatibility.
- distribution: Uniform | Normal | UniformSoftBounded¶
- equals(expression: Expression | Parameter | Calculation | float)[source]¶
Set a parameter equal to another parameter or expression.
Use
unlink()to convert from an expression to a variable.
- property fittable¶
bool(x) -> bool
Returns True when the argument x is true, False otherwise. The builtins True and False are the only two instances of the class bool. The class bool is a subclass of the class int, and cannot be subclassed.
- property fixed¶
bool(x) -> bool
Returns True when the argument x is true, False otherwise. The builtins True and False are the only two instances of the class bool. The class bool is a subclass of the class int, and cannot be subclassed.
- pm(plus, minus=None, limits=None)[source]¶
Allow the parameter to vary as value +/- delta.
pm(delta) -> [value-delta, value+delta]
pm(plus, minus) -> [value+minus, value+plus]
In the plus/minus form, one of the numbers should be plus and the other minus, but it doesn’t matter which.
If limits are provided, bound the end points of the range to lie within the limits.
The resulting range is converted to “nice” numbers.
- pmp(plus, minus=None, limits=None)[source]¶
Allow the parameter to vary as value +/- percent.
pmp(percent) -> [value*(1-percent/100), value*(1+percent/100)]
pmp(plus, minus) -> [value*(1+minus/100), value*(1+plus/100)]
In the plus/minus form, one of the numbers should be plus and the other minus, but it doesn’t matter which.
If limits are provided, bound the end points of the range to lie within the limits.
The resulting range is converted to “nice” numbers.
- residual() float[source]¶
Return the z score equivalent for the current parameter value.
That is, the given the value of the parameter in the underlying distribution, find the equivalent value in the standard normal. For a gaussian, this is the z score, in which you subtract the mean and divide by the standard deviation to get the number of sigmas away from the mean. For other distributions, you need to compute the cdf of value in the parameter distribution and invert it using the ppf from the standard normal distribution.
- slot: Variable | Expression | Parameter | Calculation | float¶
- soft_range(low, high, std)[source]¶
Allow the parameter to vary within the given range, or with Gaussian probability, stray from the range.
- property value¶
- class bumps.parameter.ParameterSet(reference: Parameter, names: List[str] | None = None, parameterlist: List[Parameter] | None = None)[source]¶
Bases:
objectA parameter that depends on the model.
- pm(*args, **kw)[source]¶
Like
Parameter.pm(), but applied to all models.
- pmp(*args, **kw)[source]¶
Like
Parameter.pmp(), but applied to all models.
- range(*args, **kw)[source]¶
Like
Parameter.range(), but applied to all models.
- property values¶
- class bumps.parameter.Reference(obj, attr, **kw)[source]¶
Bases:
ParameterCreate an adaptor so that a model attribute can be treated as if it were a parameter. This allows only direct access, wherein the storage for the parameter value is provided by the underlying model.
Indirect access, wherein the storage is provided by the parameter, cannot be supported since the parameter has no way to detect that the model is asking for the value of the attribute. This means that model attributes cannot be assigned to parameter expressions without some trigger to update the values of the attributes in the model.
NOTE: this class can not be serialized with a dataclass schema TODO: can sasmodels just use Parameter directly?
- property value¶
- class bumps.parameter.SupportsPrior[source]¶
Bases:
objectParameter mixin allowing the value to be restricted to a prior probability.
Every prior has hard limits and bounds.
- bounds: Unbounded | Bounded | BoundedAbove | BoundedBelow | BoundedNormal | SoftBounded | Normal¶
- distribution: Uniform | Normal | UniformSoftBounded¶
- prior: Unbounded | Bounded | BoundedAbove | BoundedBelow | BoundedNormal | SoftBounded | Normal | None¶
- class bumps.parameter.UniformSoftBounded(std: float)[source]¶
Bases:
objectUniform distribution with error-function PDF on boundaries
- class bumps.parameter.UserFunction(fn: Callable)[source]¶
Bases:
objectUser-defined functions.
This is a helper class for the @function decorator, which treats the operator as one of the possible expression operators.
These won’t be properly serialized/deserialized through the JSON schema unless the function is registered in advance. The schema will not include these functions as possible values even if registered, so a schema validator may fail on one of these functions.
- class bumps.parameter.ValueProtocol[source]¶
Bases:
OperatorMixinValues can be combined to form expressions Provide a suite of operators for creating parameter expressions.
- class bumps.parameter.Variable(value: float)[source]¶
Bases:
ValueProtocolSaved state for a random variable in the model.
- bumps.parameter.acos(*args)¶
- bumps.parameter.acosd(*args: Parameter | Expression | Calculation | float)¶
Return the arc cosine (measured in in degrees) of x.
- bumps.parameter.acosh(*args)¶
- bumps.parameter.arccosd(*args: Parameter | Expression | Calculation | float)[source]¶
Return the arc cosine (measured in in degrees) of x.
- bumps.parameter.arcsind(*args: Parameter | Expression | Calculation | float)[source]¶
Return the arc sine (measured in in degrees) of x.
- bumps.parameter.arctan2d(*args: Parameter | Expression | Calculation | float)[source]¶
Return the arc tangent (measured in in degrees) of y/x. Unlike atan(y/x), the signs of both x and y are considered.
- bumps.parameter.arctand(*args: Parameter | Expression | Calculation | float)[source]¶
Return the arc tangent (measured in in degrees) of x.
- bumps.parameter.asin(*args)¶
- bumps.parameter.asind(*args: Parameter | Expression | Calculation | float)¶
Return the arc sine (measured in in degrees) of x.
- bumps.parameter.asinh(*args)¶
- bumps.parameter.atan(*args)¶
- bumps.parameter.atan2(*args)¶
- bumps.parameter.atan2d(*args: Parameter | Expression | Calculation | float)¶
Return the arc tangent (measured in in degrees) of y/x. Unlike atan(y/x), the signs of both x and y are considered.
- bumps.parameter.atand(*args: Parameter | Expression | Calculation | float)¶
Return the arc tangent (measured in in degrees) of x.
- bumps.parameter.atanh(*args)¶
- bumps.parameter.copy_linked(has_parameters: HasParameters, exclude: List[Parameter] | None = None) HasParameters[source]¶
- make a copy of an object with parameters
then link all the parameters, except
those in exclude
- bumps.parameter.cosd(*args: Parameter | Expression | Calculation | float)[source]¶
Return the cosine of x (measured in in degrees).
- bumps.parameter.fittable(s)[source]¶
Return the list of fittable parameters in no paraticular order.
Note that some fittable parameters may be fixed during the fit.
- bumps.parameter.format(p, indent=0, freevars=None, field=None)[source]¶
Format parameter set for printing.
Note that this only says how the parameters are arranged, not how they relate to each other.
Note that freevars here is the substitution dictionary returned by FreeVars.get_model(i), not the FreeVars object itself.
- bumps.parameter.function(fn: Callable)[source]¶
Convert a function into a delayed evaluator.
The value of the function is computed from the values of the parameters at the time that the function value is requested rather than when the function is created.
Wrapped functions are automatically added to bumps.pmath
- bumps.parameter.make_linked_copies(has_parameters: HasParameters, num: int = 1, exclude: List[Parameter] | None = None) List[HasParameters][source]¶
- make a list of <num> copies of an object with parameters
then link all the parameters, except
those in exclude
- bumps.parameter.max(Parameter)¶
- bumps.parameter.min(Parameter)¶
- bumps.parameter.priors(s: List[Parameter]) List[Parameter][source]¶
Return the list of parameters (fitted or computed) that have prior probabilities associated with them. This may include parameters linked to expressions and parameters that are not fitted, but excludes parameters that are unbounded.
- bumps.parameter.randomize(s: List[Parameter])[source]¶
Set random values to the parameters in the parameter set, with values chosen according to the bounds.
- bumps.parameter.sind(*args: Parameter | Expression | Calculation | float)[source]¶
Return the sine of x (measured in in degrees).
- bumps.parameter.substitute(a)[source]¶
Return structure a with values substituted for all parameters.
The function traverses lists, tuples and dicts recursively. Things which are not parameters are returned directly.
- bumps.parameter.summarize(pars, sorted=False)[source]¶
Return a stylized list of parameter names and values with range bars suitable for printing.
If sorted, then print the parameters sorted alphabetically by name.
- bumps.parameter.tand(*args: Parameter | Expression | Calculation | float)[source]¶
Return the tangent of x (measured in in degrees).