function poissonMGF(t, lambda) {
return Math.exp(lambda * (Math.exp(t) - 1));
}
function poissonMGF_derivative1(t, lambda) {
return lambda * Math.exp(t) * Math.exp(lambda * (Math.exp(t) - 1));
}
// Generate data for plotting
t_range = d3.range(-1, 1, 0.02)
mgf_data = t_range.map(t => ({
t: t,
mgf: poissonMGF(t, lambda_mgf),
derivative1: poissonMGF_derivative1(t, lambda_mgf)
}))
// Values at t=0
mgf_at_0 = poissonMGF(0, lambda_mgf)
deriv1_at_0 = poissonMGF_derivative1(0, lambda_mgf)
// Dynamic y-axis with logarithmic scale
y_min_all = Math.min(...mgf_data.map(d => Math.min(d.mgf, d.derivative1)))
y_max_all = Math.max(...mgf_data.map(d => Math.max(d.mgf, d.derivative1)))
// Use log scale if range is large
use_log_scale = (y_max_all / y_min_all) > 100
y_scale_type = use_log_scale ? "log" : "linear"
// Set domain with padding - no truncation
y_min_domain = use_log_scale ? Math.max(0.001, y_min_all * 0.1) : Math.max(0, y_min_all - (y_max_all - y_min_all) * 0.1)
y_max_domain = use_log_scale ? y_max_all * 10 : y_max_all * 1.15
Plot.plot({
width: 800,
height: 450,
marginLeft: 80,
marginBottom: 50,
x: {
label: "t (MGF parameter)",
grid: true,
domain: [-1, 1]
},
y: {
label: "Function Value" + (use_log_scale ? " (log scale)" : ""),
grid: true,
type: y_scale_type,
domain: [y_min_domain, y_max_domain],
ticks: use_log_scale ? 6 : undefined
},
marks: [
// MGF curve
Plot.line(mgf_data, {
x: "t",
y: "mgf",
stroke: "steelblue",
strokeWidth: 3,
tip: true
}),
// First derivative
Plot.line(mgf_data, {
x: "t",
y: "derivative1",
stroke: "orange",
strokeWidth: 2.5,
strokeDasharray: "5,5",
tip: true
}),
// t=0 vertical line
Plot.ruleX([0], {
stroke: "red",
strokeWidth: 2.5,
strokeDasharray: "3,3"
}),
// Point at m(0)
Plot.dot([{t: 0, y: mgf_at_0}], {
x: "t",
y: "y",
fill: "steelblue",
r: 8,
stroke: "white",
strokeWidth: 2
}),
// Point at m'(0)
Plot.dot([{t: 0, y: deriv1_at_0}], {
x: "t",
y: "y",
fill: "orange",
r: 8,
stroke: "white",
strokeWidth: 2
}),
Plot.ruleY([0])
],
caption: html`
<div style="font-size: 14px; text-align: center; margin-top: 10px; line-height: 1.8;">
<div style="margin-bottom: 5px;">
<span style="color: steelblue; font-weight: bold; font-size: 16px;">โโโ</span>
<strong>m(t)</strong> = exp(ฮป(e<sup>t</sup>โ1)) [MGF] |
<span style="color: orange; font-weight: bold; font-size: 16px;">โ โ โ</span>
<strong>m'(t)</strong> [First Derivative] |
<span style="color: red; font-weight: bold;">โ</span> <strong>t = 0</strong>
</div>
<div style="font-size: 13px; color: #555;">
<span style="color: steelblue; font-size: 18px;">โ</span> m(0) = ${mgf_at_0.toFixed(3)} |
<span style="color: orange; font-size: 18px;">โ</span> m'(0) = ${deriv1_at_0.toFixed(3)} = <strong>Mean</strong>
</div>
</div>
`
})