def _add_prior(self, prior, name): alpha_prior = xml.prior(prior, id="covarion_alpha_prior.s:%s" % name, name="distribution", x="@covarion_alpha.s:%s" % name) xml.Uniform(alpha_prior, id="CovAlphaUniform:%s" % name, name="distr", upper="Infinity") switch_prior = xml.prior(prior, id="covarion_s_prior.s:%s" % name, name="distribution", x="@covarion_s.s:%s" % name) gamma = xml.Gamma(switch_prior, id="Gamma.0:%s" % name, name="distr") xml.parameter(gamma, text="0.05", id="covarion_switch_gamma_param1:%s" % name, name="alpha", lower="0.0", upper="0.0") xml.parameter(gamma, text="10.0", id="covarion_switch_gamma_param2:%s" % name, name="beta", lower="0.0", upper="0.0")
def add_prior(self, prior): BaseClock.add_prior(self, prior) # Gamma prior over rates sub_prior = xml.prior(prior, id="RandomRatesPrior.c:%s" % self.name, name="distribution", x="@clockrates.c:%s" % self.name) xml.Gamma(sub_prior, id="RandomRatesPrior:%s" % self.name, name="distr", alpha="@%s" % self.shape_id, beta="@%s" % self.scale_id) # Exponential prior over Gamma scale parameter # (mean param copied from rate heterogeneity implementation in BaseModel) if self.estimate_variance: sub_prior = xml.prior(prior, id="randomClockGammaScalePrior.s:%s" % self.name, name="distribution", x="@%s" % self.shape_id) xml.Exponential(sub_prior, id="randomClockGammaScalePriorExponential.s:%s" % self.name, mean="0.23", name="distr") # Poisson prior over number of rate changes sub_prior = xml.prior(prior, id="RandomRateChangesPrior.c:%s" % self.name, name="distribution") xml.x(sub_prior, id="RandomRateChangesCount:%s" % self.name, spec="util.Sum", arg="@Indicators.c:%s" % self.name) poisson = xml.distr(sub_prior, id="RandomRatechangesPoisson.c:%s" % self.name, spec="beast.math.distributions.Poisson") xml.parameter(poisson, text="0.6931471805599453", id="RandomRateChangesPoissonLambda:%s" % self.name, estimate=False, name="lambda")
def add_prior(self, prior): # TODO: Lift some logic from beastxml.BeastXML.add_calibration # and surroundings to parse prior specifications. # Uniform prior on mean clock rate sub_prior = xml.prior(prior, id="clockPrior:%s" % self.name, name="distribution", x="@clockRate.c:%s" % self.name) self.distribution.generate_xml_element(sub_prior)
def add_prior(self, prior): # Uniform prior on mean clock rate sub_prior = xml.prior(prior, id="clockPrior:%s" % self.name, name="distribution", x="@clockRate.c:%s" % self.name) xml.Uniform(sub_prior, id="UniformClockPrior:%s" % self.name, name="distr", upper="Infinity")
def add_prior(self, beastxml): """Add a (calibrated) birth-death tree prior.""" # Tree prior attribs = {} attribs["id"] = "BirthDeathModel.t:beastlingTree" attribs["tree"] = "@Tree.t:beastlingTree" attribs[ "spec"] = "beast.evolution.speciation.BirthDeathGernhard08Model" attribs["birthDiffRate"] = "@birthRate.t:beastlingTree" attribs["relativeDeathRate"] = "@deathRate.t:beastlingTree" attribs["sampleProbability"] = "@sampling.t:beastlingTree" attribs[ "type"] = "unscaled" #TODO: Someone dropped the "restricted" type here, which does not exist. xml.distribution(beastxml.prior, attrib=attribs) # Birth rate prior attribs = {} attribs["id"] = "BirthRatePrior.t:beastlingTree" attribs["name"] = "distribution" attribs["x"] = "@birthRate.t:beastlingTree" sub_prior = xml.prior(beastxml.prior, attrib=attribs) xml.Uniform(sub_prior, id="Uniform.0", name="distr", upper="Infinity") # Relative death rate prior attribs = {} attribs["id"] = "relativeDeathRatePrior.t:beastlingTree" attribs["name"] = "distribution" attribs["x"] = "@deathRate.t:beastlingTree" sub_prior = xml.prior(beastxml.prior, attrib=attribs) xml.Uniform(sub_prior, id="Uniform.1", name="distr", upper="Infinity") # Sample probability prior attribs = {} attribs["id"] = "samplingPrior.t:beastlingTree" attribs["name"] = "distribution" attribs["x"] = "@sampling.t:beastlingTree" sub_prior = xml.prior(beastxml.prior, attrib=attribs) xml.Uniform(sub_prior, id="Uniform.3", name="distr", lower="0", upper="1")
def _add_prior(self, prior, name): switch_prior = xml.prior( prior, id="%s:pdcovarion_s_prior.s" % name, name="distribution", x="@%s:pdcovarion_s.s" % name) gamma = xml.Gamma(switch_prior, id="%s: Gamma.0" % name, name="distr") xml.parameter( gamma, text="0.05", id="%s:pdcovarion_switch_gamma_param1" % name, name="alpha", lower="0.0", upper="0.0") xml.parameter( gamma, text="10.0", id="%s:pdcovarion_switch_gamma_param2" % name, name="beta", lower="0.0", upper="0.0") origin_prior = xml.prior( prior, id="%s:pdcovarion_origin_prior.s" % name, name="distribution", x="@%s:pdcovarion_origin.s" % name) xml.Uniform( origin_prior, id="%s:PDCovOriginUniform" % name, name="distr", upper="Infinity") death_prior = xml.prior( prior, id="%s:pdcovarion_death_prior.s" % name, name="distribution", x="@{:}:pdcovarion_death.s".format(name)) xml.Exponential( death_prior, id="%s:PDCovDeathExp" % name, name="distr", mean="1.0")
def add_prior(self, prior): """ Add prior distributions for Gamma-distributed rate heterogenetiy, if configured. """ if self.rate_variation: # Gamma prior with mean 1 over all mutation rates sub_prior = xml.prior(prior, id="featureClockRatePrior.s:%s" % self.name, name="distribution") compound = xml.input(sub_prior, id="featureClockRateCompound:%s" % self.name, spec="beast.core.parameter.CompoundValuable", name="x") plate = xml.plate(compound, var="rate", range=self.all_rates) xml.var(plate, idref="featureClockRate:%s:$(rate)" % self.name) xml.input(sub_prior, id="featureClockRatePriorGamma:%s" % self.name, spec="beast.math.distributions.Gamma", name="distr", alpha="@featureClockRateGammaShape:%s" % self.name, beta="@featureClockRateGammaScale:%s" % self.name) # Exponential hyperprior on scale of Gamma prior # Exponential prior favours small scales over large scales, i.e. less rate variation # Mean scale 0.23 chosen for general sensibility, e.g.: # - Prior distribution is roughly 50/50 that ratio of fastest # to slowest feature rate in a dataset of size 200 is below # or above 10. # - Prior probability of roughly 0.90 that this ratio is below # 100. sub_prior = xml.prior( prior, id="featureClockRateGammaScalePrior.s:%s" % self.name, name="distribution", x="@featureClockRateGammaScale:%s" % self.name) xml.Exponential( sub_prior, id="featureClockRateGammaShapePriorExponential.s:%s" % self.name, mean="0.23", name="distr")
def add_prior(self, prior): RelaxedClock.add_prior(self, prior) gamma_param_prior = xml.prior(prior, id="clockRateGammaShapePrior.s:%s" % self.name, name="distribution", x="@clockRateGammaShape:%s" % self.name) xml.Exponential(gamma_param_prior, id="clockRateGammaShapePriorExponential.s:%s" % self.name, name="distr", mean="1.0")
def add_prior(self, prior): """ Add prior distributions for Gamma-distributed rate heterogenetiy, if configured. """ if self.scale_precision: precision_prior = xml.prior(prior, id="sphericalPrecisionPrior", x="@sphericalPrecision", name="distribution") xml.Uniform(precision_prior, id="sphericalPrecisionPriorUniform", name="distr", lower="0", upper="1e10")
def add_prior(self, prior): RelaxedClock.add_prior(self, prior) if self.estimate_variance: # Gamma prior on the standard deviation for lognormal dist sub_prior = xml.prior(prior, id="ucldSdev:%s" % self.name, name="distribution", x="@ucldSdev.c:%s" % self.name) gamma = xml.Gamma(sub_prior, id="uclSdevPrior:%s" % self.name, name="distr") xml.parameter(gamma, text="0.5396", id="uclSdevPriorAlpha:%s" % self.name, estimate="false", name="alpha") xml.parameter(gamma, text="0.3819", id="uclSdevPriorBeta:%s" % self.name, estimate="false", name="beta")
def add_prior(self, beastxml): """ Add Yule birth-process tree prior. """ # Tree prior ## Decide whether to use the standard Yule or the fancy calibrated one if len(beastxml.config.calibrations) == 1: yule = "calibrated" elif len(beastxml.config.calibrations) == 2: # Two calibrations can be handled by the calibrated Yule if they # are nested langs1, langs2 = [ c.langs for c in beastxml.config.calibrations.values() ] if len(set(langs1) & set(langs2)) in (len(langs1), len(langs2)): yule = "calibrated" else: yule = "standard" else: yule = "standard" attribs = {} attribs["id"] = "YuleModel.t:beastlingTree" attribs["tree"] = "@Tree.t:beastlingTree" if yule == "standard": attribs["spec"] = "beast.evolution.speciation.YuleModel" attribs["birthDiffRate"] = "@birthRate.t:beastlingTree" if "root" in beastxml.config.calibrations: attribs["conditionalOnRoot"] = "true" elif yule == "calibrated": attribs["spec"] = "beast.evolution.speciation.CalibratedYuleModel" attribs["birthRate"] = "@birthRate.t:beastlingTree" xml.distribution(beastxml.prior, attrib=attribs) # Birth rate prior sub_prior = xml.prior(beastxml.prior, id="YuleBirthRatePrior.t:beastlingTree", name="distribution", x="@birthRate.t:beastlingTree") xml.Uniform(sub_prior, id="Uniform.0", name="distr", upper="Infinity")
def add_prior(self, prior): BaseModel.add_prior(self, prior) for n, f in enumerate(self.features): fname = "%s:%s" % (self.name, f) # Boolean Rate on/off sub_prior = xml.prior(prior, id="nonZeroRatePrior.s:%s" % fname, name="distribution") xml.x(sub_prior, arg="@rateIndicator.s:%s" % fname, spec="util.Sum") N = self.valuecounts[f] if self.symmetric: offset = N-1 dim = N*(N-1)/2 else: offset = N dim = N*(N-1) if dim == offset: # In this situation (e.g. N=2, symmetric), we have no real # freedom in the number of non-zero rates. So just set a # uniform prior xml.distr( sub_prior, id="Poisson:%s.%d" % (fname, n), offset=offset, spec="beast.math.distributions.Uniform", lower="0.0", upper="Infinity") elif self.svsprior == "poisson": distr = xml.distr( sub_prior, id="Poisson:%s.%d" % (fname, n), offset=offset, spec="beast.math.distributions.Poisson") xml.parameter( distr, # Set Poisson mean equal to the midpoint of the range of sensible values text=(dim - offset) / 2.0, id="RealParameter:%s.%d.0" % (fname, n), lower="0.0", name="lambda", upper="0.0") elif self.svsprior == "exponential": # Set Exponential mean so that 99% of probability density # lies inside the sensible range # Exponential quantile function is # F(p,lambda) = -ln(1-p) / lambda exponential_mean = math.log(100.0) / (dim - offset) distr = xml.distr( sub_prior, id="Exponential:%s.%d" % (fname, n), offset=offset, spec="beast.math.distributions.Exponential") xml.parameter( distr, text=exponential_mean, id="RealParameter:%s.%d.0" % (fname, n), lower="0.0", name="mean", upper="0.0") # Relative rate sub_prior = xml.prior( prior, id="relativeGeoRatesPrior.s:%s" % fname, name="distribution", x="@relativeGeoRates.s:%s" % fname) gamma = xml.Gamma(sub_prior, id="Gamma:%s.%d.0" % (fname, n), name="distr") xml.parameter( gamma, text="1.0", id="RealParameter:%s.%d.1" % (fname, n), lower="0.0", name="alpha", upper="0.0") xml.parameter( gamma, text="1.0", id="RealParameter:%s.%d.2" % (fname, n), lower="0.0", name="beta", upper="0.0")