Spaces:
Running
Running
File size: 13,717 Bytes
518343a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 | import {
Activity,
ChevronDown,
ChevronRight,
Minus,
Target,
TrendingDown,
TrendingUp,
} from 'lucide-react';
import { useState } from 'react';
import {
Area,
AreaChart,
Bar,
BarChart,
ReferenceLine,
ResponsiveContainer,
Tooltip,
XAxis,
YAxis,
} from 'recharts';
const BG = { surface: 'var(--gi-bg-surface)', elevated: 'var(--gi-bg-raised)' };
const BORDER = { subtle: 'rgba(255,255,255,0.04)', muted: 'rgba(255,255,255,0.07)' };
const TEXT = {
primary: 'rgba(255,255,255,0.88)',
secondary: 'rgba(255,255,255,0.55)',
tertiary: 'rgba(255,255,255,0.28)',
muted: 'rgba(255,255,255,0.14)',
};
export interface SimulationScenario {
id: string;
label: string;
color: string;
outputMetric: string;
outputUnit: string;
p5: number;
p25: number;
p50: number;
p75: number;
p95: number;
mean: number;
stdDev: number;
confidence: number;
}
export interface SensitivityDriver {
input: string;
lowValue: number;
highValue: number;
baseOutput: number;
lowOutput: number;
highOutput: number;
unit: string;
}
export interface MonteCarloSimPanelProps {
scenarios: SimulationScenario[];
sensitivityDrivers: SensitivityDriver[];
iterations: number;
compact?: boolean;
}
function formatValue(v: number, unit: string): string {
if (unit === '$' || unit === 'USD') {
if (Math.abs(v) >= 1_000_000) return `$${(v / 1_000_000).toFixed(1)}M`;
if (Math.abs(v) >= 1_000) return `$${(v / 1_000).toFixed(0)}K`;
return `$${v.toFixed(0)}`;
}
if (unit === '%') return `${v.toFixed(1)}%`;
return v.toFixed(1);
}
function DistributionChart({ scenario }: { scenario: SimulationScenario }) {
const bins = 30;
const range = scenario.p95 - scenario.p5;
const binWidth = range / bins;
const data = Array.from({ length: bins }, (_, i) => {
const x = scenario.p5 + i * binWidth;
const z = (x - scenario.mean) / scenario.stdDev;
const density = Math.exp(-0.5 * z * z) / (scenario.stdDev * Math.sqrt(2 * Math.PI));
return {
x: Math.round(x),
density: density * 1000,
label: formatValue(x, scenario.outputUnit),
};
});
return (
<div className="h-32">
<ResponsiveContainer width="100%" height="100%">
<AreaChart data={data} margin={{ top: 5, right: 5, bottom: 0, left: 5 }}>
<defs>
<linearGradient id={`grad-${scenario.id}`} x1="0" y1="0" x2="0" y2="1">
<stop offset="0%" stopColor={scenario.color} stopOpacity={0.3} />
<stop offset="100%" stopColor={scenario.color} stopOpacity={0.02} />
</linearGradient>
</defs>
<Area
type="monotone"
dataKey="density"
stroke={scenario.color}
fill={`url(#grad-${scenario.id})`}
strokeWidth={1.5}
/>
<ReferenceLine
x={data.reduce(
(closest, d, i) =>
Math.abs(d.x - scenario.p50) < Math.abs(data[closest].x - scenario.p50)
? i
: closest,
0,
)}
stroke={scenario.color}
strokeDasharray="3 3"
strokeOpacity={0.6}
/>
<XAxis
dataKey="label"
tick={{ fontSize: 8, fill: TEXT.muted }}
interval={Math.floor(bins / 5)}
axisLine={false}
tickLine={false}
/>
<Tooltip
contentStyle={{
background: BG.elevated,
border: `1px solid ${BORDER.muted}`,
borderRadius: 8,
fontSize: 10,
}}
labelStyle={{ color: TEXT.secondary }}
formatter={(value: number) => [value.toFixed(2), 'Density']}
/>
</AreaChart>
</ResponsiveContainer>
</div>
);
}
function TornadoChart({ drivers, unit }: { drivers: SensitivityDriver[]; unit: string }) {
const sorted = [...drivers].sort(
(a, b) => Math.abs(b.highOutput - b.lowOutput) - Math.abs(a.highOutput - a.lowOutput),
);
const data = sorted.map((d) => ({
name: d.input,
low: d.lowOutput - d.baseOutput,
high: d.highOutput - d.baseOutput,
lowLabel: formatValue(d.lowOutput, unit),
highLabel: formatValue(d.highOutput, unit),
}));
return (
<div className="h-40">
<ResponsiveContainer width="100%" height="100%">
<BarChart data={data} layout="vertical" margin={{ top: 5, right: 30, bottom: 5, left: 80 }}>
<XAxis
type="number"
tick={{ fontSize: 8, fill: TEXT.muted }}
axisLine={false}
tickLine={false}
/>
<YAxis
type="category"
dataKey="name"
tick={{ fontSize: 9, fill: TEXT.secondary }}
axisLine={false}
tickLine={false}
width={75}
/>
<Tooltip
contentStyle={{
background: BG.elevated,
border: `1px solid ${BORDER.muted}`,
borderRadius: 8,
fontSize: 10,
}}
formatter={(value: number) => [formatValue(value, unit), 'Impact']}
/>
<ReferenceLine x={0} stroke={TEXT.muted} />
<Bar dataKey="low" fill="#c45a4a" opacity={0.7} radius={[4, 0, 0, 4]} />
<Bar dataKey="high" fill="#6b8f71" opacity={0.7} radius={[0, 4, 4, 0]} />
</BarChart>
</ResponsiveContainer>
</div>
);
}
export function MonteCarloSimPanel({
scenarios,
sensitivityDrivers,
iterations,
compact,
}: MonteCarloSimPanelProps) {
const [activeScenario, setActiveScenario] = useState(scenarios[0]?.id);
const [showTornado, setShowTornado] = useState(false);
const selected = scenarios.find((s) => s.id === activeScenario) ?? scenarios[0];
if (compact && selected) {
const trend =
selected.p50 > selected.mean ? 'up' : selected.p50 < selected.mean ? 'down' : 'flat';
const TrendIcon = trend === 'up' ? TrendingUp : trend === 'down' ? TrendingDown : Minus;
const trendColor = trend === 'up' ? '#6b8f71' : trend === 'down' ? '#c45a4a' : TEXT.tertiary;
return (
<div
className="rounded-xl p-4"
style={{ background: BG.surface, border: `1px solid ${BORDER.muted}` }}
>
<div className="flex items-center gap-2 mb-3">
<Activity className="w-3.5 h-3.5" style={{ color: '#8b7ac8' }} />
<span
className="text-[10px] font-bold uppercase tracking-widest"
style={{ color: '#8b7ac8' }}
>
Monte Carlo Simulation
</span>
<span className="ml-auto text-[9px] font-mono" style={{ color: TEXT.tertiary }}>
{iterations.toLocaleString()} runs
</span>
</div>
<div className="flex items-center justify-between">
<div>
<div className="text-lg font-bold font-mono" style={{ color: TEXT.primary }}>
{formatValue(selected.p50, selected.outputUnit)}
</div>
<div className="text-[9px]" style={{ color: TEXT.tertiary }}>
Median ({selected.outputMetric})
</div>
</div>
<div className="flex items-center gap-1 text-[10px]" style={{ color: trendColor }}>
<TrendIcon className="w-3 h-3" />
<span className="font-mono">
{formatValue(selected.p5, selected.outputUnit)} —{' '}
{formatValue(selected.p95, selected.outputUnit)}
</span>
</div>
<div className="text-right">
<div
className="text-sm font-bold font-mono"
style={{ color: selected.confidence >= 75 ? '#6b8f71' : '#c8953c' }}
>
{selected.confidence}%
</div>
<div className="text-[9px]" style={{ color: TEXT.tertiary }}>
Confidence
</div>
</div>
</div>
</div>
);
}
return (
<div
className="rounded-xl overflow-hidden"
style={{ background: BG.surface, border: `1px solid ${BORDER.muted}` }}
>
<div
className="flex items-center gap-2 px-5 py-3"
style={{ borderBottom: `1px solid ${BORDER.subtle}` }}
>
<Activity className="w-4 h-4" style={{ color: '#8b7ac8' }} />
<span className="text-xs font-bold uppercase tracking-widest" style={{ color: '#8b7ac8' }}>
Monte Carlo Simulation
</span>
<span className="ml-auto text-[10px] font-mono" style={{ color: TEXT.tertiary }}>
{iterations.toLocaleString()} iterations
</span>
</div>
{scenarios.length > 1 && (
<div
className="flex gap-1 px-5 py-2"
style={{ borderBottom: `1px solid ${BORDER.subtle}` }}
>
{scenarios.map((s) => (
<button
key={s.id}
onClick={() => setActiveScenario(s.id)}
className="px-3 py-1 rounded-lg text-[10px] font-medium transition-all"
style={{
background: activeScenario === s.id ? `${s.color}15` : 'transparent',
border: `1px solid ${activeScenario === s.id ? `${s.color}40` : 'transparent'}`,
color: activeScenario === s.id ? s.color : TEXT.tertiary,
}}
>
{s.label}
</button>
))}
</div>
)}
{selected && (
<div className="p-5">
<div className="grid grid-cols-5 gap-3 mb-4">
{[
{
label: 'P5 (Worst)',
value: formatValue(selected.p5, selected.outputUnit),
color: '#c45a4a',
},
{
label: 'P25',
value: formatValue(selected.p25, selected.outputUnit),
color: '#c8953c',
},
{
label: 'P50 (Median)',
value: formatValue(selected.p50, selected.outputUnit),
color: selected.color,
},
{
label: 'P75',
value: formatValue(selected.p75, selected.outputUnit),
color: '#6b8f71',
},
{
label: 'P95 (Best)',
value: formatValue(selected.p95, selected.outputUnit),
color: '#6b8f71',
},
].map((p) => (
<div key={p.label} className="text-center">
<div
className="text-[8px] font-mono uppercase tracking-wider mb-0.5"
style={{ color: TEXT.muted }}
>
{p.label}
</div>
<div className="text-sm font-bold font-mono" style={{ color: p.color }}>
{p.value}
</div>
</div>
))}
</div>
<DistributionChart scenario={selected} />
<div
className="flex items-center justify-between mt-3 pt-3"
style={{ borderTop: `1px solid ${BORDER.subtle}` }}
>
<div className="flex items-center gap-4">
<div>
<div
className="text-[8px] font-mono uppercase tracking-wider"
style={{ color: TEXT.muted }}
>
Decision Confidence
</div>
<div className="flex items-center gap-1 mt-0.5">
<Target
className="w-3 h-3"
style={{ color: selected.confidence >= 75 ? '#6b8f71' : '#c8953c' }}
/>
<span
className="text-sm font-bold font-mono"
style={{ color: selected.confidence >= 75 ? '#6b8f71' : '#c8953c' }}
>
{selected.confidence}%
</span>
</div>
</div>
<div>
<div
className="text-[8px] font-mono uppercase tracking-wider"
style={{ color: TEXT.muted }}
>
Std Deviation
</div>
<span className="text-xs font-mono" style={{ color: TEXT.secondary }}>
{formatValue(selected.stdDev, selected.outputUnit)}
</span>
</div>
</div>
<button
onClick={() => setShowTornado(!showTornado)}
className="flex items-center gap-1.5 px-3 py-1.5 rounded-lg text-[10px] font-medium"
style={{
background: 'rgba(139,122,200,0.08)',
border: '1px solid rgba(139,122,200,0.2)',
color: '#8b7ac8',
}}
aria-expanded={showTornado}
aria-label="Toggle sensitivity drivers tornado chart"
>
{showTornado ? (
<ChevronDown className="w-3 h-3" />
) : (
<ChevronRight className="w-3 h-3" />
)}
Sensitivity Drivers
</button>
</div>
{showTornado && sensitivityDrivers.length > 0 && (
<div className="mt-3 pt-3" style={{ borderTop: `1px solid ${BORDER.subtle}` }}>
<div
className="text-[9px] font-bold uppercase tracking-widest mb-2"
style={{ color: TEXT.muted }}
>
Tornado Chart — Key Sensitivity Drivers
</div>
<TornadoChart drivers={sensitivityDrivers} unit={selected.outputUnit} />
</div>
)}
</div>
)}
</div>
);
}
|