Published
Edited
Aug 9, 2019
Importers
1 star
Insert cell
Insert cell
y = estimate`${x.value}*120 + logGuess(10, 20)`
Insert cell
x = estimate`logGuess(2, 20)`
Insert cell
Distributions = new Object({
beta: jStat.beta.sample,
centralF: jStat.centralF.sample,
cauchy: jStat.cauchy.sample,
chisquare: jStat.chisquare.sample,
exponential: jStat.exponential.sample,
invgamma: jStat.invgamma.sample,
lognormal: jStat.lognormal.sample,
logGuess: function(low, high) {
const logHigh = mathjs.log(high)
const logLow = mathjs.log(low)

const mean = mathjs.mean(logHigh, logLow)
const stdev = (logHigh-logLow) / (2*1.645)

return jStat.lognormal.sample(mean, stdev)
},
normal: jStat.normal.sample,
normGuess: function(low, high){
const mean = mathjs.mean(high, low)
const stdev = (high - mean) / 1.645
return jStat.normal.sample(mean, stdev)
},
studentt: jStat.studentt.sample,
weibull: jStat.weibull.sample,
uniform: jStat.uniform.sample,
gamma: jStat.gamma.sample,
pert: function(min, mode, max, lambda = 4) {
const width = max - min
const a = 1 + lambda * ((mode - min)/width)
const b = 1 + lambda * ((max - mode)/width)
const p = jStat.beta.sample(a, b)
return min + p*width
}
})
Insert cell
function histogram(text, data) {
const height = 200
const margin = ({top: 30, right: 40, bottom: 30, left: 40})

const x = d3.scaleLinear()
.domain([jStat.percentile(data, 0.01), jStat.percentile(data, 0.99)])
.range([margin.left, width - margin.right])
const bins = d3.histogram()
.domain(x.domain())
.thresholds(x.ticks(40))
(data)

const y = d3.scaleLinear()
.domain([0, d3.max(bins, d => d.length)]).nice()
.range([height - margin.bottom, margin.top])

const xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(d3.scaleLinear().domain(x.domain()).range([50, width - 50]))
.ticks(width / 80)
.tickSizeOuter(0))

const svg = d3.select(DOM.svg(width, height)).attr("font-family", "sans-serif")
const color = "steel"
const bar = svg.append("g")
.attr("fill", color)
.selectAll("rect")
.data(bins)
.join("rect")
.attr("x", d => x(d.x0) + 1)
.attr("width", d => Math.max(0, x(d.x1) - x(d.x0) - 1))
.attr("y", d => y(d.length))
.attr("height", d => y(0) - y(d.length))

let percs = percentiles(data)
let mean = mathjs.mean(data)
let basicStats = svg.append("g")
.attr("transform", `translate(${margin.left}, 0)`)
basicStats
.append("text")
.attr("dy", "30")
.attr("font-size", "30")
.attr("font-weight", "bold")
.text(numberShow(mean))
basicStats
.append("text")
.attr("dy", 60)
.attr("font-size", "20")
.text(numberShow(percs["5%"]) + " to " + numberShow(percs["95%"]))
let tableWidth=100
let tableHeight=height-margin.top-margin.bottom
let percsTable = svg.append("g")
.attr("transform", `translate(${width-margin.right-tableWidth},0)`)
let table = percsTable
.append("foreignObject")
.attr("width", tableWidth)
.attr("height", tableHeight )
.append("xhtml:body")
.append("table")
table.append("thead").append("tr").append("th")
.attr("colspan", 2)
.attr("font-weight", "bold")
.text("Percentiles")
let percKeys = Object.keys(percs)
let rows = table.append("tbody").selectAll("tr")
.data(percKeys)
.enter().append("tr")
let cells = rows.selectAll("td")
.data(function(row) {
return [{value: row}, {value: numberShow(percs[row])}]
}).enter()
.append("td")
.text(x => x.value)


svg.append("g")
.call(xAxis)
return svg
}
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