322 lines
13 KiB
TypeScript
322 lines
13 KiB
TypeScript
import { useEffect, useRef, useState } from 'react';
|
|
import { Link, useSearchParams } from 'react-router-dom';
|
|
import type {
|
|
ExperimentComparisonDimension,
|
|
ExperimentComparisonInput,
|
|
} from '@stock-bot/stock-config/experiment-comparison';
|
|
import { Button } from '@/components/ui/Button';
|
|
import { ExperimentCounts } from './components/ExperimentCounts';
|
|
import { ExperimentInputs } from './components/ExperimentInputs';
|
|
import { metricText } from './components/trialMetricText';
|
|
import {
|
|
experimentComparisonApi,
|
|
parseComparisonQuery,
|
|
type ExperimentComparisonView,
|
|
} from './services/experimentComparisonApi';
|
|
|
|
const dimensionLabels: Record<ExperimentComparisonDimension, string> = {
|
|
dataset: 'Dataset and retained provenance',
|
|
window: 'Warmup and evaluation window',
|
|
capital: 'Initial capital and strategy budget',
|
|
costs: 'Fees, spread or slippage',
|
|
execution: 'Execution and liquidity settings',
|
|
risk: 'Portfolio risk limits and ledger settings',
|
|
clock: 'Account risk-session clock',
|
|
analytics: 'Metric definitions and analytics settings',
|
|
seed: 'Random seed',
|
|
build: 'Engine, worker or runtime build',
|
|
models: 'Model artifacts',
|
|
};
|
|
export function ExperimentComparisonPage() {
|
|
const [params, setParams] = useSearchParams();
|
|
let input: ExperimentComparisonInput;
|
|
try {
|
|
input = parseComparisonQuery(params);
|
|
} catch (cause) {
|
|
return (
|
|
<div className="space-y-3 p-1">
|
|
<h1 className="text-lg font-bold">Compare experiments</h1>
|
|
<p role="alert">{cause instanceof Error ? cause.message : 'Invalid comparison URL'}</p>
|
|
<Link className="text-primary-500" to="/experiments">
|
|
Select experiments
|
|
</Link>
|
|
</div>
|
|
);
|
|
}
|
|
return (
|
|
<Comparison
|
|
key={params.toString()}
|
|
input={input}
|
|
onReference={key => setParams({ ...input, referenceTrialKey: key })}
|
|
/>
|
|
);
|
|
}
|
|
function Comparison({
|
|
input,
|
|
onReference,
|
|
}: {
|
|
input: ExperimentComparisonInput;
|
|
onReference: (key: string) => void;
|
|
}) {
|
|
const [value, setValue] = useState<ExperimentComparisonView | null>(null),
|
|
[loading, setLoading] = useState(true),
|
|
[error, setError] = useState(''),
|
|
[reload, setReload] = useState(0);
|
|
const reading = useRef(true);
|
|
useEffect(() => {
|
|
let current = true;
|
|
const controller = new AbortController();
|
|
reading.current = true;
|
|
setLoading(true);
|
|
setError('');
|
|
void experimentComparisonApi
|
|
.get(input, controller.signal)
|
|
.then(result => {
|
|
if (current) {
|
|
setValue(result);
|
|
}
|
|
})
|
|
.catch(cause => {
|
|
if (current) {
|
|
setError(cause instanceof Error ? cause.message : 'Could not load comparison');
|
|
}
|
|
})
|
|
.finally(() => {
|
|
if (current) {
|
|
reading.current = false;
|
|
setLoading(false);
|
|
}
|
|
});
|
|
return () => {
|
|
current = false;
|
|
controller.abort();
|
|
};
|
|
}, [input.leftId, input.rightId, input.referenceTrialKey, reload]);
|
|
return (
|
|
<div className="h-full overflow-auto p-1 space-y-4">
|
|
<Link className="text-primary-500 text-sm" to="/experiments">
|
|
All experiments
|
|
</Link>
|
|
<div className="flex flex-wrap items-center justify-between gap-3">
|
|
<h1 className="text-lg font-bold">Compare experiments</h1>
|
|
<Button
|
|
variant="outline"
|
|
disabled={loading}
|
|
onClick={() => {
|
|
if (!reading.current) {
|
|
reading.current = true;
|
|
setReload(current => current + 1);
|
|
}
|
|
}}
|
|
>
|
|
Refresh comparison
|
|
</Button>
|
|
</div>
|
|
<p className="text-sm">
|
|
Read-only comparison of recorded settings and saved summaries. Matching settings do not
|
|
establish economic equivalence, full financial reconciliation, independence, statistical
|
|
significance or out-of-sample validity. No ranking, deltas or aggregate performance are
|
|
calculated.
|
|
</p>
|
|
{loading && <p role="status">Loading comparison...</p>}
|
|
{error && (
|
|
<p role="alert" className="text-error">
|
|
{error}
|
|
{value && ' Showing the last loaded comparison for this same selection.'}
|
|
</p>
|
|
)}
|
|
{value && (
|
|
<>
|
|
<label className="block text-sm">
|
|
Reference trial
|
|
<select
|
|
aria-label="Reference trial"
|
|
className="mt-1 w-full rounded border border-border bg-background p-2"
|
|
value={value.selection.referenceTrialKey}
|
|
onChange={event => onReference(event.target.value)}
|
|
>
|
|
{value.experiments[0].trials.map(trial => (
|
|
<option key={trial.trialKey} value={trial.trialKey}>
|
|
{'costs' in trial
|
|
? trial.index === 0
|
|
? 'Baseline'
|
|
: `Scenario ${trial.index}`
|
|
: `Trial ${trial.index + 1}`}{' '}
|
|
· {JSON.stringify(trial.parameters)} · {trial.status}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
<p className="text-sm">
|
|
Reference comes from {value.experiments[0].name}. Without an explicit URL reference, its
|
|
frozen first candidate is used; completion status and performance never choose the
|
|
reference.
|
|
</p>
|
|
{!value.reference.summaryAvailable && (
|
|
<p role="status">
|
|
Reference saved summary unavailable:{' '}
|
|
{value.reference.diagnostic ?? 'No comparable saved summary exists for this trial.'}{' '}
|
|
Settings comparisons and every outcome remain visible.
|
|
</p>
|
|
)}
|
|
<section aria-label="Comparison limitations" className="text-sm space-y-1">
|
|
{value.limitations.map((text, index) => (
|
|
<p key={index}>{text}</p>
|
|
))}
|
|
</section>
|
|
{value.experiments.map((experiment, side) => (
|
|
<section
|
|
key={experiment.id}
|
|
aria-label={`${side === 0 ? 'Left' : 'Right'} experiment outcomes`}
|
|
className="space-y-3"
|
|
>
|
|
<h2 className="font-semibold">
|
|
{side === 0 ? 'Left' : 'Right'}: {experiment.name}
|
|
</h2>
|
|
<Link
|
|
className="text-primary-500 text-sm"
|
|
to={`/experiments/${encodeURIComponent(experiment.id)}`}
|
|
>
|
|
Open {side === 0 ? 'left' : 'right'} experiment
|
|
</Link>
|
|
<ExperimentCounts experiment={experiment} />
|
|
<p className="text-sm break-all">
|
|
Strategy {experiment.preview.request.strategy.selection.strategyId} · Version{' '}
|
|
{experiment.strategyVersionId} · Dataset {experiment.datasetHash}
|
|
</p>
|
|
<details>
|
|
<summary className="cursor-pointer">
|
|
{side === 0 ? 'Left' : 'Right'} frozen inputs
|
|
</summary>
|
|
<ExperimentInputs preview={experiment.preview} />
|
|
</details>
|
|
<div className="overflow-x-auto">
|
|
<table className="w-full text-left text-sm">
|
|
<thead>
|
|
<tr>
|
|
{[
|
|
'Trial / parameters',
|
|
'Status',
|
|
'Effective costs (fractions)',
|
|
'Input settings',
|
|
'Saved summary',
|
|
'Total return (fraction)',
|
|
'Max drawdown (fraction)',
|
|
'Sharpe ratio',
|
|
'Diagnostics / evidence',
|
|
].map(label => (
|
|
<th className="p-2 border-b border-border whitespace-nowrap" key={label}>
|
|
{label}
|
|
</th>
|
|
))}
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{experiment.trials.map(trial => {
|
|
const row = value.rows.find(
|
|
item =>
|
|
item.experimentId === experiment.id && item.trialKey === trial.trialKey
|
|
);
|
|
if (!row) {
|
|
throw new Error('Verified comparison row is missing');
|
|
}
|
|
const matching = experiment.preview.request.policy.matching,
|
|
costs =
|
|
'costs' in trial
|
|
? trial.costs
|
|
: {
|
|
feeRate: matching.risk.feeRate,
|
|
slippageRate: matching.slippageRate,
|
|
};
|
|
const reference =
|
|
experiment.id === value.reference.experimentId &&
|
|
trial.trialKey === value.reference.trialKey;
|
|
return (
|
|
<tr key={trial.trialKey} className="align-top">
|
|
<td className="p-2 border-b border-border">
|
|
<p>
|
|
{'costs' in trial
|
|
? trial.index === 0
|
|
? 'Baseline'
|
|
: `Scenario ${trial.index}`
|
|
: `Trial ${trial.index + 1}`}
|
|
{reference ? ' · Reference' : ''}
|
|
</p>
|
|
<pre className="whitespace-pre-wrap text-xs">
|
|
{JSON.stringify(trial.parameters, null, 2)}
|
|
</pre>
|
|
</td>
|
|
<td className="p-2 border-b border-border">
|
|
<p>{trial.status}</p>
|
|
<p>
|
|
{trial.status === 'missing'
|
|
? 'Expected run missing'
|
|
: `${trial.progress}% complete`}
|
|
</p>
|
|
{trial.cancelRequested && <p>Cancellation requested</p>}
|
|
</td>
|
|
<td className="p-2 border-b border-border">
|
|
<p>Fee: {costs.feeRate}</p>
|
|
<p>Half-spread: {matching.halfSpreadRate}</p>
|
|
<p>Slippage: {costs.slippageRate}</p>
|
|
</td>
|
|
<td className="p-2 border-b border-border">
|
|
<p>
|
|
{row.inputCompatibility === 'match'
|
|
? 'Matching settings'
|
|
: 'Settings differ'}
|
|
</p>
|
|
{row.differences.map(difference => (
|
|
<p key={difference}>{dimensionLabels[difference]}</p>
|
|
))}
|
|
</td>
|
|
<td className="p-2 border-b border-border">
|
|
<p>
|
|
{row.summaryAvailable
|
|
? 'Saved summary available'
|
|
: (trial.diagnostic ?? 'Saved summary unavailable')}
|
|
</p>
|
|
<p>
|
|
{row.ready
|
|
? 'Matching settings and both saved summaries available'
|
|
: 'Comparison evidence not ready'}
|
|
</p>
|
|
</td>
|
|
<td className="p-2 border-b border-border">
|
|
{metricText(trial, 'totalReturn')}
|
|
</td>
|
|
<td className="p-2 border-b border-border">
|
|
{metricText(trial, 'maxDrawdown')}
|
|
</td>
|
|
<td className="p-2 border-b border-border">
|
|
{metricText(trial, 'sharpeRatio')}
|
|
</td>
|
|
<td className="p-2 border-b border-border min-w-64 space-y-1">
|
|
{trial.error && <p>{trial.error}</p>}
|
|
{trial.diagnostic && <p>{trial.diagnostic}</p>}
|
|
{trial.limitations.map((text, index) => (
|
|
<p key={index}>{text}</p>
|
|
))}
|
|
{trial.runId && (
|
|
<Link
|
|
className="text-primary-500"
|
|
to={`/backtests/${encodeURIComponent(experiment.backtestId)}/run/${encodeURIComponent(trial.runId)}`}
|
|
>
|
|
Open {side === 0 ? 'left' : 'right'} trial {trial.index + 1} run
|
|
</Link>
|
|
)}
|
|
<p className="break-all text-xs">Trial key {trial.trialKey}</p>
|
|
</td>
|
|
</tr>
|
|
);
|
|
})}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</section>
|
|
))}
|
|
</>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|