Public
Edited
Oct 8, 2023
Insert cell
Insert cell
Insert cell
Insert cell
{
const flatten = flatMat(powerMat(mat, order));

const plt1 = Plot.plot({
color: { scheme: "Blues", nice: true, legend: true },
aspectRatio: 1.0,
width: 500,
marks: [
Plot.cell(flatten, {
x: "i",
y: (d) => d.j - mat.length + order,
fill: "x"
}),
Plot.text(flatten, {
x: "i",
y: (d) => d.j - mat.length + order,
text: "x",
fill: "white",
fontSize: "large"
})
]
});

const plt2 = Plot.plot({
grid: true,
color: { scheme: "Blues", nice: true, legend: true, domain: [0, 2 / 7] },
x: { nice: true },
y: { nice: true },
marks: [
Plot.ruleX([order - 1], { stroke: "gray" }),
Plot.line(ground.res, { x: "i", y: "p", stroke: "gray" }),
Plot.dot(ground.res, { x: "i", y: "p", tip: true, fill: "p" }),
Plot.line(simulation.res, {
x: "i",
y: "p",
z: "j",
stroke: (d) => d3.schemeCategory10[d.j % 10]
})
]
});

return htl.html`
<div class='FlexLayout'>
${plt1}
${plt2}
</div>
`;
}
Insert cell
<style>
div.FlexLayout {
display: flex
}
</style>
Insert cell
powerMat = (mat, n) => {
var res = new ML.Matrix(mat),
m = new ML.Matrix(mat);

for (let i = 1; i < n; ++i) {
res = res.mmul(m);
}

return res.data;
}
Insert cell
flatMat = (mat) => {
const arr = [];

mat.map((vec, j) =>
vec.map((x, i) => {
arr.push({ i, j, x });
})
);

return arr;
}
Insert cell
simulation = {
shuffle;

const m = new ML.Matrix(mat),
res = [],
n = 7,
rnd = d3.randomNormal(),
rndVec = () => [...new Array(n)].map(rnd);

var v = new ML.Matrix([...new Array(m.rows)].map(rndVec));

for (let i = 0; i < 30; ++i) {
v = m.mmul(v);
for (let j = 0; j < n; ++j) {
res.push({ i: i, j, p: v.data[m.rows - 1][j] });
}
}

return { v, res };
}
Insert cell
ground = {
const m = new ML.Matrix(mat),
res = [];

var v = new ML.Matrix([[0], [0], [0], [0], [0], [1]]);

for (let i = 0; i < 30; ++i) {
v = m.mmul(v);

res.push({ i: i, p: v.data[m.rows - 1][0] });
}

return { v, res };
}
Insert cell
ev = new ML.EigenvalueDecomposition(m)
Insert cell
m = new ML.Matrix(mat)
Insert cell
mat1 = [
[0, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1],
[1 / 6, 1 / 6, 1 / 6, 1 / 6, 1 / 6, 1 / 6]
]
Insert cell
mat = {
shuffle;
return rndRightStochasticMatrix();
}
Insert cell
rndRightStochasticMatrix = (rows = 6, cols = 6) => {
const rnd = d3.randomUniform();

function rndRow() {
const row = [...new Array(cols)].map(rnd),
sum = d3.sum(row);

return row.map((d) => d / sum);
}

return [...new Array(rows)].map(rndRow);
}
Insert cell
ML = require("https://cdn.jsdelivr.net/npm/ml-matrix@6.10.5/matrix.umd.min.js")
Insert cell
d3 = require("d3")
Insert cell
<h2>https://en.wikipedia.org/wiki/Stochastic_matrix</h2>
<iframe src="https://en.wikipedia.org/wiki/Stochastic_matrix" width='100%' height=600></iframe>
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