Published
Edited
Aug 26, 2021
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
t = 216
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
get(dataEmbed, t + tp).deaths_per
Insert cell
Insert cell
AB2 = {
const meanDistance = d3.mean(neighbors, d => d[1])
let A = []
let b = []
let ws = []
for (const nearest of neighbors.map(d => d[0])) {
const dist = math.distance(nearest.point, xt.point)
const w = Math.exp(-theta * dist / meanDistance)
//const sc = math.norm(math.subtract(nearest.point, xt.point))
const sc = 1
// if (arrIntersect(xt.ts, nearest.ts).length > 0) {
// continue
// }
b.push(w * sc * get(dataEmbed, nearest.t + tp)["val0"])
A.push(math.multiply([1].concat([...nearest.point].reverse()), w * sc)) // TODO: Note the point flipping!
ws.push(w)
}
return [A, b, ws]
}
Insert cell
Insert cell
Insert cell
w = {
const w = Array.from({length: nn}, () => 0)
const meanDistance = d3.mean(neighbors, d => d[1])
for (let i = 0; i < nn; i++) {
const dist = math.distance(neighbors[i][0].point, xt.point)
w[i] = Math.exp(-theta * dist / meanDistance)
}
return w
}
Insert cell
AB = {
let A = []
let b = []
for (const nearest of neighbors.map(d => d[0])) {
const p = nearest.point
A.push([1].concat(p))
b.push(get(dataEmbed, nearest.t + tp)["val0"])
}
return [math.multiply(math.diag(w), A), math.multiply(math.diag(w), b)]
}
Insert cell
ABx = {
const meanDistance = d3.mean(neighbors, d => d[1])
let A = []
let b = []
for (const nearest of neighbors.map(d => d[0])) {
const dist = math.distance(nearest.point, xt.point)
const w = Math.exp(-theta * dist / meanDistance)
const p = nearest.point
A.push(math.multiply(w, [1].concat(p)))
b.push(w * get(dataEmbed, nearest.t + tp)["val0"])
}
//return [math.multiply(math.diag(w), A), math.multiply(math.diag(w), b)]
return [A, b]
}
Insert cell
Insert cell
// TODO: Add zField
function autoSmap(dataEmbed, t, vField, args = {}) {
args = {
zField: null,
tp: 1,
E: null,
theta: null,
nn: null,
...args
}
const {zField, tp, E, theta, nn} = args
const trainPoints = dataEmbed.filter(d => d.t < t - (tp - 1))
const nowRow = get(dataEmbed, t)
const dims = Array.from({length: E}, (_,i) => `${vField}_t-${i}`)
dims.push("point")
const tree = new kdTree.kdTree([...trainPoints], (a, b) => math.distance(a["point"], b["point"]), dims)
const neighbors = tree.nearest(nowRow, nn)
const meanDistance = d3.mean(neighbors, d => d[1])
let A = []
let b = []
for (const nearest of neighbors.map(d => d[0])) {
const dist = math.distance(nearest.point, nowRow.point)
const w = Math.exp(-theta * dist / meanDistance)
const p = nearest.point
A.push(math.multiply(w, [1].concat(p)))
b.push(w * get(dataEmbed, nearest.t + tp)[vField])
}
const C = leastSquares(A, b)
const yh = math.multiply([[1].concat(get(dataEmbed, t).point)], math.transpose([C]))[0][0]
return {
baseT: t,
baseVal: nowRow[vField],
t: t + tp,
pred: yh,
neighbors: neighbors,
params: {E: E, theta: theta, nn: nn}
// TODO: Add z
}
}
Insert cell
function crudeAll(dataEmbed, f, vField, ts, args = {}) {
const preds = []
for (const t of ts) {
const pred = f(dataEmbed, t, vField, args)
preds.push(pred)
}
return preds
}
Insert cell
crudeAll(dataEmbed2, autoSmap, vField, Array.from({length: 10}, (_,i) => i+100), {E: E, tp: tp, theta: theta, nn: nn})
Insert cell
autoSmap(dataEmbed2, t, "deaths_per", {E: E, tp: tp, theta: theta, nn: nn})
Insert cell
Insert cell
Insert cell
{
const A = [[1, 4, 1],
[1, 1, 1],
[1,-1, 0],
[1, 0, 0],
[1, 5, 1],
[1, 5, 1],
[1, 6, 0],
[1, 4, 0]]
const b = [52,45,58,50,61,50,62,49]
return leastSquares(A, b)
}
Insert cell
Insert cell
Insert cell
Insert cell
function delayEmbed(data, fields, dim, args={}) {
args = {
tau: 1,
zField:
null,
period: null,
prefix: "",
...args
}
const {tau, zField, period, prefix} = args
const embedData = []
const dataMap = d3.group(data, d => d[zField])
for (const series of dataMap.values()) {
for (let i = (dim-1)*tau; i < series.length; i++) {
const row = {...series[i]}

for (const field of fields) {
const point = []
const ts = []
for (let j = (dim-1); j >= 0; j--) {
const p = series[i-j*tau]
point.push(p[field])
ts.push(p.t)
//row[field + j] = p[field]
row["val" + j] = p[field]
}
row["point"] = point
row["ts"] = ts
//row[field + "Point"] = point
//row[field + "Ts"] = ts
}
embedData.push(row)
}
}
return embedData
}
Insert cell
function delayEmbed2(data, fields, dim, args={}) {
args = {
tau: 1,
zField: null,
period: null,
prefix: "",
...args
}
const {tau, zField, period, prefix} = args
const embedData = []
const dataMap = d3.group(data, d => d[zField])
for (const series of dataMap.values()) {
for (let i = (dim-1)*tau; i < series.length; i++) {
const row = {...series[i]}

for (const field of fields) {
const point = []
const ts = []
for (let j = 0; j < dim; j++) {
const p = series[i-j*tau]
point.push(p[field])
ts.push(p.t)
row[`${field}_t-${j}`] = p[field]
//row["val" + j] = p[field]
}
row["point"] = point
row["ts"] = ts
//row[field + "Point"] = point
//row[field + "Ts"] = ts
}
embedData.push(row)
}
}
return embedData
}
Insert cell
dataEmbed2 = delayEmbed2(dataUs, [vField], E, {tau: tau, zField: zField})
Insert cell
//function sMap(
Insert cell
Insert cell
Insert cell
Insert cell
function leastSquares(A, b) {
const svd = SVD(A)
const U = svd.u
const S = math.diag(svd.q)
const V = svd.v
const Sinv = math.inv(S)
const x = math.multiply(V, math.multiply(math.inv(S), math.multiply(math.transpose(U), b)))
return x
}
Insert cell
Insert cell
//regression = require('regression@2.0.1/dist/regression.js').catch(() => window["regression"])
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