Optimization analysis fails on parameter points revisited by Euler search

Open
#10,057 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
54/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
csharp
Domain
backend, data

Research direction

Start in Optimizer/Analysis/OptimizationModes.cs at the ToDictionary call in OptimizationModes.Find, then read the analysis flow through OptimizationAnalyzer.Run and LeanOptimizer.TriggerOnEndEvent. Add coverage for duplicate coordinates and overlapping Euler passes, including repeated coordinates with different metrics; done means successful final analysis with the chosen representation policy explicit in tests.

Written by the indexing model from the issue text.

Description

Problem

The final optimization analysis throws when multiple backtests have the same parameter coordinates. Euler search legitimately revisits points from its coarse pass during refinement, so a completed Euler optimization can lose its aggregate analysis even when every backtest succeeds.

OptimizationModes.Find builds a dictionary of grid coordinates using indexed.ToDictionary(...) in Optimizer/Analysis/OptimizationModes.cs:66. Repeated coordinates cause ArgumentException. LeanOptimizer.TriggerOnEndEvent catches the exception and completes the optimization, but the final analysis is not assigned.

This is existing behavior on master; it does not require automatically calculated steps.

Minimal reproduction

using System.Collections.Generic;
using QuantConnect.Optimizer;
using QuantConnect.Optimizer.Analysis;
using QuantConnect.Optimizer.Parameters;
using QuantConnect.Statistics;

var backtests = new List<OptimizationBacktestMetrics>
{
    new()
    {
        BacktestId = "coarse",
        Parameters = new Dictionary<string, decimal> { ["x"] = 1m },
        SharpeRatio = 1m,
        TotalPerformance = new AlgorithmPerformance()
    },
    new()
    {
        BacktestId = "refined",
        Parameters = new Dictionary<string, decimal> { ["x"] = 1m },
        SharpeRatio = 1m,
        TotalPerformance = new AlgorithmPerformance()
    }
};
var parameters = new HashSet<OptimizationParameter>
{
    new OptimizationStepParameter("x", 1m, 2m, 1m)
};

new OptimizationAnalyzer().Run(
    new OptimizationAnalysisRunParameters(backtests, parameters));

Observed:

System.ArgumentException: An item with the same key has already been added. Key: 0
  at QuantConnect.Optimizer.Analysis.OptimizationModes.Find(...)
  at QuantConnect.Optimizer.Analysis.OptimizationAnalyzer.Run(...)

Also reproduced end to end with ParameterizedAlgorithm: Euler search with ema-fast 10..30, step 10, minimum step 5, and ema-slow fixed at 50 produces a three-backtest coarse pass followed by five refined backtests. All eight backtests complete successfully. The repeated points (10,50), (20,50), and (30,50) then trigger the duplicate-key error during final analysis.

Expected behavior

Repeated parameter coordinates must not prevent analysis of an otherwise successful optimization. Define how repeated observations are represented when finding modes, and add coverage for duplicate coordinates and overlapping Euler passes. Any aggregation or representative-selection policy should be explicit and tested, including repeated coordinates with different metrics.

Dominant language
C#
Stars
22.6k
Forks
5.4k
Avg merge
11h 39m
Merged PRs (30d)
104

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from QuantConnect/Lean

All issues in QuantConnect/Lean

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.