Published
Edited
Apr 13, 2020
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function pdf_powerlaw({x, x_min, alpha}){
return ((alpha - 1)/x_min)*Math.pow(x/x_min, -alpha);
}
Insert cell
Insert cell
Insert cell
pdf_curves = {
const samples = 50;
return param_combos({
alphas: seq({start: 2, end: 3, length: 4, buffer: 0.1}),
x_mins: [2],
fn: ({x_min, alpha}) => seq({length:150, start:2, end: 15})
.map(x => ({x, x_min, alpha, val: pdf_powerlaw({x, x_min, alpha})}))
})
}
Insert cell
function param_combos({alphas, x_mins, fn}){
const n = alphas.length;
const m = x_mins.length;
let combos = [];
for(let i = 0; i < n; i++){
const alpha = alphas[i];
for(let j = 0; j < m; j++){
const x_min = x_mins[j];
combos = [...combos, ...fn({alpha, x_min})];
}
}

return combos;
}
Insert cell
function seq({length = 10, start, end, by = NaN, buffer = 0}){
const w = end - start - 2*buffer;
const x_0 = start + buffer;
return isNaN(by)
? Array.from({length}).map((_,i) => x_0 + i*w/(length - 1))
: Array.from({length: Math.ceil(w/by)+1}).map((_,i) => x_0 + i*by);
}
Insert cell
function inv_cdf_powerlaw({p, x_min, alpha}) {
return x_min * Math.pow(p, -1/(alpha - 1));
}
Insert cell
function gen_powerlaw({n, x_min, alpha}){
const gen_val = r => Math.ceil((x_min - 0.5)*Math.pow(1 - r, -1/(alpha - 1)) + 0.5)
return Array.from({length: n}).map(() => gen_val(Math.random()));
}
Insert cell
function gen_unif(n){
return Array.from({length: n}).map(() => Math.random());
}
Insert cell
import {vl} from '@vega/vega-lite-api'
Insert cell
vegalite = require("@observablehq/vega-lite@0.1")
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