Public
Edited
Sep 2, 2024
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
// Going from match weight to probability
Insert cell
mw_1 = {
const rounded_partial_match_weight = roundToSignificantFigures(
selected_val,
4
);
return html`${tex`\text{Partial Match Weight} = \omega = ${rounded_partial_match_weight}`}`;
}
Insert cell
bf_1 = {
const bayes_factor = roundToSignificantFigures(
calcBayesFactorFromPartialMatchWeight(selected_val),
4
);
const p_val = calcProbabilityFromPartialMatchWeight(selected_val).toFixed(4);
const rounded_partial_match_weight = roundToSignificantFigures(
selected_val,
4
);

return html`${tex`\text{Bayes Factor} = K = 2^{\omega} = 2^{{${rounded_partial_match_weight}}} = ${bayes_factor}`}`;
}
Insert cell
p_1 = {
const p_val = calcProbabilityFromPartialMatchWeight(selected_val).toFixed(4);

const partial_match_weight = roundToSignificantFigures(selected_val, 4);

return html`${tex`\text{Probability} = p = \frac{K}{1 + K} = \frac{2^{{${partial_match_weight}}}}{1 + 2^{{${partial_match_weight}}}} = ${p_val}`}`;
}
Insert cell
html`${tex`\text{Bayes Factor} = K = \frac{p}{1-p}`}`

Insert cell
// Going from probability to match weight
Insert cell
p_2 = {
const p_val = calcProbabilityFromPartialMatchWeight(selected_val);
const p_val_rounded = p_val.toFixed(4);
return html`${tex`\text{Probability} = p = ${p_val_rounded}`}`;
}
Insert cell
bf_2 = {
const p_val = calcProbabilityFromPartialMatchWeight(selected_val);
const p_val_rounded = p_val.toFixed(4);
const bayes_factor = roundToSignificantFigures(
calcBayesFactorFromProbability(p_val),
4
);

return html`${tex`\text{Bayes Factor} = K = \frac{p}{1-p} = \frac{${p_val_rounded}}{1 - ${p_val_rounded}} = ${bayes_factor}`}`;
}
Insert cell
Insert cell
viewof prior_probabability = Inputs.range([0, 1], {
label: "Prior probability"
})
Insert cell
viewof max_match_weight = Inputs.range([5, 40], {
label: "Max match weight",
step: 5,
value: 10 // Default value
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// https://observablehq.com/@visnup/vega-lite-data-out?collection=@visnup/visualization
// https://observablehq.com/@robinl/m-and-u-probabilities
Insert cell
function probToBayesFactor(prob) {
return prob !== 1 ? prob / (1 - prob) : Infinity;
}
Insert cell
function probToMatchWeight(prob) {
const bayesFactor = probToBayesFactor(prob);
const matchWeight = Math.log2(bayesFactor);

// Round to avoid floating point errors
const precision = 8; // You can adjust the precision as needed
return (
Math.round(matchWeight * Math.pow(10, precision)) / Math.pow(10, precision)
);
}
Insert cell
function matchWeightToBayesFactor(weight) {
return Math.pow(2, weight);
}
Insert cell
function bayesFactorToProb(bf) {
return bf / (1 + bf);
}
Insert cell
function formatBf(bf, mw) {
if (bf >= 1000) {
return bf.toLocaleString(undefined, { maximumFractionDigits: 0 });
} else if (bf >= 100) {
return bf.toLocaleString(undefined, { maximumFractionDigits: 1 });
} else if (bf >= 10) {
return bf.toLocaleString(undefined, { maximumFractionDigits: 2 });
} else if (bf >= 1) {
return bf
.toLocaleString(undefined, { maximumFractionDigits: 3 })
.replace(/\.?0+$/, "");
} else {
// Apply similar formatting logic as prob_fmt for bf < 1
const absMw = Math.abs(mw);
const additionalDigits = Math.floor(absMw / 5);
const minFractionDigits = 4 + additionalDigits;

return bf
.toLocaleString(undefined, { minimumFractionDigits: minFractionDigits })
.replace(/\.?0+$/, "");
}
}
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more