Public
Edited
Nov 4
2 forks
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
height = 400
Insert cell
width = 400
Insert cell
Insert cell
Insert cell
// X = thepoints.map(item => item.X);
Insert cell
// Y = thepoints.map(item => item.Y);
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function generateBivariateDataset(
n = 100,
rho = 0.5, // Correlation coefficient
xMean = 50,
xStdDev = 20,
yMean = 100,
yStdDev = 20,
seed = 1337
) {
// Seeded random number generators for normal distributions
const rngX = d3.randomNormal.source(seedrandom(seed))(0, 1); // Standard normal for Zx
const rngY = d3.randomNormal.source(seedrandom(seed + 145))(0, 1); // Standard normal for Zy

// Seeded random number generator for color selection
const rngColor = d3.randomUniform.source(seedrandom(seed + 200))(0, 1); // Uniform [0, 1) for color

const colors = ["red", "blue"]; // Define possible color labels

// Generate dataset
const dataset = Array.from({ length: n }, () => {
const Zx = rngX(); // Independent standard normal for X
const Zy = rngY(); // Independent standard normal for Y
// Generate X and Y with specified correlation, without clamping
const X = xMean + xStdDev * Zx;
const Y = yMean + yStdDev * (rho * Zx + Math.sqrt(1 - rho ** 2) * Zy);

// Select color based on seeded uniform random number
const color = colors[Math.floor(rngColor() * colors.length)];

return {
X,
Y,
color
};
});

return dataset;
}
Insert cell
thepoints_alt = generateBivariateDataset(100, corr)
Insert cell
X = thepoints_alt.map(item => item.X);
Insert cell
Y = thepoints_alt.map(item => item.Y);
Insert cell
seedrandom = require("seedrandom")
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