pymc.find_constrained_prior#
- pymc.find_constrained_prior(distribution,lower,upper,init_guess,mass=0.95,fixed_params=None,mass_below_lower=None,**kwargs)[source]#
Find optimal parameters to getmass % of probability of a distribution betweenlower andupper.
Note: only works for one- and two-parameter distributions, as thereare exactly two constraints. Fix some combination of parametersif you want to use it on >=3-parameter distributions.
- Parameters:
- distributionDistribution
PyMC distribution you want to set a prior on.Needs to have a
logcdfmethod implemented in PyMC.- lower
float Lower bound to getmass % of probability ofpm_dist.
- upper
float Upper bound to getmass % of probability ofpm_dist.
- init_guess
dictof {strfloat} Initial guess for
scipy.optimize.least_squaresto find theoptimal parameters ofpm_dist fitting the interval constraint.Must be a dictionary with the name of the PyMC distribution’sparameter as keys and the initial guess as values.- mass
float, default 0.95 Share of the probability mass we want between
lowerandupper.Defaults to 95%.- fixed_params
strorfloat, optional, defaultNone Only used whenpm_dist has at least three parameters.Dictionary of fixed parameters, so that there are only 2 to optimize.For instance, for a StudentT, you fix nu to a constant and get the optimizedmu and sigma.
- mass_below_lower
float, optional, defaultNone The probability mass below the
lowerbound. IfNone,defaults to(1-mass)/2, which implies that the probabilitymass below thelowervalue will be equal to the probabilitymass above theuppervalue.
- Returns:
- opt_params
dict The optimized distribution parameters as a dictionary.Dictionary keys are the parameter names anddictionary values are the optimized parameter values.
- opt_params
Notes
Optional keyword arguments can be passed to
find_constrained_prior. These will bedelivered to the underlying call toscipy.optimize.minimize().Examples
# get parameters obeying constraintsopt_params=pm.find_constrained_prior(pm.Gamma,lower=0.1,upper=0.4,mass=0.75,init_guess={"alpha":1,"beta":10})# use these parameters to draw random samplessamples=pm.Gamma.dist(**opt_params,size=100).eval()# use these parameters in a modelwithpm.Model():x=pm.Gamma("x",**opt_params)# specify fixed values before optimizationopt_params=pm.find_constrained_prior(pm.StudentT,lower=0,upper=1,init_guess={"mu":5,"sigma":2},fixed_params={"nu":7},)
Under some circumstances, you might not want to have the same cumulativeprobability below the
lowerthreshold and above theupperthreshold.For example, you might want to constrain an Exponential distribution tofind the parameter that yields 90% of the mass below theupperbound,and have zero mass belowlower. You can do that with the following calltofind_constrained_prioropt_params=pm.find_constrained_prior(pm.Exponential,lower=0,upper=3.0,mass=0.9,init_guess={"lam":1},mass_below_lower=0,)
