Skip to content

Model PortfolioOptimizationBacktestDemo

github-actions[bot] edited this page Oct 18, 2026 · 8 revisions

PortfolioOptimizationBacktestDemo — quantitative model tutorial

All quantitative model results · Pinned source · Javadoc

Paper introduction

Harry Markowitz, Portfolio Selection (1952) explains diversification as a portfolio problem: an asset's risk cannot be judged independently of how its returns move with the other holdings. Expected portfolio return is a weighted mean; portfolio variance depends on the full covariance matrix.

The efficient set contains portfolios for which expected return cannot be improved without accepting more variance, or variance reduced without giving up expected return. An investor selects a tradeoff according to preferences and constraints. Holding many positively correlated assets need not provide much diversification.

The framework assumes the relevant return means and covariances are supplied. In practice, estimates can be unstable and constraints can materially change the answer. A solved optimization is therefore a set of weights under assumptions, not evidence of future outperformance. The original article was reviewed. Rolling calibration, rebalance schedules, costs and the dataset used here belong to the demo, not to the static 1952 formulation.

How this demo connects to the idea

A rolling backtest separates estimated weights from realized returns after rebalancing. Follow the data-window and rebalance callbacks to see when each estimate is available. Transaction and turnover conventions belong to the simulation, not the static mean–variance equations.

What this example teaches

Backtests a particular optimizer to see its performance over time using historical data.

Using this example. Evaluates a portfolio optimizer over historical windows rather than computing one static allocation. Read the configured estimation, rebalancing and simulation periods together. Training inputs must be available before the corresponding rebalance decision.

Model and publication. Related theory and the scope of this example are distinguished below.

Implementation in this tutorial. A rolling backtest separates estimated weights from realized returns after rebalancing. Follow the data-window and rebalance callbacks to see when each estimate is available. Transaction and turnover conventions belong to the simulation, not the static mean–variance equations.

Verified execution record

Item Value
Status completed
Run 39114360151
UTC publication 2026-10-18 09:18 UTC
Source commit 355aa9242fc2a247b256aecc8406cbb409aa5ff3
Data mode original main defaults
Exit code 0
Elapsed seconds 5.81
Time limit seconds 180
Maximum Java heap 1200m

The original main returned successfully. Read the output and model assumptions before interpreting it.

Reproduce the calculation

Follow the repository setup for JDK 8, Maven credentials and an external NM license. From the repository root:

mvn -U -Dmaven.test.skip=true clean package
mvn exec:java -Dexec.mainClass=tech.nmfin.algoquant.demo.model.portfoliooptimization.PortfolioOptimizationBacktestDemo

The automated run uses scripts/ModelDemoRunner.java to initialize the external license before the unchanged main, save JFreeChart windows as PNGs, and exit after main returns. It uses an isolated working directory with the repository datasets/configuration linked in. Running directly may leave desktop windows open.

Observed output


09:13:25.684 [main] INFO  t.n.algoquant.util.config.AQConfig,194 - looking up configuration file: config.default.xml
09:13:25.694 [main] INFO  t.n.algoquant.util.config.AQConfig,194 - looking up configuration file: config/config.default.xml
09:13:25.694 [main] INFO  t.n.algoquant.util.config.AQConfig,209 - loading config file: file:/home/runner/work/algoquant-examples/algoquant-examples/target/model-results/PortfolioOptimizationBacktestDemo/work/config/config.default.xml


09:13:25.937 [main] INFO  t.n.algoquant.util.config.AQConfig,95 - current directory: /home/runner/work/algoquant-examples/algoquant-examples/target/model-results/PortfolioOptimizationBacktestDemo/work
09:13:25.937 [main] INFO  t.n.algoquant.util.config.AQConfig,169 - current config:
09:13:25.970 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - nm.license: /home/runner/work/algoquant-examples/algoquant-examples/license/nm.lic
09:13:25.971 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - DATA_FOLDER: ./test-data
09:13:25.971 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - DATA_GAINCAPITAL: ${DATA_FOLDER}/processed/gaincapital/extracted-from-raw-arrange-by-dates
09:13:25.971 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - DATA_BLOOMBERG: ${DATA_FOLDER}/processed/bloomberg
09:13:25.971 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - DATA_YAHOO: ${DATA_FOLDER}/yahoo
09:13:25.972 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - DATA_SGX: ${DATA_FOLDER}/raw/sgx
09:13:25.972 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - DATA_HOLIDAYS: ./log/holidays
09:13:25.976 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - DATA_COMPUSTAT_HK: ${DATA_FOLDER}/[database configuration redacted]/hk
09:13:25.976 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - PDF_OUTPUT_FOLDER: ./log/
09:13:25.976 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - PDF_PLOT_WIDTH: 800
09:13:25.976 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - PDF_PLOT_HEIGHT: 600
09:13:27.573 [main] INFO  t.n.a.d.h.yahoo.YahooEODCacheFactory,237 - failed to load from file, reason: no data read from file; load from web
09:13:27.574 [main] INFO  t.n.a.d.h.yahoo.YahooEODCacheFactory,196 - reading Yahoo! data for AAPL from the Internet from 2014-07-01T00:00 to 2017-01-01T16:00
09:13:27.700 [main] INFO  t.n.a.d.h.y.YahooEODCsvZipFileWriter,30 - writing csv.zip file ./log/20261018/AAPL.csv.zip
09:13:27.717 [main] INFO  t.n.a.d.h.y.YahooEODCsvZipFileWriter,81 - finished writing ./log/20261018/AAPL.csv.zip
09:13:27.720 [main] INFO  t.n.a.d.h.yahoo.YahooEODCacheFactory,237 - failed to load from file, reason: no data read from file; load from web
09:13:27.720 [main] INFO  t.n.a.d.h.yahoo.YahooEODCacheFactory,196 - reading Yahoo! data for GOOG from the Internet from 2014-07-01T00:00 to 2017-01-01T16:00
09:13:27.818 [main] INFO  t.n.a.d.h.y.YahooEODCsvZipFileWriter,30 - writing csv.zip file ./log/20261018/GOOG.csv.zip
09:13:27.832 [main] INFO  t.n.a.d.h.y.YahooEODCsvZipFileWriter,81 - finished writing ./log/20261018/GOOG.csv.zip
09:13:27.834 [main] INFO  t.n.a.d.h.yahoo.YahooEODCacheFactory,237 - failed to load from file, reason: no data read from file; load from web
09:13:27.835 [main] INFO  t.n.a.d.h.yahoo.YahooEODCacheFactory,196 - reading Yahoo! data for MSFT from the Internet from 2014-07-01T00:00 to 2017-01-01T16:00
09:13:27.935 [main] INFO  t.n.a.d.h.y.YahooEODCsvZipFileWriter,30 - writing csv.zip file ./log/20261018/MSFT.csv.zip
... excerpt shortened; complete stdout is in the release archive ...
2016-09-09: 0.0
2016-09-12: 0.0
2016-09-13: 0.0
2016-09-14: 0.0
2016-09-15: 0.0
2016-09-16: 0.0
2016-09-19: 0.0
2016-09-20: 0.0
2016-09-21: 0.0
2016-09-22: 0.0
2016-09-23: 0.0
2016-09-26: 0.0
2016-09-27: 0.0
2016-09-28: 0.0
2016-09-29: 0.0
2016-09-30: 0.0
2016-10-03: 0.0
2016-10-04: 0.0
2016-10-05: 0.0
2016-10-06: 0.0
2016-10-07: 0.0
2016-10-10: 0.0
2016-10-11: 0.0
2016-10-12: 0.0
2016-10-13: 0.0
2016-10-14: 0.0
2016-10-17: 0.0
2016-10-18: 0.0
2016-10-19: 0.0
2016-10-20: 0.0
2016-10-21: 0.0
2016-10-24: 0.0
2016-10-25: 0.0
2016-10-26: 0.0
2016-10-27: 0.0
2016-10-28: 0.0
2016-10-31: 0.0
2016-11-01: 0.0
2016-11-02: 0.0
2016-11-03: 0.0
2016-11-04: 0.0
2016-11-07: 0.0
2016-11-08: 0.0
2016-11-09: 0.0
2016-11-10: 0.0
2016-11-11: 0.0
2016-11-14: 0.0
2016-11-15: 0.0
2016-11-16: 0.0
2016-11-17: 0.0
2016-11-18: 0.0
2016-11-21: 0.0
2016-11-22: 0.0
2016-11-23: 0.0
2016-11-25: 0.0
2016-11-28: 0.0
2016-11-29: 0.0
2016-11-30: 0.0
2016-12-01: 0.0
2016-12-02: 0.0
2016-12-05: 0.0
2016-12-06: 0.0
2016-12-07: 0.0
2016-12-08: 0.0
2016-12-09: 0.0
2016-12-12: 0.0
2016-12-13: 0.0
2016-12-14: 0.0
2016-12-15: 0.0
2016-12-16: 0.0
2016-12-19: 0.0
2016-12-20: 0.0
2016-12-21: 0.0
2016-12-22: 0.0
2016-12-23: 0.0
2016-12-27: 0.0
2016-12-28: 0.0
2016-12-29: 0.0
2016-12-30: 0.0

Execution caveat: Main returned, but the log contains exceptions, failed operations or undefined values. This records process completion only; inspect the diagnostic below before treating any output as a valid backtest.

Diagnostic and prerequisites

tech.nmfin.algoquant.model.portfoliooptimization.AlgorithmFailureException: algorithm 'SOCPOptimizationAlgorithm' failed; reason: failed to compute the optimal weights: sorry, best effort fails; the problem is probably ill-defined: 
Caused by: java.lang.Exception: sorry, best effort fails; the problem is probably ill-defined: 
tech.nmfin.algoquant.model.portfoliooptimization.AlgorithmFailureException: algorithm 'SOCPOptimizationAlgorithm' failed; reason: failed to compute the optimal weights: sorry, best effort fails; the problem is probably ill-defined: 
Caused by: java.lang.Exception: sorry, best effort fails; the problem is probably ill-defined: 
tech.nmfin.algoquant.model.portfoliooptimization.AlgorithmFailureException: algorithm 'SOCPOptimizationAlgorithm' failed; reason: failed to compute the optimal weights: sorry, best effort fails; the problem is probably ill-defined: 
Caused by: java.lang.Exception: sorry, best effort fails; the problem is probably ill-defined: 
tech.nmfin.algoquant.model.portfoliooptimization.AlgorithmFailureException: algorithm 'SOCPOptimizationAlgorithm' failed; reason: failed to compute the optimal weights: sorry, best effort fails; the problem is probably ill-defined: 
Caused by: java.lang.Exception: sorry, best effort fails; the problem is probably ill-defined: 
tech.nmfin.algoquant.model.portfoliooptimization.AlgorithmFailureException: algorithm 'SOCPOptimizationAlgorithm' failed; reason: failed to compute the optimal weights: sorry, best effort fails; the problem is probably ill-defined: 
Caused by: java.lang.Exception: sorry, best effort fails; the problem is probably ill-defined: 
tech.nmfin.algoquant.model.portfoliooptimization.AlgorithmFailureException: algorithm 'SOCPOptimizationAlgorithm' failed; reason: failed to compute the optimal weights: sorry, best effort fails; the problem is probably ill-defined: 
Caused by: java.lang.Exception: sorry, best effort fails; the problem is probably ill-defined: 
tech.nmfin.algoquant.model.portfoliooptimization.AlgorithmFailureException: algorithm 'SOCPOptimizationAlgorithm' failed; reason: failed to compute the optimal weights: sorry, best effort fails; the problem is probably ill-defined: 
Caused by: java.lang.Exception: sorry, best effort fails; the problem is probably ill-defined: 
tech.nmfin.algoquant.model.portfoliooptimization.AlgorithmFailureException: algorithm 'SOCPOptimizationAlgorithm' failed; reason: failed to compute the optimal weights: sorry, best effort fails; the problem is probably ill-defined: 
Caused by: java.lang.Exception: sorry, best effort fails; the problem is probably ill-defined: 
tech.nmfin.algoquant.model.portfoliooptimization.AlgorithmFailureException: algorithm 'SOCPOptimizationAlgorithm' failed; reason: failed to compute the optimal weights: sorry, best effort fails; the problem is probably ill-defined: 
Caused by: java.lang.Exception: sorry, best effort fails; the problem is probably ill-defined: 

Downloadable evidence

Download PortfolioOptimizationBacktestDemo.zip contains stdout, stderr, the execution record, checksums and any exported CSV/chart files.

Interpreting the result

Read the reported units: optimizer weights, model-fit coefficients, forecast correlations and simulated P/L are different outputs. Synthetic runs demonstrate the API rather than historical market returns. Inspect undefined values and open positions instead of replacing them with zero. Chart exports visualize exactly this run.

For model assumptions, original publications and adaptation limits, use the literature guide.

Clone this wiki locally