nanograv / enterprise

ENTERPRISE (Enhanced Numerical Toolbox Enabling a Robust PulsaR Inference SuitE) is a pulsar timing analysis code, aimed at noise analysis, gravitational-wave searches, and timing model analysis.
https://enterprise.readthedocs.io
MIT License
64 stars 65 forks source link

TruncNormal #322

Closed paulthebaker closed 2 years ago

paulthebaker commented 2 years ago

This implements a truncated normal parameter as enterprise.signals.parameter.TruncNormal. The truncated normal distribution matches the usual normal distribution up to normalization within a finite domain, and is zero outside of that domain.

This parameter class can be used to avoid the error described in #321.

TruncNormal handles scalar and vector parameters, including vectors where each element has a unique mean, deviation, and domain. Unlike Normal, it cannot handle a full multivariate distribution defined by a covariance matrix.

This example code block shows the prior function and output of the sampler in agreement:

N = 50_000
mu, sig, pmin, pmax = 0.50, 0.30, -1, 1
cosT = parameter.TruncNormal(mu, sig, pmin, pmax)("cosT")
samps = [cosT.sample() for ii in range(N)]

xs = np.linspace(-2, 2, 200)
pdf = cosT.get_pdf(xs)

plt.hist(samps, bins=30, histtype="step", density=True)
plt.plot(xs, pdf)

truncnorm

codecov[bot] commented 2 years ago

Codecov Report

Merging #322 (870ecfc) into master (b9d7b38) will increase coverage by 0.27%. The diff coverage is 96.77%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #322      +/-   ##
==========================================
+ Coverage   88.06%   88.33%   +0.27%     
==========================================
  Files          13       13              
  Lines        2982     3010      +28     
==========================================
+ Hits         2626     2659      +33     
+ Misses        356      351       -5     
Impacted Files Coverage Δ
enterprise/signals/parameter.py 84.05% <96.77%> (+3.81%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update b9d7b38...870ecfc. Read the comment docs.

paulthebaker commented 2 years ago

This should now correctly handle meta-parameters, by wrapping the initial normalization computation in a try / except statement. If any of the four parameters are Parameter instances, this computation raises a ValueError and _norm=None. The normalization must then be computed each time get_pdf is called.

There is also a new test that instantiates a TruncNormal with mu as a Uniform instance and calls get_pdf.