### Install UltraNest from source Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/installation.md Install UltraNest after downloading or cloning the source code using the setup.py script. ```bash $ python setup.py install ``` -------------------------------- ### Simple (Classic) Nested Sampler Setup Source: https://context7.com/johannesbuchner/ultranest/llms.txt Illustrates the setup and execution of the basic `NestedSampler` for static nested sampling. This is suitable as a baseline or for simpler problems where reactive refinement is not needed. It requires specifying `num_live_points` and uses `dlogz` and `max_iters` for convergence criteria. ```python from ultranest.integrator import NestedSampler sampler = NestedSampler( param_names=['x', 'y'], loglike=log_likelihood, transform=prior_transform, num_live_points=500, log_dir='output/simple_run', resume='overwrite', vectorized=True, ) results = sampler.run(dlogz=0.01, max_iters=100000) sampler.print_results() # logZ = -12.34 +- 0.04 ``` -------------------------------- ### Install UltraNest using pip Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/installation.md Install the latest stable release of UltraNest using pip. This is the recommended installation method. ```bash $ pip install ultranest ``` -------------------------------- ### Set up Local Development Environment Source: https://github.com/johannesbuchner/ultranest/blob/master/CONTRIBUTING.rst Commands to set up a virtual environment and install the local copy of UltraNest in development mode. ```shell $ mkvirtualenv ultranest $ cd ultranest/ $ python setup.py develop ``` -------------------------------- ### Run Sampler with Warm Start Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-warmstart.ipynb Initialize and run the `ReactiveNestedSampler` using the auxiliary parameters and functions obtained from the warm start setup. This allows the sampler to leverage the previous run's results. ```python sampler = ReactiveNestedSampler(aux_paramnames, aux_log_likelihood, aux_prior_transform, vectorized=vectorized) res = sampler.run(frac_remain=0.5) ``` -------------------------------- ### Set Up Local Development Environment Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/contributing.md Commands to set up a virtual environment and install the local copy of UltraNest in development mode. ```shell mkvirtualenv ultranest cd ultranest/ python setup.py develop ``` -------------------------------- ### Run a Subset of Tests Source: https://github.com/johannesbuchner/ultranest/blob/master/CONTRIBUTING.rst Example of how to run a specific subset of tests, in this case, tests from test_utils. ```shell $ PYTHONPATH=. pytest tests.test_utils ``` -------------------------------- ### Setup and Run Gaussian Model with UltraneSt Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-outliers.ipynb Defines parameters, prior transform, and log-likelihood for a Gaussian model. Initializes and runs the UltraneSt sampler. ```python parameters = ['mean', 'scatter'] def prior_transform(cube): params = cube.copy() params[0] = cube[0] * 200 - 100 # mean from -100 to +100 params[1] = 10**(cube[1] * 2) # scatter from 1 to 100 return params import scipy.stats def log_likelihood(params): probs_samples = scipy.stats.norm(*params).pdf(samples) probs_objects = probs_samples.mean(axis=1) loglike = np.log(probs_objects + 1e-100).sum() return loglike import ultranest sampler = ultranest.ReactiveNestedSampler(parameters, log_likelihood, prior_transform) result = sampler.run() sampler.print_results() ``` -------------------------------- ### Install Cython and UltraNest Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/installation.md If a 'ModuleNotFoundError: No module named ‘Cython’' error occurs, install Cython first, then install UltraNest. ```bash $ pip install cython $ pip install ultranest ``` -------------------------------- ### Vectorized Full Program Setup Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/performance.md This Python program demonstrates setting up a vectorized Gaussian likelihood for UltraNest, allowing specification of problem dimension and sampler parameters. ```python import argparse import numpy as np from numpy import log ``` -------------------------------- ### Basic UltraNest Program Setup and Run Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/performance.md This snippet demonstrates how to set up a basic UltraNest sampler with prior and likelihood functions, configure run parameters, and initiate the sampling process. It includes options for resuming previous runs and specifies output directories. ```python import scipy.stats paramnames = ['param1', 'param2', 'param3'] centers = [0.4, 0.5, 0.6] sigma = 0.1 def transform(cube): return cube def loglike(theta): return scipy.stats.norm(centers, sigma).logpdf(theta).sum() from ultranest import ReactiveNestedSampler sampler = ReactiveNestedSampler(paramnames, loglike, transform=transform, log_dir='my_gauss', # folder where to store files resume=True, # whether to resume from there (otherwise start from scratch) ) sampler.run( min_num_live_points=400, dlogz=0.5, # desired accuracy on logz min_ess=400, # number of effective samples update_interval_volume_fraction=0.4, # how often to update region max_num_improvement_loops=3, # how many times to go back and improve ) sampler.print_results() sampler.plot() sampler.plot_trace() ``` -------------------------------- ### Install UltraNest using conda Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/installation.md Install UltraNest using the conda package manager from the conda-forge channel. ```bash $ conda install --channel conda-forge ultranest ``` -------------------------------- ### Initialize Warm Start from Similar File Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-warmstart.ipynb Use the `warmstart_from_similar_file` function to set up an accelerated likelihood and prior transform based on a previous run's posterior samples. This function requires the posterior file path, parameter names, the new log-likelihood function, and the prior transform function. ```python from ultranest.integrator import warmstart_from_similar_file aux_paramnames, aux_log_likelihood, aux_prior_transform, vectorized = warmstart_from_similar_file( posterior_upoints_file, parameters, log_likelihood_with_background, prior_transform) ``` -------------------------------- ### Setup and Run Gaussian Model with Outliers using UltraneSt Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-outliers.ipynb Defines parameters, prior transform, and log-likelihood for a model including a fraction of outliers. Initializes and runs the UltraneSt sampler. ```python parameters2 = ['mean', 'scatter', 'w_outlier'] def prior_transform2(cube): params = cube.copy() params[0] = cube[0] * 200 - 100 # mean from -100 to +100 params[1] = 10**(cube[1] * 2) # scatter from 1 to 100 params[2] = (cube[2] * 0.2) # fraction of outliers: allow 0-20% return params def log_likelihood2(params): mean, scatter, weight = params # compute probability if following distribution probs_samples = scipy.stats.norm(mean, scatter).pdf(samples) # compute probability density, if outlier: prob_outlier = scipy.stats.uniform(-100, 200).pdf(samples) # combine the two populations with weight: probs_samples = probs_samples * (1 - weight) + weight * prob_outlier # average within each object (logical OR) probs_objects = probs_samples.mean(axis=1) # multiply across object (logical AND) loglike = np.log(probs_objects + 1e-100).sum() return loglike sampler2 = ultranest.ReactiveNestedSampler(parameters2, log_likelihood2, prior_transform2) result2 = sampler2.run() sampler2.print_results() ``` -------------------------------- ### Setup and Run Student's t-distribution Model with UltraneSt Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-outliers.ipynb Defines parameters, prior transform, and log-likelihood for a Student's t-distribution model. Initializes and runs the UltraneSt sampler. ```python parameters3 = ['mean', 'scatter', 'dof'] def prior_transform3(cube): params = cube.copy() params[0] = cube[0] * 200 - 100 # mean from -100 to +100 params[1] = 10**(cube[1] * 2) # scatter from 1 to 100 params[2] = 10**(cube[2]) # degrees of freedom in student-t: 1 (heavy tails)..10 (gaussian) return params def log_likelihood3(params): mean, scatter, dof = params # compute student-t probability probs_samples = scipy.stats.t(loc=mean, scale=scatter, df=dof).pdf(samples) # average within each object (logical OR) probs_objects = probs_samples.mean(axis=1) # multiply across object (logical AND) loglike = np.log(probs_objects + 1e-100).sum() return loglike sampler3 = ultranest.ReactiveNestedSampler(parameters3, log_likelihood3, prior_transform3) sampler3.run() sampler3.print_results() ``` -------------------------------- ### Example Output of High-Dimensional Model Run Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/performance.md This is an example of the output generated after running a high-dimensional model with slice sampling. It displays the overall log evidence (Z), likelihood ranges, evaluation counts, efficiency, number of live points, and a visualization of parameter distributions. ```text Z=0.3(43.44%) | Like=89.39..96.29 [89.3916..89.3936]*| it/evals=39671/11660719 eff=0.2939% N=400 param1 : +0.0| +0.2 0 0000 0100000000000000000000100010001000010100010000000010000 0 0 00 0 0 +0.8 | +1.0 param2 : +0.0| +0.3 0 0 0 001 0 00000100000100010110001010000010000000000000000 00 0 00 00 +0.9 | +1.0 param3 : +0.0| +0.4 00 0 0 000001010000100000010000010001010000100000000000000000000000 0 0 0 | +1.0 param4 : +0.0| +0.4 0 0 0 000000 001 0000000000100000010001100000000010010000000000 000 00| +1.0 param5 : +0.0| +0.5 0 0 000000000000 00000000000100000100011001000101100000000000000000 0000 | +1.0 param6 : +0.0| +0.4 0 0 0000000 000000010110000000110001100100000000000100000000 0 0 0 | +1.0 param7 : +0.0| +0.3 00 0 00 00000000000000010000000000110010010100000000010000000 0 0 0 0 +0.8 | +1.0 param8 : +0.0| +0.1 0 0 100 00101000000000000000010010000010110000000000010000000 0 0 0 0 +0.7 | +1.0 param9 : +0.00| 0 000 00000000000000100000100011100010010010000000100000000000000 00 0 +0.59 | +1.00 param10 : +0.00| 00000000000001010100000100100101010000000000000100000000 00 000 +0.50 | +1.00 param11 : +0.00| 0 0 00000000 0000010000000101001000000101000001010000000000 0 000 0 +0.51 | +1.00 param12 : +0.00| 0 0 00 0000 00000100000000000010000010110010000001001000000000 000 00 0 0 00 +0.61 | +1.00 param13 : +0.0| +0.2 0 00 0 0000000000000010010000000110000110000000000010001100 000 00 0 0 +0.7 | +1.0 param14 : +0.0| +0.3 0 0 0 00000000000000001100000001010001001000010001000000000 001000 0 0 +0.9 | +1.0 param15 : +0.0| +0.4 00000000000000010011000000000010010000000010000100010000 00 1 0000 +0.9 | +1.0 param16 : +0.0| +0.4 01 000 010 00000000010001100010000000000000000000000100010000000 00 | +1.0 param17 : +0.0| +0.4 0 0000 0 00000000000000010000000011000110000111000000000000000000000 0 0| +1.0 param18 : +0.0| +0.4 01 0 000 0000000010100000000100100010001000100000100000000000 000 0 0 | +1.0 param19 : +0.0| +0.2 0 0 0 0 000 0000000010101100000001101100000000000000000000000 00000000 000 0 +0.9 | +1.0 param20 : +0.0| +0.1 0 0 000000 0001000000000100000010000000010100000011001000000 00000010 +0.7 | +1.0 param21 : +0.00| 0 010 00000000000000000110010000100000110000010000 0000000000 1 +0.63 | +1.00 param22 : +0.00| 00 0 0000001 00000100000010001110110100000000000000000000000000000 00 0 +0.58 | +1.00 param23 : +0.000| 0 0000000000000000000001000110000010011000110000000000000 0 00 0 0 0 +0.597 | +1.000 param24 : +0.000|0 00000 00001000000010000000010000000001000001001011000010000 000 0 +0.577 | +1.000 param25 : +0.00| 0 0 1 0000000010000000000010100000000010001011010000000000000 0 0 0 +0.64 | +1.00 ``` -------------------------------- ### Visualize Reference and Warm-Start Results Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-warmstart.ipynb Plot the spectral flux density data with error bars, overlaying the predictions from the reference run and the warm-started run. This visualization helps compare the results and assess the effectiveness of warm starting. ```python plt.figure(figsize=(10, 5)) plt.errorbar(x=wavelength, y=y_obs, yerr=sigma, marker='x', ls=' ') from ultranest.plot import PredictionBand band = PredictionBand(wavelength) for T, ampl in results_ref['samples']: band.add(black_body_model(wavelength, ampl, T)) band.line(color='k') band.shade(color='k', alpha=0.5) band = PredictionBand(wavelength) for T, ampl, _ in res['samples']: band.add(black_body_model(wavelength, ampl, T)) band.line(color='orange') band.shade(color='orange', alpha=0.5) plt.plot(wavelength, y_true, ':', color='gray') plt.ylabel('Spectral flux density [Jy]'); plt.xlabel('Wavelength [$\mu$m]'); ``` -------------------------------- ### Calculate and Print Speed-up Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-warmstart.ipynb Calculate and print the percentage speed-up achieved by using warm starting compared to a standard run, based on the number of calls to the log-likelihood function. ```python print("Speed-up of warm-start: %d%%" % ((results_ref['ncall'] / res['ncall'] - 1)*100)) ``` -------------------------------- ### Vectorized Loglikelihood and Ultranest Setup Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/performance.md Demonstrates setting up a vectorized loglikelihood function and initializing ReactiveNestedSampler with vectorized=True. This approach processes multiple points simultaneously, reducing function call overhead. Ensure your likelihood function is designed to accept and return arrays for this to be effective. ```python import argparse import numpy as np from ultranest import ReactiveNestedSampler import ultranest.stepsampler # define command line arguments: parser = argparse.ArgumentParser() parser.add_argument('--x_dim', type=int, default=2, help="Dimensionality") parser.add_argument("--num_live_points", type=int, default=400) parser.add_argument('--sigma', type=float, default=0.1) parser.add_argument('--slice', action='store_true') parser.add_argument('--slice_steps', type=int, default=100) parser.add_argument('--log_dir', type=str, default='logs/loggauss') args = parser.parse_args() ndim = args.x_dim sigma = args.sigma width = max(0, 1 - 5 * sigma) centers = (np.sin(np.arange(ndim)/2.) * width + 1.) / 2. # Here, we implement a vectorized loglikelihood, which can # process many points at the same time. This reduces function calls. def loglike(theta): like = -0.5 * (((theta - centers)/sigma)**2).sum(axis=1) - 0.5 * np.log(2 * np.pi * sigma**2) * ndim return like def transform(x): return x paramnames = ['param%d' % (i+1) for i in range(ndim)] # set up nested sampler: sampler = ReactiveNestedSampler(paramnames, loglike, transform=transform, log_dir=args.log_dir + 'RNS-%dd' % ndim, resume=True, vectorized=True) if args.slice: # set up step sampler. Here, we use a differential evolution slice sampler: sampler.stepsampler = ultranest.stepsampler.SliceSampler( nsteps=args.slice_steps, generate_direction=ultranest.stepsampler.generate_mixture_random_direction, ) # run sampler, with a few custom arguments: sampler.run(dlogz=0.5 + 0.1 * ndim, update_interval_volume_fraction=0.4 if ndim > 20 else 0.2, max_num_improvement_loops=3, min_num_live_points=args.num_live_points) sampler.print_results() if args.slice: sampler.stepsampler.plot(filename = args.log_dir + 'RNS-%dd/stepsampler_stats_regionslice.pdf' % ndim) sampler.plot() ``` -------------------------------- ### Load Reference Run Posterior File Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-warmstart.ipynb Specify the path to the posterior un-transformed samples file from a previous reference run. This file is used for warm starting. ```python posterior_upoints_file = reference_run_folder + '/chains/weighted_post_untransformed.txt' ``` -------------------------------- ### Clone UltraNest repository Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/installation.md Clone the UltraNest public repository from GitHub to get the source code. ```bash $ git clone git://github.com/JohannesBuchner/ultranest ``` -------------------------------- ### Test MPI Communication Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/debugging.ipynb Run a simple MPI command to verify that your MPI installation is working correctly and that cores can communicate. This command should output ranks and total size. ```bash !mpiexec -np 4 python3 -c 'from mpi4py import MPI; print(MPI.COMM_WORLD.Get_rank(), MPI.COMM_WORLD.Get_size())' ``` -------------------------------- ### Parallelise with MPI using mpiexec Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/performance.md Launch multiple UltraNest scripts in parallel using MPI and mpiexec. No code changes are required in your script, but you need MPI and mpi4py installed. This scales UltraNest to multiple processors and cores. ```bash mpiexec -np 4 python3 gauss.py --x_dim=100 --num_live_points=400 --slice --slice_steps=100 ``` -------------------------------- ### Prepare and Save Untransformed Posterior Data Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-warmstart.ipynb Creates and saves a 'custom-weighted_post_untransformed.txt' file containing weights, log likelihoods, and untransformed posterior samples (u-space). This file is formatted for use with the warm start functionality. ```python weights = np.ones((len(usamples), 1)) / len(usamples) logl = np.zeros(len(usamples)).reshape((-1, 1)) np.savetxt( 'custom-weighted_post_untransformed.txt', np.hstack((weights, logl, usamples)), header=' '.join(['weight', 'logl'] + parameters), fmt='%f' ) ``` -------------------------------- ### Get Minimum Likelihood Threshold Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/debugging.ipynb Access the minimum likelihood threshold value from the sampler object. ```python sampler.Lmin ``` -------------------------------- ### Initialize and Run Sampler Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/using-ultranest.ipynb Initializes the ReactiveNestedSampler with parameter names, likelihood, and prior transform, then runs the sampling process. Requires ultranest. ```python import ultranest sampler = ultranest.ReactiveNestedSampler(param_names, my_likelihood, my_prior_transform) ``` ```python result = sampler.run() sampler.print_results() ``` -------------------------------- ### Get Samples Shape Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-line.ipynb Prints the shape of the generated samples array. This is a utility to check the dimensions of the data after resampling. ```python samples.shape ``` -------------------------------- ### Initialize and run UltraNest sampler Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-warmstart.ipynb Initializes the UltraNest ReactiveNestedSampler with the defined parameters, likelihood, prior transform, and output directory. It then runs the sampler, resuming if a previous run exists. ```python from ultranest import ReactiveNestedSampler reference_run_folder = 'blackbody-alldata' sampler_ref = ReactiveNestedSampler(parameters, log_likelihood, prior_transform, log_dir=reference_run_folder, resume='overwrite') results_ref = sampler_ref.run(frac_remain=0.5) sampler_ref.print_results() ``` -------------------------------- ### Get Shape of Bootstrapped Weights Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-line.ipynb Retrieves the shape of the bootstrapped weights from the result object. This is useful for understanding the structure of the uncertainty emulation. ```python result['weighted_samples']['bootstrapped_weights'].shape ``` -------------------------------- ### Untransform Posterior Samples to U-Space Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-warmstart.ipynb Converts posterior samples from p-space to u-space using optimization. This is a crucial step for enabling warm starts with arbitrary priors. ```python nparams = len(parameters) u = np.ones(nparams) * 0.5 stdevs = posterior_samples.std(axis=0) def minfunc(ui, i, u, pi): if not 0 < ui < 1: return 1e100 u[i] = ui p = prior_transform(u) return (p[i] - pi)**2 usamples = np.empty((len(posterior_samples), nparams)) for j, sample in enumerate(tqdm.tqdm(posterior_samples)): for i, param in enumerate(parameters): ui0 = np.interp(sample[i], pguess[:,i], uguess) result = scipy.optimize.minimize_scalar( minfunc, args=(i, u, sample[i]), method='brent', bracket=(ui0 - 1e-4, ui0, ui0 + 1e-4), tol=0.001 * stdevs[i], ) usamples[j,i] = result.x ``` -------------------------------- ### Advanced Sampler Initialization Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/using-ultranest.ipynb Demonstrates advanced initialization options for ReactiveNestedSampler, including parameter properties, derived parameters, and output logging/resuming. ```python sampler = ultranest.ReactiveNestedSampler( param_names, loglike=my_likelihood, transform=my_prior_transform, ## additional parameter properties: # identify circular parameters wrapped_params=[False, False, False], # add derived calculations derived_param_names=[], # store outputs for resuming: log_dir='my_folder/, resume='resume' or 'overwrite' or 'subfolder', ) ``` -------------------------------- ### Reparameterized Prior Transform Function Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/debugging.ipynb Define a reparameterized prior transform function for a model with 'Bfrac' and 'A1' as parameters. This example shows how to rescale and derive parameters. ```python parameters_reparametrized = ['Bfrac', 'A1', 'P1', 't1'] def prior_transform_reparametrized(cube): params = cube.copy() # amplitude: params[1] = 10**(cube[1] * 3 - 1) # background is scaled by params[1] params[0] = cube[0] * params[1] # rest is unchanged params[2] = 10**(cube[1] * 2) params[3] = cube[3] return params ``` -------------------------------- ### Deploying UltraNest Source: https://github.com/johannesbuchner/ultranest/blob/master/CONTRIBUTING.rst Commands for maintainers to version bump, commit, push, and tag for deployment. Travis CI handles PyPI deployment. ```shell $ bump2version patch # possible: major / minor / patch $ git push $ git push --tags ``` -------------------------------- ### Calculate Average Prediction Power Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-line.ipynb Compute the mean of the Kpredicts array to get an overall measure of the line model's predictive advantage over the constant model. ```python np.mean(Kpredicts) ``` -------------------------------- ### Warm-start from a Similar File Source: https://context7.com/johannesbuchner/ultranest/llms.txt Accelerates a new sampling run by reusing posterior samples from a similar previous run using `warmstart_from_similar_file`. This function wraps the likelihood in an auxiliary parameterization to focus sampling on the previously explored region. ```python from ultranest.integrator import warmstart_from_similar_file, ReactiveNestedSampler ``` -------------------------------- ### Run UltraNest Sampler Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-sine-bayesian-workflow.ipynb Initializes and runs the UltraNest sampler with the defined parameters, log-likelihood, and prior transform. This performs the nested sampling to explore the parameter space and obtain posterior distributions. ```python import ultranest sampler = ultranest.ReactiveNestedSampler(parameters, log_likelihood, prior_transform) result = sampler.run() ``` -------------------------------- ### Initialize Ultranest Sampler Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-line.ipynb Initializes the ultranest.ReactiveNestedSampler with parameter names, the log-likelihood function, and the prior transform function. This sets up the Bayesian inference process. ```python import ultranest sampler = ultranest.ReactiveNestedSampler(parameters, log_likelihood, prior_transform) ``` -------------------------------- ### Initialize Ultranest Sampler Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-sine-line.ipynb Initializes the Ultranest ReactiveNestedSampler with the defined parameters, log-likelihood function, prior transform, and specifies which parameters are wrapped. ```python from ultranest import ReactiveNestedSampler sampler = ReactiveNestedSampler(parameters, log_likelihood, prior_transform, wrapped_params=[False, False, False, True], ) ``` -------------------------------- ### Run UltraNest Sampler for Linear Model Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-line.ipynb Initializes and runs the UltraNest ReactiveNestedSampler with the defined parameters, log-likelihood, and prior transform for the linear model. ```python sampler0 = ultranest.ReactiveNestedSampler(parameters0, log_likelihood0, prior_transform0) result0 = sampler0.run() ``` -------------------------------- ### Initialize UltraNest Sampler for 1-Component Model Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-sine-highd.ipynb Initializes the UltraNest ReactiveNestedSampler for the 1-component sine model. It specifies the parameter names, the log-likelihood function, the prior transform, and indicates which parameters are periodic. ```python import ultranest sampler1 = ultranest.ReactiveNestedSampler( parameters1, log_likelihood1, prior_transform1, wrapped_params=[False, False, False, True], ) ``` -------------------------------- ### Initialize UltraNest Sampler for 2-Component Model Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-sine-highd.ipynb Initializes the UltraNest ReactiveNestedSampler for the 2-component sine model. It includes all seven parameters, the corresponding log-likelihood and prior transform functions, and specifies periodic parameters. ```python sampler2 = ultranest.ReactiveNestedSampler( parameters2, log_likelihood2, prior_transform2, wrapped_params=[False, False, False, True, False, False, True], ) ``` -------------------------------- ### Define Sine Model and Generate Data Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/debugging.ipynb Sets up the sine wave model and generates synthetic data for testing. Ensure numpy, scipy, matplotlib, ultranest, and corner are imported. ```python import numpy as np import scipy.stats import matplotlib.pyplot as plt import ultranest import corner from numpy import sin, pi def sine_model1(t, B, A1, P1, t1): return A1 * sin((t / P1 + t1) * 2 * pi) + B np.random.seed(42) n_data = 50 # time of observations t = np.random.uniform(0, 5, size=n_data) # measurement values yerr = 1.0 y = np.random.normal(sine_model1(t, B=1.0, A1=4.2, P1=3, t1=0), yerr) ``` -------------------------------- ### Initialize UltraNest Samplers Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-sine-modelcomparison.ipynb Initializes `ReactiveNestedSampler` instances for both the one-component (`sampler0`) and two-component (`sampler1`) sine models. This sets up the nested sampling process. ```python import ultranest sampler1 = ultranest.ReactiveNestedSampler(parameters1, log_likelihood1, prior_transform1) sampler0 = ultranest.ReactiveNestedSampler(parameters0, log_likelihood0, prior_transform0) ``` -------------------------------- ### Warm-start a Nested Sampling Run Source: https://context7.com/johannesbuchner/ultranest/llms.txt Use this to initialize a new run with parameters from a previous run. Ensure the likelihood and transform functions are compatible. ```python from ultranest.utils import warmstart_from_similar_file from ultranest import ReactiveNestedSampler # Suppose we have a previous run of a similar model saved in model1/ aux_param_names, aux_loglike, aux_transform, vectorized = warmstart_from_similar_file( usample_filename='model1/chains/weighted_post_untransformed.txt', param_names=['mean', 'sigma'], loglike=log_likelihood_new_data, # updated likelihood transform=prior_transform, vectorized=True, min_num_samples=50, ) # Run with the auxiliary parameterization to hot-start aux_sampler = ReactiveNestedSampler( aux_param_names, aux_loglike, transform=aux_transform, vectorized=vectorized, log_dir='output/warmstart_run', ) aux_results = aux_sampler.run(min_num_live_points=400) # The last column in samples is the auxiliary parameter print("Auxiliary posterior samples:", aux_results['samples'].shape) ``` -------------------------------- ### Configure and Run Slice Sampler Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-sine-highd.ipynb Configures and runs a SliceSampler for nested sampling. Adjust `nsteps` based on parameter count and initial results for consistency. ```python nsteps = 2 * len(parameters2) # create step sampler: sampler2.stepsampler = ultranest.stepsampler.SliceSampler( nsteps=nsteps, generate_direction=ultranest.stepsampler.generate_mixture_random_direction, # adaptive_nsteps=False, # max_nsteps=400 ) # run again: result2 = sampler2.run(min_num_live_points=400) sampler2.print_results() ``` -------------------------------- ### Import fastKDE Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-line.ipynb Imports the fastKDE library, which is used for kernel density estimation. ```python from fastkde import fastKDE ``` -------------------------------- ### Run UltraNest Sampler for Model 0 Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-sine-modelcomparison.ipynb Executes the nested sampling run for the simpler sine model (`sampler0`) with a specified minimum number of live points. It then prints the results. ```python result0 = sampler0.run(min_num_live_points=400) sampler0.print_results() ``` -------------------------------- ### Run UltraNest Sampler for Model 1 Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-sine-modelcomparison.ipynb Executes the nested sampling run for the first sine model (`sampler1`) with a specified minimum number of live points. It then prints the results. ```python result1 = sampler1.run(min_num_live_points=400) sampler1.print_results() ``` -------------------------------- ### Run UltraNest Sampler with Interruption Handling Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/debugging.ipynb Initializes and runs the `ReactiveNestedSampler` from UltraNest. It includes a try-except block to catch the `TimeoutError` raised by the signal handler, allowing inspection of the sampler's state upon interruption. ```python import ultranest sampler = ultranest.ReactiveNestedSampler(parameters, log_likelihood, prior_transform, wrapped_params=[False, False, False, True]) try: sampler.run() except TimeoutError: print("run interrupted!") signal.signal(signal.SIGALRM, old_handler); ``` -------------------------------- ### Run ReactiveNestedSampler with Vectorized Log-Likelihood Source: https://context7.com/johannesbuchner/ultranest/llms.txt Demonstrates setting up and running the primary `ReactiveNestedSampler` with a vectorized log-likelihood function and prior transform. Use this for standard Bayesian inference tasks where likelihood evaluations can be batched. Ensure `log_dir` is set for saving checkpoints and results, and `resume='resume'` to continue previous runs. ```python import numpy as np import ultranest # 1. Define the prior transform: maps unit cube [0,1]^n -> physical space def prior_transform(u): # u: array of shape (n_samples, n_params) # Returns physical parameters: mean in [-5, 5], sigma in [0.01, 100] (log-uniform) mean = u[:, 0] * 10 - 5 sigma = 10 ** (u[:, 1] * 4 - 2) return np.column_stack([mean, sigma]) # 2. Define a vectorized log-likelihood data = np.random.normal(2.0, 0.5, size=50) def log_likelihood(params): # params: array of shape (n_samples, 2) -> [mean, sigma] mean = params[:, 0] sigma = params[:, 1] return np.sum( -0.5 * ((data[:, None] - mean) / sigma) ** 2 - np.log(sigma), axis=0 ) # 3. Create and run the sampler sampler = ultranest.ReactiveNestedSampler( param_names=['mean', 'sigma'], loglike=log_likelihood, transform=prior_transform, log_dir='output/gauss_run', # saves checkpoints & results here resume='resume', # resume if previous run exists vectorized=True, # loglike/transform accept batches ) results = sampler.run( min_num_live_points=400, # minimum live points throughout dlogz=0.5, # target log-evidence uncertainty dKL=0.5, # target KL divergence accuracy min_ess=400, # target effective sample size max_num_improvement_loops=3, # reactive improvement passes ) sampler.print_results() # logZ = -12.345 +- 0.032 # mean : 2.01 +- 0.07 # sigma : 0.50 +- 0.05 print("log Z =", results['logz'], "±", results['logzerr']) print("Posterior samples shape:", results['samples'].shape) # (N, 2) print("Effective sample size:", results['ess']) print("MWW insertion test converged:", results['insertion_order_MWW_test']['converged']) ``` -------------------------------- ### Run Nested Sampling Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-sine-line.ipynb Executes the nested sampling process using the initialized sampler. Sets parameters for the number of live points and convergence criteria. ```python result = sampler.run(min_num_live_points=400, dKL=np.inf, min_ess=100) ``` -------------------------------- ### Sample and Visualize Gibbs Prior Samples Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/priors.ipynb Generate samples using the transform_correlated_gibbs function and visualize them. This demonstrates how a conditional prior approach can also result in a correlated prior distribution. ```python samples = transform_correlated_gibbs(np.random.uniform(0, 1, size=(100, 2))) plt.figure() plt.title('Gibbs prior') plt.plot(samples[:,0], samples[:,1], 'o', mew=1, mfc='w', mec='k') plt.xlabel('Parameter 1') plt.ylabel('Parameter 2'); ``` -------------------------------- ### Sample from Prior for Debugging Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/debugging.ipynb Generates 1000 samples from the prior distribution to visualize and debug the prior transform. This helps identify issues like parameter correlations. ```python p = [prior_transform(np.random.uniform(size=ndim)) for i in range(1000)] ``` -------------------------------- ### Import Optimization and Progress Libraries Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-warmstart.ipynb Imports necessary libraries for optimization and progress tracking, which are used in the process of untransforming posterior samples to u-space. ```python import scipy.optimize ``` ```python import tqdm ``` -------------------------------- ### Comparing Posterior Distributions with GetDist Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-outliers.ipynb Loads and prepares samples from different UltraneSt models for comparison using the GetDist library. ```python from getdist import MCSamples, plots samples_g = MCSamples(samples=sampler.results['samples'], names=sampler.results['paramnames'], label='Gaussian', settings=dict(smooth_scale_2D=3), sampler='nested') samples_o = MCSamples(samples=sampler2.results['samples'][:,:2], names=sampler2.results['paramnames'][:2], label='Gaussian + Outlier', settings=dict(smooth_scale_2D=3), sampler='nested') samples_t = MCSamples(samples=sampler3.results['samples'][:,:2], ``` -------------------------------- ### Initialize PopulationSliceSampler for GPU Source: https://context7.com/johannesbuchner/ultranest/llms.txt Initializes a PopulationSliceSampler for vectorized GPU likelihoods. The `popsize` determines the number of walkers per batch, and `generate_direction` specifies the direction generation strategy. ```python from ultranest.popstepsampler import PopulationSliceSampler, generate_mixture_random_direction sampler = ultranest.ReactiveNestedSampler( param_names=['p%d' % i for i in range(10)], loglike=gpu_log_likelihood, # vectorized GPU function transform=prior_transform, vectorized=True, ndraw_min=100, ) sampler.stepsampler = PopulationSliceSampler( popsize=100, # number of walkers per batch nsteps=10, generate_direction=generate_mixture_random_direction, ) results = sampler.run(min_num_live_points=400) print("ncall:", results['ncall']) ``` -------------------------------- ### Generate Sample Data Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/using-ultranest.ipynb Generates synthetic data with a Gaussian signal and noise for demonstration purposes. Requires numpy. ```python import numpy as np x = np.linspace(400, 800, 100) yerr = 1.0 y = np.random.normal(20 * np.exp(-0.5 * ((x-500)/4.2)**2), yerr) ``` -------------------------------- ### Download UltraNest tarball Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/installation.md Download the master tarball of the UltraNest source code from GitHub. ```bash $ curl -OJL https://github.com/JohannesBuchner/ultranest/tarball/master ``` -------------------------------- ### Import UltraNest Step Sampler Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-sine-highd.ipynb Imports the necessary module for using step samplers within UltraNest. Step samplers are employed for more efficient exploration of parameter space in high-dimensional or complex problems. ```python import ultranest.stepsampler ``` -------------------------------- ### Run Ultranest Sampler Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-line.ipynb Executes the ultranest sampler with specified minimum live points and effective sample size. This performs the nested sampling to explore the parameter space. ```python result = sampler.run(min_num_live_points=50, min_ess=100) # you can increase these numbers later ``` -------------------------------- ### Sample and Visualize Transformed Priors Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/priors.ipynb Generates samples from uniform and Gaussian priors by transforming random uniform quantiles and visualizes their distributions using histograms. ```python uniform_samples = transform_1d_uniform(np.random.uniform(0, 1, size=100000)) gauss_samples = transform_1d(np.random.uniform(0, 1, size=100000)) plt.figure() plt.hist(uniform_samples, bins=20, histtype='step', density=True, label='Uniform') plt.hist(gauss_samples, bins=100, histtype='step', density=True, label='Gauss') plt.xlabel('Model parameter x') plt.ylabel('Density') plt.legend() ``` -------------------------------- ### Plot Posterior Samples Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-warmstart.ipynb Visualizes posterior samples obtained from a reference run. Useful for understanding the initial distribution of parameters in p-space. ```python posterior_samples = results_ref['samples'] plt.scatter(posterior_samples[:,0], posterior_samples[:,1]); plt.xlabel('%s (p-space)' % parameters[0]) plt.ylabel('%s (p-space)' % parameters[1]); ``` -------------------------------- ### UltraNest Live Point Display Explanation Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/issues.md Details the live point display, including mono-modal and expected volume estimations, and parameter plots showing live points distribution. ```text Mono-modal Volume: ~exp(-5.89) * Expected Volume: exp(-2.02) param1: +0.0| ********************************* | +1.0 param2: +0.0| ********************************* | +1.0 ... ``` ```text how many how large the ow large the clusters volume should be MLFriends region | | | Mono-modal Volume: ~exp(-5.89) * Expected Volume: exp(-2.02) For each parameter you will find a simple linear plot of the live points: param1: +0.0| ********************************* | +1.0 | | | | where live points are | | lower value upper value parameter name Live points are shown as *, or numbers, which indicate which cluster they belong to. Sometimes too many clusters are being found, but that does not make the result incorrect. Increasing the number of live points can avoid this (use >100). ``` -------------------------------- ### Implementing Log-Likelihood Function (Manual Calculation) Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/performance.md Implementing the log-likelihood function manually can offer further performance improvements by avoiding overhead from library functions. ```python def loglike(theta): like = -0.5 * (((theta - centers)/sigma)**2).sum() - 0.5 * np.log(2 * np.pi * sigma**2) * ndim return like ``` -------------------------------- ### Import Libraries for Prior Specification Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/priors.ipynb Imports necessary libraries for numerical operations, statistical distributions, and plotting, which are essential for defining and visualizing priors. ```python import numpy as np import scipy.stats import matplotlib.pyplot as plt ``` -------------------------------- ### Deploy to PyPI Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/contributing.md Commands for maintainers to deploy a new version to PyPI after committing changes and updating HISTORY.rst. This includes version bumping, pushing to git, and pushing tags. ```shell bump2version patch # possible: major / minor / patch git push git push --tags ``` -------------------------------- ### Commit and Push Changes Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/contributing.md Commands to stage all changes, commit them with a descriptive message, and push the branch to GitHub. ```shell git add . git commit -m "Your detailed description of your changes." git push origin name-of-your-bugfix-or-feature ``` -------------------------------- ### Plot Prior Transform Visualization Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-warmstart.ipynb Visualizes the relationship between u-space and p-space for the first two parameters based on generated prior transform samples. Helps in understanding how priors are transformed. ```python plt.subplot(2, 1, 1) plt.plot(uguess, pguess[:,0]) plt.xlabel('u-space (%s)' % parameters[0]) plt.ylabel('p-space (%s)' % parameters[0]); plt.subplot(2, 1, 2) plt.plot(uguess, pguess[:,1]) plt.xlabel('u-space (%s)' % parameters[1]) plt.ylabel('p-space (%s)' % parameters[1]); ``` -------------------------------- ### Sample from Posterior Distribution Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/priors.ipynb Generates samples from a combined uniform and normal distribution to simulate a posterior from a previous experiment. This is useful when the prior is not easily invertible. ```python posterior_samples = np.hstack((np.random.uniform(0, 3, 2000), np.random.normal(3, 0.2, 2000))) plt.figure(figsize=(4,2)) plt.hist(posterior_samples, histtype='step', bins=100); ``` -------------------------------- ### Commit and Push Changes Source: https://github.com/johannesbuchner/ultranest/blob/master/CONTRIBUTING.rst Commands to stage, commit, and push your local changes to your GitHub fork. ```shell $ git add . $ git commit -m "Your detailed description of your changes." $ git push origin name-of-your-bugfix-or-feature ``` -------------------------------- ### Display Head of Saved File Source: https://github.com/johannesbuchner/ultranest/blob/master/docs/example-warmstart.ipynb Prints the first few lines of the generated 'custom-weighted_post_untransformed.txt' file to verify its content and format. ```bash !head custom-weighted_post_untransformed.txt ``` -------------------------------- ### Clone the UltraNest Repository Source: https://github.com/johannesbuchner/ultranest/blob/master/CONTRIBUTING.rst Use this command to clone your forked UltraNest repository locally. ```shell $ git clone git@github.com:JohannesBuchner/UltraNest.git ```