Bayesian inference demonstrator for MIT bag EOS
The MIT bag model, which has commonly been applied to strange quark stars, relates pressure to energy density with the simple equation of state \(p=\frac{\epsilon}{3}-\frac{4B}{3}\). There is only one parameter, the “bag constant” \(B\). This represents the vacuum energy density, which creates a “bag” in which quarks are confined. See Chodos et al. (1974).
In this notebook, we use our Bayesian inference code to constrain the value of \(B\) using observations.
(a) Import packages
[1]:
import InferenceWorkflow.BayesianSampler as sampler
import InferenceWorkflow.Likelihood as likelihood
import InferenceWorkflow.prior as prior
import math
import numpy as np
import EOSgenerators.MITbag_EOS as MITbag
from TOVsolver.unit import MeV, fm, g_cm_3, dyn_cm_2, km, Msun
import TOVsolver.main as main
(b) Set up priors
Next, we need to set up the priors. We first use a parameters array to specify the variable name. This process should be consistent with what you need to call them.
Define a prior transform function to define prior. Cube is a set of random numbers from 0 to 1. This prior setting is the standard set-up of UltraNest package, since we are using UltraNest to do nest-sampling.
We provided two options call from prior:”normal_Prior” and “flat_prior”.
We note that since we are doing Equation of state inference from mass-radius data of neutron star measurement. The central density of the star should be also sampled. Otherwise, this will be a partially-defined prior that did not span all of the parameter space, and proved to be different from full-scope inference.
This request will randomly generate a density from a EoS range, however, this process is not that trivial, since we need to determine the upper limit of the central density of the compact star — different equations of state will predict different upper bounds, so here we need to use the prior-setting EoS parameters computing the mass-radius curve for this equation of state, then find out the last stable point of this equation of state (first mass points that give the direvative to be negative). We can find that index with the len() function, then reset this max_d to be upper limit of this density range.
[2]:
parameters = ['B','d1']
def prior_transform(cube):
params = cube.copy()
params[0] = prior.flat_prior(20,100,cube[0])
B = params[0]
epsilon,p = MITbag.MITbag_compute_EOS(B)
RFSU2R = []
MFSU2R = []
density = np.logspace(14.3, 15.6, 50)
if all(x<y for x,y in zip(epsilon[:], epsilon[1:])) and all(x<y for x, y in zip(p[:], p[1:])):
MR = main.OutputMR("",epsilon,p).T
else:
MR = []
if len(MR) == False:
params[1] = 0
#this line for showing how to add one more observation
else:
for i in range(len(MR[1])):
RFSU2R.append(MR[1][i])
MFSU2R.append(MR[0][i])
if i > 20 and MR[0][i] - MR[0][i-1]< 0:
break
if len(MFSU2R)==False:
params[1] = 0
#params[3] = 0
#this line for showing how to add one more observation
else:
max_index = len(MFSU2R)
max_d = np.log10(density[max_index-1])
params[1] = 14.3 + (max_d - 14.3) * cube[1]
#params[3] = 14.3 + (max_d - 14.3) * cube[3]
#this line for showing how to add one more observation
return params
In the upper part, we define a flat (uniform) prior for the parameters in the strangeon matter equation of state, due to the lack of constraints from terrestrial experiments.
Note that the above code is an example of Bayesian analysis for a given mass and radius observation measurement. For example, if you use the NICER data for the measurements of J0030, then you should define another parameter, except the strangeon EOS parameters, e.g. “d1” for the centre density of this measurement, in the meantime add “params[2]” to this code.
If you further consider the adjoint analysis with J0030+J0740, then you should define the other two parameters, e.g. “d1” and “d2” for the centre density of these two measurements, in the meantime add “params[3]” to the above code.
Set up likehood
We need to set up a likelihood, Using standard definition way of UltraNest, that is below.
Here the likelihood is generated from a simulated mass radius measurement, which is 𝑀=1.4𝑀⊙ and 𝑅=13 km, With a 5% Mass radius measurement uncertainty,
so here
likelihood.MRlikihood_Gaussian
function will be use for our likelihood, please check likelihood.MRlikihood_Gaussian to see the original code, and more choice of likelihood. eg:
1.If we have some real mass-radius measurements, say PSR J0030 or PSR J0740, come from NICER, a KDE kernel could be trained to feed into
likelihood.MRlikihood_kernel(eps_total,pres_total,x,d1)
set the KDE kernel as a input for this function
2.If we gain measurement from radio-timing, say only measure the neutron star mass, then
likelihood.Masslikihood_Gaussian(eps_total,pres_total,x,d1)
Which will give the likelihood from single mass measurement, x is the parameters of that measurement, you should specify where this measurement mass is located and what is the sigma width of this mass measurement.
3.If we have nuclear measurements, and want to constrain this RMF model by nuclear properties like K(The Incompressibility of nuclear matter),J ( the symmetry energy at saturation density) and L( the slope of symmetry energy at saturation density). You can choose:
likelihood.Kliklihood(theta,K_low,K_up) likelihood.Jliklihood(theta,K_low,K_up) likelihood.Lliklihood(theta,K_low,K_up)
We are defaulting a hard-cut flat constrain, so if you don’t like this default hard cut, also could define the likelihood by youself with similiar style.
4.If we have a Tidal measurements from Gravitational wave detector, we can use it to do constraint:
likelihood.TidalLikihood_kernel(eps_total,pres_total,x,d1)
Where x is sampled distribution from real measurements, the standard is
kernel, chrip = x,
where the kernel is a whole set sampling from GW event, that is [chrip mass, M2/M1, tidal of M1, tidal of M2] four quantities. Chrip is the single smapling that comes only the chrip mass sampling.
[3]:
import scipy.stats as stats
def likelihood_transform(theta):
# This is a demonstration code for only introduce one constraint from one mass-radius observation.
# Could be very easy to implement more constraint from nuclear quantity, since that do not need to
# sample more central density of real neutron star. If user want to expand to two mass radius measurement
# the code could be:
B, d1 = theta
####################################################################################################################
############ This is the block to compute out all the EoS you need based on your parameters#########################
epsilon,p = MITbag.MITbag_compute_EOS(B)
####################################################################################################################
probMRgaussian = likelihood.MRlikihood_Gaussian(epsilon,p,(1.4,13,0.07,0.65),d1)
prob = probMRgaussian
return prob
Set up sampler
Here next, we define sampler, there is two different sampler we provided for.
Considering where you need resume file:
sampler.UltranestSampler and sampler.UltranestSamplerResume
Here since it is our first run, so we only use first one. Some of the sampler parameters is requested, first is step number, our choice for UltraNest sampler is slicesampler, which could easily be sliced up your total computation load, and parallelize, speed up sampling. So step as suggested by documentation of UltraNest, we use 2*len(parameters).
live_point we set 2000, it will influence the sampling precision, We suggest for 7 dimension space, maybe 5000 is a better choice, however, since my computer only have limited resources, we set 2000.
max_calls set 10000, it is how many iteration after it will stop, we suggest to set this number significantly higher, otherwise maybe will broken before the inference converging to a definite value. That result will be un-phyiscal.
[4]:
step = 2 * len(parameters)
live_point = 400
max_calls = 60000
samples = sampler.UltranestSampler(parameters,likelihood_transform,prior_transform,step,live_point,max_calls)
Creating directory for new run output/run2
[ultranest] Sampling 400 live points from prior ...
capi_return is NULL
Call-back cb_fcn_in___user__routines failed.
---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last)
Cell In[4], line 5
2 live_point = 400
4 max_calls = 60000
----> 5 samples = sampler.UltranestSampler(parameters,likelihood_transform,prior_transform,step,live_point,max_calls)
File ~/Desktop/HackWeek/CompactOject/InferenceWorkflow/BayesianSampler.py:32, in UltranestSampler(parameters, likelihood, prior, step, live_points, max_calls)
24 sampler = ultranest.ReactiveNestedSampler(parameters, likelihood, prior,log_dir='output')
25 sampler.stepsampler = ultranest.stepsampler.SliceSampler(
26 nsteps=step,
27 generate_direction=ultranest.stepsampler.generate_mixture_random_direction,
28 # adaptive_nsteps=False,
29 # max_nsteps=400
30 )
---> 32 result = sampler.run(min_num_live_points=live_points,max_ncalls= max_calls)
33 flat_samples = sampler.results['samples']
34 return flat_samples
File ~/miniconda3/envs/Thesis-Project/lib/python3.11/site-packages/ultranest/integrator.py:2459, in ReactiveNestedSampler.run(self, update_interval_volume_fraction, update_interval_ncall, log_interval, show_status, viz_callback, dlogz, dKL, frac_remain, Lepsilon, min_ess, max_iters, max_ncalls, max_num_improvement_loops, min_num_live_points, cluster_num_live_points, insertion_test_zscore_threshold, insertion_test_window, region_class, widen_before_initial_plateau_num_warn, widen_before_initial_plateau_num_max)
2279 def run(
2280 self,
2281 update_interval_volume_fraction=0.8,
(...)
2300 widen_before_initial_plateau_num_max=50000,
2301 ):
2302 r"""Run until target convergence criteria are fulfilled.
2303
2304 Parameters
(...)
2457 - converged (bool): whether the run is converged according to the MWW test, at the given threshold
2458 """
-> 2459 for _result in self.run_iter(
2460 update_interval_volume_fraction=update_interval_volume_fraction,
2461 update_interval_ncall=update_interval_ncall,
2462 log_interval=log_interval,
2463 dlogz=dlogz, dKL=dKL,
2464 Lepsilon=Lepsilon, frac_remain=frac_remain,
2465 min_ess=min_ess, max_iters=max_iters,
2466 max_ncalls=max_ncalls, max_num_improvement_loops=max_num_improvement_loops,
2467 min_num_live_points=min_num_live_points,
2468 cluster_num_live_points=cluster_num_live_points,
2469 show_status=show_status,
2470 viz_callback=viz_callback,
2471 insertion_test_window=insertion_test_window,
2472 insertion_test_zscore_threshold=insertion_test_zscore_threshold,
2473 region_class=region_class,
2474 widen_before_initial_plateau_num_warn=widen_before_initial_plateau_num_warn,
2475 widen_before_initial_plateau_num_max=widen_before_initial_plateau_num_max,
2476 ):
2477 if self.log:
2478 self.logger.debug("did a run_iter pass!")
File ~/miniconda3/envs/Thesis-Project/lib/python3.11/site-packages/ultranest/integrator.py:2560, in ReactiveNestedSampler.run_iter(self, update_interval_volume_fraction, update_interval_ncall, log_interval, dlogz, dKL, frac_remain, Lepsilon, min_ess, max_iters, max_ncalls, max_num_improvement_loops, min_num_live_points, cluster_num_live_points, show_status, viz_callback, insertion_test_window, insertion_test_zscore_threshold, region_class, widen_before_initial_plateau_num_warn, widen_before_initial_plateau_num_max)
2557 if viz_callback == 'auto':
2558 viz_callback = get_default_viz_callback()
-> 2560 self._widen_roots_beyond_initial_plateau(
2561 min_num_live_points,
2562 widen_before_initial_plateau_num_warn, widen_before_initial_plateau_num_max)
2564 Llo, Lhi = -np.inf, np.inf
2565 Lmax = -np.inf
File ~/miniconda3/envs/Thesis-Project/lib/python3.11/site-packages/ultranest/integrator.py:1435, in ReactiveNestedSampler._widen_roots_beyond_initial_plateau(self, nroots, num_warn, num_stop)
1433 user_has_been_warned = False
1434 while True:
-> 1435 self._widen_roots(nroots_needed)
1436 Ls = np.array([node.value for node in self.root.children])
1437 Lmin = np.min(Ls)
File ~/miniconda3/envs/Thesis-Project/lib/python3.11/site-packages/ultranest/integrator.py:1533, in ReactiveNestedSampler._widen_roots(self, nroots)
1531 if num_live_points_todo > 0:
1532 active_u = np.random.uniform(size=(num_live_points_todo, self.x_dim))
-> 1533 active_v = self.transform(active_u)
1534 active_logl = self.loglike(active_v)
1535 else:
File ~/miniconda3/envs/Thesis-Project/lib/python3.11/site-packages/ultranest/utils.py:137, in vectorize.<locals>.vectorized(args)
135 def vectorized(args):
136 """Vectorized version of function."""
--> 137 return np.asarray([function(arg) for arg in args])
File ~/miniconda3/envs/Thesis-Project/lib/python3.11/site-packages/ultranest/utils.py:137, in <listcomp>(.0)
135 def vectorized(args):
136 """Vectorized version of function."""
--> 137 return np.asarray([function(arg) for arg in args])
Cell In[2], line 15, in prior_transform(cube)
13 density = np.logspace(14.3, 15.6, 50)
14 if all(x<y for x,y in zip(epsilon[:], epsilon[1:])) and all(x<y for x, y in zip(p[:], p[1:])):
---> 15 MR = main.OutputMR("",epsilon,p).T
17 else:
18 MR = []
File ~/Desktop/HackWeek/CompactOject/TOVsolver/main.py:62, in OutputMR(input_file, density, pressure, central_density_range)
60 try:
61 center_rho = central_density_range[i] * G / c**2
---> 62 M, R = TOV_solver.solveTOV(center_rho, Pmin, eos, inveos)
63 Mass.append(M)
64 Radius.append(R)
File ~/Desktop/HackWeek/CompactOject/TOVsolver/solver_code.py:279, in solveTOV(center_rho, Pmin, eos, inveos)
276 sy.set_initial_value(stateTOV, r).set_f_params(inveos)
278 while sy.successful() and stateTOV[0] > Pmin:
--> 279 stateTOV = sy.integrate(sy.t + dr)
280 dpdr, dmdr = TOV(sy.t + dr, stateTOV, inveos)
281 dr = 0.46 * (1.0 / stateTOV[1] * dmdr - 1.0 / stateTOV[0] * dpdr) ** (-1.0)
File ~/miniconda3/envs/Thesis-Project/lib/python3.11/site-packages/scipy/integrate/_ode.py:431, in ode.integrate(self, t, step, relax)
428 mth = self._integrator.run
430 try:
--> 431 self._y, self.t = mth(self.f, self.jac or (lambda: None),
432 self._y, self.t, t,
433 self.f_params, self.jac_params)
434 except SystemError as e:
435 # f2py issue with tuple returns, see ticket 1187.
436 raise ValueError(
437 'Function to integrate must not return a tuple.'
438 ) from e
File ~/miniconda3/envs/Thesis-Project/lib/python3.11/site-packages/scipy/integrate/_ode.py:1176, in dopri5.run(self, f, jac, y0, t0, t1, f_params, jac_params)
1175 def run(self, f, jac, y0, t0, t1, f_params, jac_params):
-> 1176 x, y, iwork, istate = self.runner(*((f, t0, y0, t1) +
1177 tuple(self.call_args) + (f_params,)))
1178 self.istate = istate
1179 if istate < 0:
File ~/Desktop/HackWeek/CompactOject/TOVsolver/solver_code.py:74, in TOV(r, y, inveos)
62 adind = (
63 ci
64 * presgrid[idx - 1]
(...)
69 / P
70 )
71 return adind
---> 74 def TOV(r, y, inveos):
75 pres, m = y
77 # eps = 10**inveos(np.log10(pres))
KeyboardInterrupt: