Skip to content

Commit 0f4cbbc

Browse files
authored
Add optimization analysis (#9495)
* Add optimization analysis * Address feedback * Remove redundant CurveType * Address feedback pt 2 * Address review pt 3 * Address review pt 4 * Removed redundant properties * Add Interpretation member
1 parent b68e9c8 commit 0f4cbbc

21 files changed

Lines changed: 1767 additions & 0 deletions

Common/Api/Optimization.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.Collections.Generic;
1818
using Newtonsoft.Json;
19+
using QuantConnect.Optimizer;
1920
using QuantConnect.Optimizer.Objectives;
2021
using QuantConnect.Util;
2122

@@ -74,6 +75,12 @@ public class Optimization : BaseOptimization
7475
/// </summary>
7576
[JsonConverter(typeof(DateTimeJsonConverter), DateFormat.ISOShort, DateFormat.UI)]
7677
public DateTime Requested { get; set; }
78+
79+
/// <summary>
80+
/// Aggregate diagnostic of the optimization; omitted when no analysis was produced.
81+
/// </summary>
82+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
83+
public OptimizationAnalysis Analysis { get; set; }
7784
}
7885

7986
/// <summary>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3+
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
*/
16+
17+
using QuantConnect.Optimizer.Parameters;
18+
using System.Collections.Generic;
19+
20+
namespace QuantConnect.Optimizer.Analysis
21+
{
22+
/// <summary>
23+
/// Bundles the inputs to the optimization analyzer: per-backtest metrics and the parameter grid spec.
24+
/// </summary>
25+
public class OptimizationAnalysisRunParameters
26+
{
27+
/// <summary>
28+
/// Completed backtests from the optimization, already reduced to the metrics the analyzer reads.
29+
/// </summary>
30+
public IReadOnlyList<OptimizationBacktestMetrics> CompletedBacktests { get; }
31+
32+
/// <summary>
33+
/// The optimization parameter grid spec.
34+
/// </summary>
35+
public IReadOnlyCollection<OptimizationParameter> OptimizationParameters { get; }
36+
37+
/// <summary>
38+
/// Initializes a new instance of the <see cref="OptimizationAnalysisRunParameters"/> class.
39+
/// </summary>
40+
/// <param name="completedBacktests">The completed backtest metrics.</param>
41+
/// <param name="optimizationParameters">The parameter grid spec.</param>
42+
public OptimizationAnalysisRunParameters(
43+
IReadOnlyList<OptimizationBacktestMetrics> completedBacktests,
44+
IReadOnlyCollection<OptimizationParameter> optimizationParameters)
45+
{
46+
CompletedBacktests = completedBacktests;
47+
OptimizationParameters = optimizationParameters;
48+
}
49+
}
50+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3+
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
*/
16+
17+
using Newtonsoft.Json;
18+
using System.Collections.Generic;
19+
20+
namespace QuantConnect.Optimizer
21+
{
22+
/// <summary>
23+
/// Per-backtest identity + Sharpe ratio shared by all optimization-analysis records that describe one backtest.
24+
/// </summary>
25+
public class BacktestSummary
26+
{
27+
/// <summary>
28+
/// The backtest id; kept for programmatic access but not serialized into the analysis JSON.
29+
/// </summary>
30+
[JsonIgnore]
31+
public string BacktestId { get; set; }
32+
33+
/// <summary>
34+
/// Parameter values the backtest was run with.
35+
/// </summary>
36+
public IReadOnlyDictionary<string, decimal> Parameters { get; set; }
37+
38+
/// <summary>
39+
/// The backtest's Sharpe ratio.
40+
/// </summary>
41+
public decimal SharpeRatio { get; set; }
42+
}
43+
}

Common/Optimizer/Cluster.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3+
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
*/
16+
17+
using System.Collections.Generic;
18+
19+
namespace QuantConnect.Optimizer
20+
{
21+
/// <summary>
22+
/// One k-means cluster of backtests in standardized parameter space.
23+
/// </summary>
24+
public class Cluster
25+
{
26+
/// <summary>
27+
/// Cluster centroid in original parameter units.
28+
/// </summary>
29+
public IReadOnlyDictionary<string, decimal> Centroid { get; set; }
30+
31+
/// <summary>
32+
/// Number of backtests assigned to this cluster.
33+
/// </summary>
34+
public int MemberCount { get; set; }
35+
36+
/// <summary>
37+
/// Mean Sharpe ratio across the cluster's members.
38+
/// </summary>
39+
public decimal SharpeMean { get; set; }
40+
41+
/// <summary>
42+
/// Sample standard deviation of Sharpe ratios within this cluster.
43+
/// </summary>
44+
public decimal SharpeStdDev { get; set; }
45+
46+
/// <summary>
47+
/// Minimum Sharpe ratio within this cluster.
48+
/// </summary>
49+
public decimal SharpeMin { get; set; }
50+
51+
/// <summary>
52+
/// Maximum Sharpe ratio within this cluster.
53+
/// </summary>
54+
public decimal SharpeMax { get; set; }
55+
}
56+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3+
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
*/
16+
17+
using System.Collections.Generic;
18+
19+
namespace QuantConnect.Optimizer
20+
{
21+
/// <summary>
22+
/// Breakdown of backtests in an optimization that produced zero orders.
23+
/// </summary>
24+
public class FailedBacktestSummary
25+
{
26+
/// <summary>
27+
/// Total number of backtests that produced zero orders.
28+
/// </summary>
29+
public int ZeroOrderCount { get; set; }
30+
31+
/// <summary>
32+
/// Number of zero-order backtests inspected for analysis tags; may be smaller than <see cref="ZeroOrderCount"/>.
33+
/// </summary>
34+
public int InspectedCount { get; set; }
35+
36+
/// <summary>
37+
/// Map of analysis-tag name to the number of inspected backtests carrying that tag.
38+
/// </summary>
39+
public IReadOnlyDictionary<string, int> AnalysisNameCounts { get; set; }
40+
}
41+
}

Common/Optimizer/LinearSegment.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3+
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
*/
16+
17+
namespace QuantConnect.Optimizer
18+
{
19+
/// <summary>
20+
/// One linear piece of a piecewise interpolant on [<see cref="XLo"/>, <see cref="XHi"/>], evaluated as y(x) = A + B * (x - XLo).
21+
/// </summary>
22+
public class LinearSegment
23+
{
24+
/// <summary>
25+
/// Lower bound of this segment.
26+
/// </summary>
27+
public decimal XLo { get; set; }
28+
29+
/// <summary>
30+
/// Upper bound of this segment.
31+
/// </summary>
32+
public decimal XHi { get; set; }
33+
34+
/// <summary>
35+
/// Sharpe ratio at <see cref="XLo"/>.
36+
/// </summary>
37+
public decimal A { get; set; }
38+
39+
/// <summary>
40+
/// Slope through the segment.
41+
/// </summary>
42+
public decimal B { get; set; }
43+
}
44+
}

Common/Optimizer/Mode.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3+
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
*/
16+
17+
namespace QuantConnect.Optimizer
18+
{
19+
/// <summary>
20+
/// A local maximum of the Sharpe surface on the parameter grid; strictly greater than every face-neighbor's Sharpe.
21+
/// </summary>
22+
public class Mode : BacktestSummary
23+
{
24+
/// <summary>
25+
/// Number of face-neighbors this backtest was compared against.
26+
/// </summary>
27+
public int NeighborCount { get; set; }
28+
}
29+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3+
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
*/
16+
17+
using Newtonsoft.Json;
18+
using System.Collections.Generic;
19+
using System.ComponentModel;
20+
21+
namespace QuantConnect.Optimizer
22+
{
23+
/// <summary>
24+
/// Aggregate diagnostic produced by analyzing a completed optimization.
25+
/// </summary>
26+
public class OptimizationAnalysis
27+
{
28+
/// <summary>
29+
/// Natural-language interpretation of the analysis produced by a downstream AI consumer; empty until populated.
30+
/// </summary>
31+
[DefaultValue("")]
32+
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
33+
public string Interpretation { get; set; } = string.Empty;
34+
35+
/// <summary>
36+
/// Total number of backtests observed, including failures.
37+
/// </summary>
38+
public int BacktestCountTotal { get; set; }
39+
40+
/// <summary>
41+
/// Number of backtests used in the analysis after filtering failures.
42+
/// </summary>
43+
public int BacktestCountUsed { get; set; }
44+
45+
/// <summary>
46+
/// Sharpe ratio statistics across all used backtests.
47+
/// </summary>
48+
public SharpeSummary OverallSharpe { get; set; }
49+
50+
/// <summary>
51+
/// The best-performing backtest (argmax of Sharpe).
52+
/// </summary>
53+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
54+
public BacktestSummary Best { get; set; }
55+
56+
/// <summary>
57+
/// Per-parameter sensitivity report; one entry per optimized parameter.
58+
/// </summary>
59+
public IReadOnlyList<ParameterReport> Parameters { get; set; }
60+
61+
/// <summary>
62+
/// K-means clusters in standardized parameter space, ordered by mean Sharpe descending.
63+
/// </summary>
64+
public IReadOnlyList<Cluster> Clusters { get; set; }
65+
66+
/// <summary>
67+
/// Local maxima of the Sharpe surface on the parameter grid, ordered by Sharpe descending.
68+
/// </summary>
69+
public IReadOnlyList<Mode> Modes { get; set; }
70+
71+
/// <summary>
72+
/// Breakdown of zero-order backtests; null when none exist.
73+
/// </summary>
74+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
75+
public FailedBacktestSummary FailedBacktests { get; set; }
76+
}
77+
}

0 commit comments

Comments
 (0)