Public
Edited
Dec 10, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
populationTotal=0.5*(146748590 + 146171015)
Insert cell
Insert cell
incomeTotal = 62700.8e9
Insert cell
Insert cell
incomeMean = incomeTotal/populationTotal/12
Insert cell
Insert cell
incomeMedian = 27036.4
Insert cell
Insert cell
dataset15 = datasetPure(15)
Insert cell
dataset146 = datasetPure(146)
Insert cell
dataset1465 = datasetPure(1465)
Insert cell
dataset1465[1464]
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
Insert cell
incomeOfPopulationGroup = function(x) {
return x*populationWithIncome(x)
}
Insert cell
Insert cell
Insert cell
datasetPure = {
function incomeDistribution(nGroups) {
let data = []
let groupPopulation = populationTotal/nGroups
let groupIncomeMin = 1e-12, groupIncomeMax, groupIncomeMean
let groupTotalYearIncome = []
//The function f and its derivative dfdx come in handy for Newtonian iterations
let f = function(groupIncomeMin, groupIncomeMax) {
return integral(populationWithIncome, groupIncomeMin, groupIncomeMax, 1) - groupPopulation
}
let dfdx = function(groupIncomeMax) {
return populationWithIncome(groupIncomeMax)
}
let delta0 = 10000 //initial step
for (let i = 0; i < nGroups; i++) {
if (i < nGroups-1) { //
groupIncomeMax = groupIncomeMin + delta0
let groupIncomeMaxPre = groupIncomeMax
let err = 100
let j = 0
//Newton iterations: Calculating the maximum income difference at which the population size is equal to a given population size.
while (err*err > 0.01) {
groupIncomeMax += -f(groupIncomeMin, groupIncomeMaxPre)/dfdx(groupIncomeMaxPre)
err = groupIncomeMax - groupIncomeMaxPre
groupIncomeMaxPre = groupIncomeMax
j++
if (j>15) {
groupIncomeMax = j + " iterations were not enough, last result: " + groupIncomeMax
groupIncomeMean = j + " iterations were not enough"
break
}
groupTotalYearIncome[i] = integral(incomeOfPopulationGroup, groupIncomeMin, groupIncomeMax, 1)*12
groupIncomeMean = groupTotalYearIncome[i]/12.0/groupPopulation
delta0 = groupIncomeMax - groupIncomeMin
}
} else { //The maximum income can be infinitely large, so we just write that it is very large.
groupTotalYearIncome[i] = incomeTotal
for (let j = 0; j < nGroups - 1; j++) {
groupTotalYearIncome[i] -= groupTotalYearIncome[j]
}
groupIncomeMean = groupTotalYearIncome[i]/12/groupPopulation
groupIncomeMax = 2*groupIncomeMean - groupIncomeMin
}
data.push({population: groupPopulation, incomeMin: groupIncomeMin, incomeMean: groupIncomeMean, incomeMax: groupIncomeMax, populationIncomeYear: groupTotalYearIncome[i]})

groupIncomeMin = groupIncomeMax
}
return data
}
return incomeDistribution
}
Insert cell
Insert cell
Insert cell
integral = function(f, a, b, tol) {
let n = 1
let h = b - a
let sum = []
let i = 1;
let s, result
sum[0] = 0.5*h*(f(a) + f(b))
let err = 10*tol
while ((err > tol)) {
h = h/2
s = 0
for (let j = 1; j <= n; j++) {
s += f(a + (2*j-1)*h)
}
n = 2*n
sum[i] = 0.5*sum[i-1] + h*s
err = Math.abs(sum[i] - sum[i-1])
result = sum[i]
i = i+1
if (i>20) {
result = "integration error"
break
}
}
return result
}
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