Published
Edited
Apr 13, 2020
1 star
Generating data from the Power Law distribution
A graphical experiment of exponential spread
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

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