Published
Edited
Jun 3, 2020
Insert cell
md`# Gauss prices`
Insert cell
html`<svg viewBox="0 0 ${width} ${height}">
<path d="${line(prices)}" fill="none" stroke="steelblue" stroke-width="1.5" stroke-miterlimit="1"></path>
${d3.select(svg`<g>`).call(xAxis).node()}
${d3.select(svg`<g>`).call(yAxis).node()}
</svg>`
Insert cell
x = d3.scaleLinear()
.domain([0, prices.length - 1])
.range([margin.left, width - margin.right])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80).tickSizeOuter(0))
Insert cell
line(prices)
Insert cell
line = d3.line()
.x((_, i) => x(i))
.y(y)
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(prices)])
.range([height - margin.bottom, margin.top])
Insert cell
prices = {
let p = 50
return Array.from(Array(1000), () => p += gauss(0, 1))
}
Insert cell
gauss = {
const { EPSILON: epsilon } = Number
const { random, sqrt, PI: pi, log, sin, cos } = Math

const tau = pi * 2

let flip = false
let z1 = 0

/** @returns normally distributed random number(s) with `mean` and `standardDeviation`. */
const randomOfNormalDistribution /*: (mean?: number, standardDeviation?: number) => number */ =
(mean = 0, standardDeviation = 1) => {
if (!(flip = !flip)) {
return (z1 * standardDeviation) + mean
}
let u1
do {
u1 = random()
} while (u1 <= epsilon)
const u2 = random()
const z0 = sqrt(-2.0 * log(u1)) * cos(tau * u2)
z1 = sqrt(-2.0 * log(u1)) * sin(tau * u2)
return (z0 * standardDeviation) + mean
}

return randomOfNormalDistribution
}
Insert cell
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(height / 40))
.call(g => g.select(".domain").remove())
Insert cell
height = 240
Insert cell
margin = ({top: 20, right: 30, bottom: 30, left: 40})
Insert cell
d3 = require("d3@5")
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