Public
Edited
Apr 21, 2024
Insert cell
Insert cell
Insert cell
Insert cell
Snake = (loglike, start, spacing) =>
{
const point2param = point => point.map((p,i) => start[i] + p*spacing[i])
// Find all points adjacent to x
const adjacents = x => {
let points = [];
x.forEach((e,i) => {
let right = [...x]; right[i] += 1; points.push(right);
let left = [...x]; left[i] -= 1; points.push(left);
});
return points;
}

const origin = start.map(() => 0.)

// Heap sorted by loglike
let surface = [{loglike: loglike(point2param(origin)), x: origin}]
let active_points = {};
active_points[origin] = true;
let internals = [];

let bestlike = surface[0].loglike

let iterations = 0;

const iterate = () => {
iterations += 1;
let a = adjacents(surface[0].x);
a = _.default.shuffle(a);
let p;
do p = a.pop(); while (p in active_points);
if(p === undefined) { // no unexplored adjacent points
internals.push(heapPop(surface));
} else {
const plike = loglike(point2param(p));
heapPush(surface, {loglike: plike, x: p});
active_points[p] = true;
if(plike > bestlike) bestlike = plike;
}

// Returns delta chi2 between global
// best fit and best fit on surface
return bestlike - surface[0].loglike
}

const samples = () => [...internals, ...surface]
.map(s => ({loglike: s.loglike, x:point2param(s.x)}));
return {iterate,
internals: () => internals.map(s => ({loglike: s.loglike, x:point2param(s.x)})),
surface: () => surface.map(s => ({loglike: s.loglike, x:point2param(s.x)})),
samples,
run: (threshold) => {while (iterate() < threshold) continue;
return bestlike - surface[0].loglike},
bestlike: () => bestlike,
delta_chi2: () => bestlike - surface[0].loglike,
}
}
Insert cell
Insert cell
Insert cell
sampler = Snake(loglike, start, spacing);
Insert cell
samples = {
run;
// sampler.run(4.0);
sampler.iterate();
return [...sampler.internals().map(d => ({...d, surface: false})), ...sampler.surface().map(d => ({...d, surface: true}))];
// return chain.map(d => ({loglike: d.post, x: [d["cosmological_parameters--omega_m"], d["distances--rs_zdrag"]]})).filter(d => d.loglike < 0)
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more