Astromodels supports the definition of priors for all parameters in your model. You can use as prior any function (although of course not all functions should be used this way, but the choice is up to you).
First let's define a simple model containing one point source (see the "Model tutorial" for more info):
In [1]:
from astromodels import *
# Create a point source named "pts1"
pts1 = PointSource('pts1',ra=125.23, dec=17.98, spectral_shape=powerlaw())
# Create the model
my_model = Model(pts1)
Now let's assign uniform priors to the parameters of the powerlaw function. The function uniform_prior is defined like this:
In [2]:
uniform_prior.info()
We can use it as such:
In [12]:
# Set 'lower_bound' to -10, 'upper bound' to 10, and leave the 'value' parameter
# to the default value
pts1.spectrum.main.powerlaw.K.prior = log_uniform_prior(lower_bound = 1e-15, upper_bound=1e-7)
# Display it
pts1.spectrum.main.powerlaw.K.display()
# Set 'lower_bound' to -10, 'upper bound' to 0, and leave the 'value' parameter
# to the default value
pts1.spectrum.main.powerlaw.index.prior = uniform_prior(lower_bound = -10, upper_bound=0)
pts1.spectrum.main.powerlaw.index.display()
Now we can evaluate the prior simply as:
In [13]:
# Create a short cut to avoid writing too much
po = pts1.spectrum.main.powerlaw
# Evaluate the prior in 2.3e-5
point = 2.3e-21
prior_value1 = po.K.prior(point * po.K.unit)
# Equivalently we can use the fast call with no units
prior_value2 = po.K.prior.fast_call(point)
assert prior_value1 == prior_value2
print("The prior for logK evaluate to %s in %s" % (prior_value1, point))
Let's plot the value of the prior at some random locations:
In [14]:
# You need matplotlib installed for this
import matplotlib.pyplot as plt
# This is for the IPython notebook
%matplotlib inline
# Let's get 500 points uniformly distributed between -20 and 20
random_points = np.logspace(-30,2,50)
plt.loglog(random_points,pts1.spectrum.main.powerlaw.K.prior.fast_call(random_points), '.' )
#plt.xscale("log")
#plt.ylim([-0.1,1.2])
plt.xlabel("value of K")
plt.ylabel("Prior")
Out[14]:
In [ ]: