Published
Edited
Mar 24, 2018
1 fork
Insert cell
Insert cell
Insert cell
anchors = [
{x: 20, y: 60},
{x: 80, y: 20}
]
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Computes a weighted sum of `points`, with weights `thetas`.
function pointSum(points, theta) {
let p = {x: 0, y: 0};
for (let i = 0; i < points.length; i++) {
p.x += theta[i] * points[i].x;
p.y += theta[i] * points[i].y;
}
return p;
}
Insert cell
Insert cell
pointsum = {
let theta = Array(n).fill(1);
return pointSum(anchors, theta);
}
Insert cell
Insert cell
Insert cell
midpoint = {
let theta = Array(n).fill(1 / n);
return pointSum(anchors, theta);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
thetaGrid = cartesianGrid(n, range(-5, 5, 0.2))
Insert cell
Insert cell
{
let points = thetaGrid.map(theta => pointSum(anchors, theta));
return plot(anchors, points);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Checks if the elements of a `theta` vector are non-negative.
function isNonNegative(theta, tol = 0.001) {
return theta.reduce((acc, cur) => acc && (cur >= -tol), true);
}
Insert cell
Insert cell
{
let points = thetaGrid
.filter(theta => isNonNegative(theta))
.map(theta => pointSum(anchors, theta))
return plot(anchors, points)
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Checks if the elements of a `theta` vector sum to 1.
function sumsToOne(theta, tol = 0.001) {
var sum = theta.reduce((acc, cur) => acc + cur);
return (sum > 1 - tol && sum < 1 + tol);
}
Insert cell
Insert cell
{
let points = thetaGrid
.filter(theta => sumsToOne(theta))
.map(theta => pointSum(anchors, theta))
return plot(anchors, points)
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
let points = thetaGrid
.filter(theta => isNonNegative(theta))
.filter(theta => sumsToOne(theta))
.map(theta => pointSum(anchors, theta))
return plot(anchors, points)
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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