cluster_slug: Inference for Star Clusters
SLUG can use a library of cluster simulations to perform Bayesian inference of the
physical properties of star clusters from their observed photometry. The basic tool
for this is the cluster_slug class in slugpy.
Using cluster_slug
cluster_slug is a class that is accessible via the Python API. It works
following the general Bayesian inference method implemented in SLUG.
The basic steps required to perform inference are as follows:
Decide on a library of SLUG simulation outputs to use to perform the inference. This library defines the choice of IMF, stellar tracks, stellar atmospheres, etc., and is produced by Running a SLUG Simulation. A default library is provided for users who do not want to produce their own; see Making your own library for cluster_slug for requirements for a valid library if you want to make your own.
Create a
cluster_slugobject by loading the library, by doingfrom slugpy import cluster_slug cs = cluster_slug('libname')
where
'libname'is the name of the library to load. To use the default library, instead dofrom slugpy import cluster_slug cs = cluster_slug()
Users can optionally also specify the following keywords in the constructor:
bw_photandbw_phys: these control the bandwidths used for physical and photometric quantities in the kernel; see Choosing a Bandwidth for details. Default bandwidths are 0.1 dex for physical quantities (0.1 mag forA_V), and 0.1 mag for photometric quantities, with no correlation.inference_quantities: the list of quantities to infer from photometry; defaults to whichever of["mass", "age", "A_V", "metallicity"]vary in the library, but users can choose to infer only a subset of these, and can also choose to infer photometric quantities that have not been observed (e.g., inferring the distribution of ionizing luminosityQ(HI)from longer-wavelength bands). Mass and age are automatically logged, so for examplemassgives \(\log_{10} M\) in units of \(M_\odot\) as the inferred quantity, whileA_V(in mag) andmetallicity(\([\mathrm{Fe/H}]\), already logarithmic) are used as-is.priorsandpobs: the prior probability and the probability of observational detection for each cluster in the library; see Priors and Observation Probabilities for details.
Add a set of photometric filters on which to perform inference, by doing
cs.add_filterset(['filter1', 'filter2', ...])
where
'filter1','filter2', … are the names of photometric filters available in the library. Acluster_slugobject can have multiple filter sets loaded at once; just calladd_filtersetas many times as needed.Supply a set of observed photometry, along with an optional set of photometric uncertainties, by doing
results = cs.conditional(phot, errors=photerr)
or, if more than one filter set was loaded at the previous step,
results = cs.conditional(phot, errors=photerr, filterset=fs)
Here
fsis a list of filters specifying which filter set the photometry is for (not needed if only a single filter set is loaded, but otherwise required),photis an array of magnitudes or log luminosities (depending on the photometric system) andphoterris an array (orastropy.units.Quantity) of errors, both of lengthnfilter, wherenfilteris the number of filters in the filterset added in the previous step. The returned valueresultswill be aKDEobject giving the PDF of cluster physical properties conditional on the input photometry and photometric errors. Alternatively,photandphoterrcan be arrays of shape(M,nfilter), in which case the function will return a list ofKDEobjects of lengthM, one per row ofphot; these will be computed in parallel. The inputphotandphoterrmust be in the same photometric system as used in the library; usephot_convert(slugpy) if necessary to convert observations into the photometric system used in the library. For photometric systems using magnitudes, the inputphotmust be absolute magnitudes, not apparent ones.The
KDEobjects in results can be used to compute the full PDF of physical properties at any given set of physical parameters by doingresults.pdf(x)
where
xis an array with as many elements as the number of quantities ininference_quantities. Alternatively, the KDEs can be further reduced to produce marginal 1D KDEs for each physical property by doingmarginal_kde = results.marginalize(dims)
where
dimsis a list of integers or strings specifying which dimensions or quantities ininference_quantitiesare to be marginalized out. Thus for example to get the marginal posterior PDF of log cluster mass for the default list ofinference_quantities, one would usedims = ["age", "A_V", "metallicity"]. Alternatively, the KDEs can also be used to directly compute confidence intervals on each of theinference_quantitiesby doingresults.percentile(qty, pct)
where
qtyis a string specifying the quantity whose confidence intervals are to be computed (e.g.,"mass") andpctgives the percentile values to be computed (e.g.,[16, 50, 84]to get the 16th, 50th, and 84th percentiles). This function behaves analogously tonp.percentile.Instead of producing
KDEobjects directly, users can also use theconditional_samplefunction, by doingpts, wgts, idx = cs.conditional_sample(phot, errors=photerr, filterset=fs)
This returns the sample points, weights, and indices from which the
KDEis constructed; hereptsis an array of shape(npts, nqty), wherenptsis the number of points andnqtyis the number of quantities ininference_quantities,wgtsis an array of shapenptscontaining the weights of each of those points, andidxis an array of integers of shapenptscontaining the indices of these points in the library. This form is more convenient for writing outputs to file, and the correspondingKDEobjects can then be constructed from the points whenever needed by passingptsandwgtsto theKDEclass’s constructor, along with the desired bandwidth for inference of physical quantities (see The slug Python API).A quick way of checking whether a given set of input photometry is reproduced well by the library is to use the
neighborsfunction to find the nearest neighbors to a given cluster’s photometry in the library, by doingdist, idx = cs.neighbors(phot, n, errors=photerr)
where
nis the number of neighbors to find. As withconditional, if more than one filter set is loaded, then thefiltersetkeyword is also required to specify which filter set to use. The return valuesdistandidxare the distances and indices in the library of thennearest neighbors of the input cluster. Clusters that have at least a few neighbors with distances \(<2\) can be regarded as well-fit by the library, while clusters for which the nearest neighbor has a distance \(\gtrsim 4\) are probably not well-fit by the library. However, there is no strict goodness of fit test for a single cluster with a probabilistic model, so this is only a rule of thumb.
This is only a partial list of the capabilities of cluster_slug; see The slug Python API for
full documentation of all its methods and properties.
For a worked example that puts these steps together, inferring the masses and ages of 50 star
clusters in NGC 628 from their photometry using the default library, see the marimo notebook
examples/cluster_slug/cluster_slug_legus.py (see Examples).
The Default cluster_slug Library
Inference requires a library, which is the output of a SLUG cluster-type simulation (see Cluster Simulations and Galaxy Simulations). A default library is available, which can be obtained by the following methods depending on how you installed SLUG:
For installs from pip / PyPI:
python -m slugpy.download_data --subdir cluster_slug
For installs using a Docker container:
docker run --rm -v /path/to/slug-data:/data ghcr.io/markkrumholz/slug3:latest \
python -m slugpy.download_data --subdir cluster_slug
For installs using an Apptainer/Singularity container:
apptainer exec --bind /path/to/slug-data:/data slug3.sif python -m slugpy.download_data \
--subdir cluster_slug
If you have cloned the repository and are building from scratch, execute the following from the repository root:
python3 data/tools/download_data.py --subdir cluster_slug
WARNING: the default library is 33 GB in size because it contains a large sample of clusters for a wide range of filters. The large size is why it is not included as part of the default SLUG data download.
The default library uses the following settings:
IMF |
Chabrier (2005): lognormal below \(1\,\mathrm{M}_\odot\) (centered on \(0.2\,\mathrm{M}_\odot\), dispersion 0.55 dex), power law of slope -2.35 from \(1-120\,\mathrm{M}_\odot\); full mass range \(0.08-120\,\mathrm{M}_\odot\) |
Stellar tracks |
MIST, rotating at \(v/v_\mathrm{crit} = 0.4\), \([\alpha/\mathrm{Fe}] = 0\) |
Metallicity |
[Fe/H] drawn uniformly from -1.0 to +0.3 |
Cluster mass (CMF) |
\(dN/dM \propto M^{-1}\) from \(10^2\) to \(10^5\,\mathrm{M}_\odot\), and \(\propto M^{-2}\) from \(10^5\) to \(10^7\,\mathrm{M}_\odot\) |
Cluster age |
\(dN/dt \propto t^{-1}\) (uniform in \(\log t\)) from \(10^5 - 1.5 \times 10^{10}\) yr |
Extinction |
\(A_V\) drawn uniformly from 0 to 3 mag, Calzetti starburst extinction curve |
Nebular emission |
Included. The nebular emission’s \(A_V\) is the stellar \(A_V\) times a factor drawn from a normal distribution of mean 2.1 and dispersion 0.5, truncated to the range 1 - 100. |
Spectral synthesis |
Default spectral model, 1024-point wavelength grid from 200 Å to 20 μm |
Photometric system |
Vega magnitudes |
It includes the following filters:
HST WFC3/UVIS: F225W, F275W, F336W, F438W, F555W, F814W, F547M, F657N, F689M (
HST.WFC3_UVIS1.*)HST ACS/WFC: F435W, F555W, F606W, F814W, F658N (
HST.ACS_WFC.*)JWST NIRCam: F115W, F150W, F187N, F200W, F277W, F300M, F335M, F405N, F444W (
JWST.NIRCam.*)JWST MIRI: F560W, F770W (
JWST.MIRI.*)Ionizing photon rates:
Q(HI),Q(HeI),Q(OII)(photons/s above the H, He, and \(\mathrm{O}^+\) ionization thresholds)Bolometric luminosity:
Lbol
Note that the JWST NIRCam F200W and redder filters do not include extinction, because they are beyond the range tabulated in the Calzetti starburst extinction curve. Similarly, the ionizing photon rates do not include extinction.
Making your own library for cluster_slug
If you prefer to use your own library, you can use most cluster simulations. However, the following rules and recommendations apply:
The output must include both the
clustersandcluster_photgroups; see Output Files and Format.There can be no more than one output time per cluster, so either set the
output_timeskeyword in The SLUG Input Deck to a PDF (recommended) or a single value.In order to infer cluster mass, age, extinction, and metallicity, the
CMF,output_times,AV, andFeHparameters, respectively, in The SLUG Input Deck must be set to PDFs rather than single values; any quantity set to a single value will not be available for inference.Every filter on which inference is to be performed must be available in the library, so set the
filterskeyword in The SLUG Input Deck appropriately.If the library includes outputs with either Extinction or Nebular Emission, these will be used by default when performing inference (though this can be overridden by the
use_extinctanduse_nebularkeywords when constructing thecluster_slugobject).The size of the library, together with the typical photometric errors, will determine the bandwidth it is safe to use and the accuracy of the resulting inference (see Choosing a Bandwidth). For photometric uncertainties at the \(\approx 0.1\) mag level, libraries of \(>10^6\), and preferably \(\gtrsim 10^7\), clusters are recommended.
Priors and Observation Probabilities
The Bayesian inference method used by cluster_slug starts from a joint
PDF of physical and photometric properties of star clusters, which is formed using a weighted
kernel density estimate from the SLUG library. By default, if the priors and pobs
properties of cluster_slug are left as None (their default), all clusters in the
library are weighted equally; this is equivalent to assuming that all clusters are equally
likely to have been observed, and to adopting a prior probability for Bayesian inference that
is equal to the probability distribution from which the clusters were sampled when the library
was created, e.g., if the library was created with a CMF keyword describing a mass
distribution \(p(M) \propto M^{-2}\), then leaving priors as None is equivalent
to performing Bayesian inference assuming this prior probability for cluster masses.
Users, however, may wish to modify priors to specify a different choice of prior probability,
and to modify pobs to take into account selection bias in the observations (e.g., that
clusters with lower luminosities are less likely to be detected). The effect of each of these
parameters is to change the weights in the kernel density estimate; the weight used is
where \(w_i\) is the weight assigned to cluster \(i\) in the library,
\(P_{\mathrm{obs},i}\) is the probability that this cluster would be detected
(assumed to be unity if pobs is None), \(p_{\mathrm{prior},i}\) is the
prior probability density for the properties of cluster \(i\), and
\(p_{\mathrm{lib},i}\) is the probability density for the properties of cluster
\(i\) evaluated using the PDFs from which the library was constructed, e.g., if
the CMF and output_times keywords in The SLUG Input Deck specify PDFs
\(p_M(M)\) and \(p_T(T)\), then we will have
\(p_{\mathrm{lib},i} \propto p_M(M_i) p_T(T_i)\), where \(M_i\) and
\(T_i\) are the mass and age of cluster \(i\) in the library. Note that,
even though inference is performed on log mass and log time, the prior (and library
sampling density) are expressed in linear variables, i.e., \(p_{\mathrm{prior},i}\)
must be the probability per unit mass and per unit age, not per unit log mass and log age.
Setting priors = None is equivalent to setting
\(p_{\mathrm{prior},i} = p_{\mathrm{lib},i}\).
Note that the absolute values of the weights are not meaningful because their sum
is always normalized to unity. See
Krumholz et al. (2019)
for a derivation of the theoretical underpinnings of this functional form for the
weights.
In cluster_slug, priors and pobs are properties of the class (which can also be
set by providing keywords to the constructor), and can be set to either numpy arrays or callables.
If they are set to arrays, these arrays must have the same length as the number of
clusters in the library (available as the size property of the cluster_slug object),
and each array element must be a non-negative real number; for pobs the number must be
in \([0,1]\). If these are set to callables, the callable must accept four arguments, which will contain the
mass, age, visual extinction, and metallicity of every cluster in the library: the first three as
astropy.units.Quantity arrays of length size (the extinction argument is None if the
library has no extinction), and the metallicity \([\mathrm{Fe/H}]\) as a plain numpy array of
length size. The callable must return as output
an array of shape size containing the prior or observation probability for each cluster
(which must again be a non-negative real number, with pobs restricted to \([0,1]\)).
Choosing a Bandwidth
Bandwidth is a fundamental choice in Kernel Density Estimation. The bandwidth determines how much
“blurring” is applied to the library clusters when performing inference, and it represents a
tradeoff between noisiness and accuracy – a bandwidth that is too large will produce inferences
that are blurred out and under-specific, while one that is too small will produce results
dominated by unphysical small-scale noise. There is no general rule for choosing the “right”
bandwidth, and general rules of thumb such as Scott’s rule or Silverman’s rule generally do
not apply to the types of data upon which cluster_slug acts, which are highly-structured
and multimodal. Experimental approaches are often needed. However, there are a few principles
which can help in selecting an appropriate bandwidth for your library.
First, you want the bandwidth to be such that most of your observed clusters will have at least
a few library clusters within the kernel defined by the combination of their photometric
uncertainties and the chosen bandwidth. To judge this, cluster_slug provides the
neighbors method, which can be invoked as
dist, idx = cs.neighbors(phot, n, errors=photerr)
where n is the number of neighbors to find and phot and photerr are the photometry
and photometric errors, as in the directions above. This
will return the distances dist and indices in the library idx of the n clusters
that are closest to the input photometry, where closest is judged using the distance metric
defined by the kernel. For a good choice of bandwidth, most observed clusters should have
at least a few neighbors for which dist is below 2. If this is not the case, you should
likely increase the photometric bandwidth; conversely, if you are finding tens of neighbors
with distances below 2 you may want to decrease your bandwidth to get more precise results.
Second, you want to be able to set a photometric bandwidth that is at least slightly smaller than \(\sqrt{2}\times\) your typical photometric uncertainties without violating the first principle. This is because the bandwidth used in the estimation in the photometric dimensions is the quadrature sum of \(\sqrt{2}\times\) photometric errors and the bandwidth (in the simple case where the bandwidth and errors are both diagonal; the relationship is more complex if errors or bandwidths are correlated, but the general principle still applies). Consequently, if you are able to set a bandwidth smaller than the typical photometric errors, this means your inferences will be limited by the quality of the data, rather than by the size of your library. If you find that making the photometric bandwidth small enough to meet this condition leads to you having too few neighbors, you should probably generate a larger library. The flip side of this point is that, once you have a library large enough to satisfy this condition, there is little benefit to using an even larger one that lets you shrink the photometric bandwidth even more, since your inferences are now limited by the accuracy of the measurements.
Third, choose your physical bandwidth in reasonable proportion to your photometric one. A difference of 1 mag corresponds to 0.4 dex, so the physical bandwidth in dex should be reasonably close (within a factor of around two) to the photometric bandwidth in mag. Choosing bandwidths that are farther apart than that will either excessively blur or induce artificial noise in the inferred physical quantities.