Public
Edited
Mar 20, 2024
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
gini = {
var A, B;

B = d3.sum(lorenzCurve.slice(1), (d, i) => (d.x - lorenzCurve[i].x) * d.y);
A = 0.5 - B;

return { A, B, gini: 2 * A };
}
Insert cell
Insert cell
lorenzCurve = {
var x, y, array;

array = wealthPDF.map((d, i) => {
x = d3.sum(wealthPDF.slice(0, i), (d) => d.y);
y = d3.sum(wealthPDF.slice(0, i), (d) => d.x * d.y);
return { x, y };
});

array.map((d) => {
d.x /= x;
d.y /= y;
});

return array;
}
Insert cell
lorenzCurve
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
guess = {
var a, b, c, int, mu, gini;

mu = d3.mean(wealthPDF, (d) => d.x * d.y);

int = d3.sum(joint, (d) => {
a = Math.abs(d.x1 - d.x2);
b = d.y1 * d.y2;
c = d.dx1 * d.dx2;
return a * b * c;
});

gini = int / mu / 2;

return { int, mu, gini };
}
Insert cell
joint = {
var array = [];

wealthPDF.map(({ x: x1, y: y1, dx: dx1 }) => {
wealthPDF.map(({ x: x2, y: y2, dx: dx2 }) => {
array.push({ x1, x2, y1, y2, dx1, dx2 });
});
});

return array;
}
Insert cell
Insert cell
d3.sum(wealthPDF, (d) => d.dx * d.y)
Insert cell
wealthPDF = {
var rndX = d3.randomUniform(1, 2),
scaleY = d3.scaleLinear().domain([-1, 1]).range([0, 1]),
t = fixRandomSeed ? tt.t : getT(),
octave = 10,
array = [];

var x = 0,
dx,
y,
cy = 0;

// Prepare x-axis
for (let i = 0; i < dataResolution; ++i) {
dx = rndX();
x += dx;
array.push({ x, dx });
}

// Simulate raw density
array.map((d) => {
d.x /= x;
d.dx /= x;
d.y = scaleY(noiseGen.noise3D(1, d.x * octave, t));
cy += d.dx * d.y;
d.cy = cy;
});

// Normalize the density: sum(xp(x)) = 1
array.map((d) => {
d.y /= cy;
d.cy /= cy;
});

array.map((d) => (d.x += offsetX));

return array;
}
Insert cell
wealthPDF
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
getT = () => {
var t;
t = performance.now();
Object.assign(tt, { t });
return t;
}
Insert cell
tt = {
return {t: performance.now()};
}
Insert cell
noiseGen.noise3D(1, 2, performance.now() * 1e-5)
Insert cell
noiseGen = new simplexNoise(performance.now())
Insert cell
simplexNoise = require("simplex-noise@2.4.0")
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