xPos = [-1.25, 0.23, 0.45, 1.16, 1.48, 1.59, 1.68, 1.85, 1.95, 2.31, 2.35, 2.65, 2.8, 3.08]
xNeg = [-2.73, -2.6, -1.91, -1.86, -1.82, -1.58, -1.03, -0.92, -0.72, -0.64, 0.27, 0.29, 0.38, 0.8]
ptsLR = xPos.map(x => ({x, k: 1})).concat(xNeg.map(x => ({x, k: -1})))
sigm = u => 1 / (1 + Math.exp(-u))
lossLR = (a, b) => d3.mean(ptsLR, d => Math.log1p(Math.exp(-d.k * (a + b * d.x))))
errLR = (a, b) => ptsLR.filter(d => d.k * (a + b * d.x) <= 0).length
wML = [-0.608, 1.719]
xsLR = d3.range(-3.5, 3.501, 0.02)
showP = showLR.includes("p(+1 | x)")
showFit = showLR.includes("fit of each sample")
Plot.plot({
width: 1300, height: 700, marginLeft: 80, marginTop: 40, marginBottom: 60,
style: {fontSize: "22px"},
x: {label: "x", domain: [-3.5, 3.5]},
y: {label: "p(+1 | x)", domain: [-0.05, 1.05], grid: true, ticks: [0, 0.5, 1]},
marks: [
showLR.includes("ML solution") ? Plot.line(xsLR, {x: d => d, y: d => sigm(wML[0] + wML[1] * d), stroke: "#4E8F3A", strokeWidth: 3, strokeDasharray: "8 6", strokeOpacity: 0.6}) : null,
showP ? Plot.line(xsLR, {x: d => d, y: d => sigm(w0L + w1L * d), stroke: "#4E8F3A", strokeWidth: 5}) : null,
showP && Math.abs(w1L) > 0.01 && Math.abs(w0L / w1L) < 3.5 ? Plot.ruleX([-w0L / w1L], {stroke: "#202729", strokeWidth: 3}) : null,
showFit ? Plot.ruleX(ptsLR, {x: "x", y1: d => d.k > 0 ? 1 : 0, y2: d => sigm(w0L + w1L * d.x), stroke: d => d.k > 0 ? "#3C7DD9" : "#C0392B", strokeWidth: 4, strokeOpacity: 0.55}) : null,
Plot.dot(ptsLR, {x: "x", y: d => d.k > 0 ? 1 : 0, r: 10, fill: d => d.k > 0 ? "#3C7DD9" : "#C0392B", stroke: "white", strokeWidth: 1.5}),
Plot.ruleY([0.5], {stroke: "#8A8F93", strokeDasharray: "3 5"})
]
})