ledge-core 0.2.0

Factor-structured convex QP kernel for portfolio optimization
Documentation
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! Constraint template builder integration tests (roadmap 3.1).
//!
//! Templates must be pure sugar over the existing linear-constraint and box
//! machinery: each builder appends the documented rows (or tightens bounds)
//! and nothing else. These tests verify the emitted QP against hand-built
//! constraints, the portfolio-level semantics on solved weights, template
//! stacking with user constraints, and rolling-sequence target updates.

use ledge_core::{
    check_kkt, FactorCovariance, Matrix, PortfolioError, PortfolioProblem, RebalanceStep,
    SolveStatus,
};

const CONSTRAINT_TOLERANCE: f64 = 1.0e-5;
const RESIDUAL_TOLERANCE: f64 = 1.0e-4;

/// Deterministic factor portfolio without external RNG dependencies.
struct Fixture {
    factors: Matrix,
    omega: FactorCovariance,
    specific: Vec<f64>,
    expected: Vec<f64>,
}

fn fixture(assets: usize, factor_count: usize) -> Fixture {
    let mut factors = Vec::with_capacity(assets * factor_count);
    for row in 0..assets {
        for col in 0..factor_count {
            let angle = (1 + row * factor_count + col) as f64;
            factors.push(0.3 * (angle * 12.9898).sin());
        }
    }
    let omega = FactorCovariance::Diagonal(
        (0..factor_count)
            .map(|index| 0.05 + 0.01 * index as f64)
            .collect(),
    );
    let specific: Vec<f64> = (0..assets)
        .map(|index| 0.08 + 0.04 * ((index * 7 % 13) as f64) / 13.0)
        .collect();
    let expected: Vec<f64> = (0..assets)
        .map(|index| 0.05 + 0.03 * ((index as f64) * 0.7).cos())
        .collect();
    Fixture {
        factors: Matrix::new(assets, factor_count, factors).unwrap(),
        omega,
        specific,
        expected,
    }
}

impl Fixture {
    fn assets(&self) -> usize {
        self.expected.len()
    }

    fn problem(&self) -> PortfolioProblem {
        PortfolioProblem::new(
            self.factors.clone(),
            self.omega.clone(),
            self.specific.clone(),
            self.expected.clone(),
        )
        .unwrap()
        .with_risk_aversion(6.0)
        .unwrap()
    }

    /// Round-robin industry ids over `count` industries.
    fn industries(&self, count: usize) -> Vec<usize> {
        (0..self.assets()).map(|asset| asset % count).collect()
    }

    /// A benchmark inside the default `[0, 1]` boxes summing to one.
    fn benchmark(&self) -> Vec<f64> {
        let assets = self.assets();
        let uniform = 1.0 / assets as f64;
        let mut benchmark: Vec<f64> = (0..assets)
            .map(|index| uniform * (1.0 + 0.2 * (((index % 7) as f64 - 3.0) / 3.0)))
            .collect();
        let total: f64 = benchmark.iter().sum();
        for value in &mut benchmark {
            *value /= total;
        }
        benchmark
    }

    /// One deterministic style loading row per style.
    fn style_exposures(&self, styles: usize) -> Matrix {
        let assets = self.assets();
        let mut data = Vec::with_capacity(styles * assets);
        for style in 0..styles {
            for asset in 0..assets {
                let angle = (1 + style * assets + asset) as f64;
                data.push((angle * 0.37).sin());
            }
        }
        Matrix::new(styles, assets, data).unwrap()
    }
}

fn group_weight(weights: &[f64], groups: &[usize], group: usize) -> f64 {
    weights
        .iter()
        .zip(groups)
        .filter(|(_, id)| **id == group)
        .map(|(weight, _)| weight)
        .sum()
}

#[test]
fn industry_neutrality_matches_hand_built_equalities_and_pins_group_weights() {
    let fixture = fixture(48, 4);
    let industries = fixture.industries(6);
    let benchmark = fixture.benchmark();

    let templated = fixture
        .problem()
        .with_tracking_benchmark(benchmark.clone())
        .unwrap()
        .with_industry_neutrality(&industries)
        .unwrap();

    // Hand-built equivalent: one indicator row per industry with the
    // benchmark's industry weight as target.
    let mut rows = vec![vec![0.0; fixture.assets()]; 6];
    let mut targets = vec![0.0; 6];
    for (asset, industry) in industries.iter().enumerate() {
        rows[*industry][asset] = 1.0;
        targets[*industry] += benchmark[asset];
    }
    let manual = fixture
        .problem()
        .with_tracking_benchmark(benchmark.clone())
        .unwrap()
        .with_equalities(Matrix::from_rows(rows).unwrap(), targets.clone())
        .unwrap();

    assert_eq!(
        templated.to_qp().unwrap(),
        manual.to_qp().unwrap(),
        "the template must emit exactly the hand-built rows"
    );

    let solution = templated.solve(None).unwrap();
    assert_eq!(solution.status, SolveStatus::Solved);
    for (industry, target) in targets.iter().enumerate() {
        let held = group_weight(&solution.x, &industries, industry);
        assert!(
            (held - target).abs() <= CONSTRAINT_TOLERANCE,
            "industry {industry}: held {held} vs benchmark {target}"
        );
    }
}

#[test]
fn group_targets_hold_on_solved_weights() {
    let fixture = fixture(40, 3);
    let groups = fixture.industries(4);
    let targets = vec![0.4, 0.3, 0.2, 0.1];

    let problem = fixture
        .problem()
        .with_group_targets(&groups, &targets)
        .unwrap();
    let solution = problem.solve(None).unwrap();
    assert_eq!(solution.status, SolveStatus::Solved);
    for (group, target) in targets.iter().enumerate() {
        let held = group_weight(&solution.x, &groups, group);
        assert!(
            (held - target).abs() <= CONSTRAINT_TOLERANCE,
            "group {group}: held {held} vs target {target}"
        );
    }

    let qp = problem.to_qp().unwrap();
    let residuals = check_kkt(&qp, &solution.x, &solution.dual).unwrap();
    assert!(residuals.primal <= RESIDUAL_TOLERANCE);
    assert!(residuals.dual <= RESIDUAL_TOLERANCE);
}

#[test]
fn style_bounds_emit_documented_rows_and_hold_on_solved_weights() {
    let fixture = fixture(36, 3);
    let exposures = fixture.style_exposures(3);
    // Style 0: two-sided band; style 1: upper only; style 2: exact.
    let lower = vec![-0.2, f64::NEG_INFINITY, 0.05];
    let upper = vec![0.2, 0.3, 0.05];

    let templated = fixture
        .problem()
        .with_style_bounds(&exposures, &lower, &upper)
        .unwrap();
    let qp = templated.to_qp().unwrap();

    // Equalities: budget row + the one exact style row.
    assert_eq!(qp.equalities.len(), 2);
    assert_eq!(qp.equalities.matrix.row(1), exposures.row(2));
    assert!((qp.equalities.rhs[1] - 0.05).abs() <= f64::EPSILON);
    // Inequalities: upper+lower rows for style 0, upper row for style 1.
    assert_eq!(qp.inequalities.len(), 3);
    assert_eq!(qp.inequalities.matrix.row(0), exposures.row(0));
    assert!((qp.inequalities.rhs[0] - 0.2).abs() <= f64::EPSILON);
    let negated: Vec<f64> = exposures.row(0).iter().map(|value| -value).collect();
    assert_eq!(qp.inequalities.matrix.row(1), negated.as_slice());
    assert!((qp.inequalities.rhs[1] - 0.2).abs() <= f64::EPSILON);
    assert_eq!(qp.inequalities.matrix.row(2), exposures.row(1));
    assert!((qp.inequalities.rhs[2] - 0.3).abs() <= f64::EPSILON);

    let solution = templated.solve(None).unwrap();
    assert_eq!(solution.status, SolveStatus::Solved);
    for style in 0..3 {
        let exposure: f64 = exposures
            .row(style)
            .iter()
            .zip(&solution.x)
            .map(|(loading, weight)| loading * weight)
            .sum();
        assert!(
            exposure >= lower[style] - CONSTRAINT_TOLERANCE
                && exposure <= upper[style] + CONSTRAINT_TOLERANCE,
            "style {style}: exposure {exposure} outside [{}, {}]",
            lower[style],
            upper[style]
        );
    }
}

#[test]
fn concentration_and_short_limits_tighten_boxes() {
    let fixture = fixture(30, 3);

    // Long-short base book: [-0.1, 0.3] boxes.
    let base = fixture
        .problem()
        .with_bounds(vec![-0.1; 30], vec![0.3; 30])
        .unwrap();

    let capped = base
        .clone()
        .with_concentration_limit(0.05)
        .unwrap()
        .to_qp()
        .unwrap();
    assert_eq!(capped.upper_bounds, vec![0.05; 30]);
    assert_eq!(capped.lower_bounds, vec![-0.05; 30]);

    let long_only = base.clone().with_short_limit(0.0).unwrap().to_qp().unwrap();
    assert_eq!(long_only.lower_bounds, vec![0.0; 30]);
    assert_eq!(long_only.upper_bounds, vec![0.3; 30]);

    let short_capped = base.with_short_limit(0.02).unwrap().to_qp().unwrap();
    assert_eq!(short_capped.lower_bounds, vec![-0.02; 30]);

    // The cap only ever tightens: an already stricter bound is kept.
    let stricter = fixture
        .problem()
        .with_bounds(vec![0.0; 30], vec![0.05; 30])
        .unwrap()
        .with_concentration_limit(0.08)
        .unwrap()
        .to_qp()
        .unwrap();
    assert_eq!(stricter.upper_bounds, vec![0.05; 30]);

    let solution = fixture
        .problem()
        .with_concentration_limit(0.06)
        .unwrap()
        .solve(None)
        .unwrap();
    assert_eq!(solution.status, SolveStatus::Solved);
    for (asset, weight) in solution.x.iter().enumerate() {
        assert!(
            *weight <= 0.06 + CONSTRAINT_TOLERANCE,
            "asset {asset}: weight {weight} above the concentration cap"
        );
    }
}

#[test]
fn templates_stack_with_user_constraints_and_each_other() {
    let fixture = fixture(42, 3);
    let assets = fixture.assets();
    let industries = fixture.industries(3);
    let benchmark = fixture.benchmark();
    let exposures = fixture.style_exposures(2);

    // One user inequality row before any template.
    let user_row = Matrix::new(1, assets, vec![1.0 / assets as f64; assets]).unwrap();
    let problem = fixture
        .problem()
        .with_inequalities(user_row.clone(), vec![0.5])
        .unwrap()
        .with_tracking_benchmark(benchmark)
        .unwrap()
        .with_industry_neutrality(&industries)
        .unwrap()
        .with_style_bounds(&exposures, &[-0.25, -0.25], &[0.25, 0.25])
        .unwrap()
        .with_concentration_limit(0.08)
        .unwrap()
        .with_short_limit(0.0)
        .unwrap();

    let qp = problem.to_qp().unwrap();
    // Equalities: budget + 3 industry rows. Inequalities: 1 user row + 2x2
    // style rows. Boxes tightened, no rows added by the box templates.
    assert_eq!(qp.equalities.len(), 4);
    assert_eq!(qp.inequalities.len(), 5);
    assert_eq!(qp.inequalities.matrix.row(0), user_row.row(0));
    assert_eq!(qp.upper_bounds, vec![0.08; assets]);
    assert_eq!(qp.lower_bounds, vec![0.0; assets]);

    let solution = problem.solve(None).unwrap();
    assert_eq!(solution.status, SolveStatus::Solved);
    let residuals = check_kkt(&qp, &solution.x, &solution.dual).unwrap();
    assert!(residuals.primal <= RESIDUAL_TOLERANCE);
    assert!(residuals.dual <= RESIDUAL_TOLERANCE);
}

#[test]
fn sequences_roll_template_targets_through_equality_rhs() {
    let fixture = fixture(36, 3);
    let groups = fixture.industries(3);

    let problem = fixture
        .problem()
        .with_group_targets(&groups, &[0.5, 0.3, 0.2])
        .unwrap();
    let mut sequence = problem.sequence().unwrap();
    let first = sequence.solve_next(&RebalanceStep::default()).unwrap();
    assert_eq!(first.status, SolveStatus::Solved);
    let factorizations = sequence.factorizations();

    // Move the sleeve targets; template rows are ordinary user equality
    // rows, so the whole user RHS (here only the three template rows) rolls.
    let new_targets = vec![0.4, 0.4, 0.2];
    let rolled = sequence
        .solve_next(&RebalanceStep {
            equality_rhs: Some(new_targets.clone()),
            ..RebalanceStep::default()
        })
        .unwrap();
    assert_eq!(rolled.status, SolveStatus::Solved);
    for (group, target) in new_targets.iter().enumerate() {
        let held = group_weight(&rolled.x, &groups, group);
        assert!(
            (held - target).abs() <= CONSTRAINT_TOLERANCE,
            "group {group}: held {held} vs rolled target {target}"
        );
    }
    assert_eq!(
        sequence.factorizations(),
        factorizations,
        "template target updates must be served from the factorization cache"
    );
}

#[test]
fn invalid_template_data_is_rejected() {
    let fixture = fixture(24, 3);
    let assets = fixture.assets();
    let benchmark = fixture.benchmark();

    // Industry neutrality requires a benchmark.
    assert!(matches!(
        fixture
            .problem()
            .with_industry_neutrality(&fixture.industries(3)),
        Err(PortfolioError::Template(_))
    ));
    // Wrong id-vector length.
    assert!(matches!(
        fixture
            .problem()
            .with_tracking_benchmark(benchmark.clone())
            .unwrap()
            .with_industry_neutrality(&vec![0; assets - 1]),
        Err(PortfolioError::Problem(_))
    ));
    // Gap in industry ids leaves industry 1 without members.
    let gappy: Vec<usize> = (0..assets).map(|asset| (asset % 2) * 2).collect();
    assert!(matches!(
        fixture
            .problem()
            .with_tracking_benchmark(benchmark)
            .unwrap()
            .with_industry_neutrality(&gappy),
        Err(PortfolioError::Template(_))
    ));
    // Group id out of the explicit target range.
    assert!(matches!(
        fixture
            .problem()
            .with_group_targets(&fixture.industries(3), &[0.5, 0.5]),
        Err(PortfolioError::Template(_))
    ));
    // Non-finite target.
    assert!(matches!(
        fixture
            .problem()
            .with_group_targets(&fixture.industries(2), &[0.5, f64::NAN]),
        Err(PortfolioError::Problem(_))
    ));

    let exposures = fixture.style_exposures(2);
    // Crossing band.
    assert!(matches!(
        fixture
            .problem()
            .with_style_bounds(&exposures, &[0.3, 0.0], &[0.2, 0.1]),
        Err(PortfolioError::Template(_))
    ));
    // A style with no finite side is a mistake, not a no-op.
    assert!(matches!(
        fixture.problem().with_style_bounds(
            &exposures,
            &[f64::NEG_INFINITY, 0.0],
            &[f64::INFINITY, 0.1]
        ),
        Err(PortfolioError::Template(_))
    ));
    // Wrong exposure width.
    assert!(matches!(
        fixture.problem().with_style_bounds(
            &Matrix::new(1, assets - 1, vec![0.1; assets - 1]).unwrap(),
            &[0.0],
            &[0.1]
        ),
        Err(PortfolioError::Problem(_))
    ));

    // Box templates: invalid caps and contradictions with existing bounds.
    assert!(matches!(
        fixture.problem().with_concentration_limit(0.0),
        Err(PortfolioError::InvalidParameter(_))
    ));
    assert!(matches!(
        fixture.problem().with_short_limit(-0.1),
        Err(PortfolioError::InvalidParameter(_))
    ));
    // Forced minimum position above the concentration cap.
    assert!(matches!(
        fixture
            .problem()
            .with_bounds(vec![0.1; assets], vec![1.0; assets])
            .unwrap()
            .with_concentration_limit(0.05),
        Err(PortfolioError::Problem(_))
    ));
}