Skip to content

Model PortfolioOptimComparisons2Demo

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

PortfolioOptimComparisons2Demo — quantitative model tutorial

All quantitative model results · Pinned source · Javadoc

Related papers 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.

This comparison also includes Corvalán’s near-efficient diversification framework (2005) and Lai, Xing and Chen’s estimation-risk framework (2011). The first permits diversification within a tolerance of a risk–return target; the second accounts for uncertain estimated inputs through predictive/resampling methods. Equal-weight and simplified-input baselines help distinguish the effects of estimation, optimization and post-processing. A common historical interval does not make the algorithms’ objectives identical.

How this demo connects to the idea

Compares allocation methods on selected historical/local data. A common estimation window and asset ordering are required for a fair comparison. Several alternative dataset branches are embedded in main; results depend on the branch and its data coverage.

What this example teaches

Compares the different asset allocation algorithms. Prices are from historical data.

Using this example. Compares allocation algorithms on historical price data. Some branches reference local China datasets and an absolute Windows Compustat path; choose the intended branch and configure those inputs before running. Consistent asset ordering and common estimation periods are essential to the comparison.

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

Implementation in this tutorial. Compares allocation methods on selected historical/local data. A common estimation window and asset ordering are required for a fair comparison. Several alternative dataset branches are embedded in main; results depend on the branch and its data coverage.

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 7.27
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.PortfolioOptimComparisons2Demo

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:12:34.932 [main] INFO  t.n.a.m.p.s.OptimizationAlgorithmComparisonSimulation,12 - Running algorithm 'equal' (window=P6M, rebalance=P6M)...
09:12:35.004 [main] INFO  t.n.algoquant.util.config.AQConfig,194 - looking up configuration file: config.default.xml
09:12:35.005 [main] INFO  t.n.algoquant.util.config.AQConfig,194 - looking up configuration file: config/config.default.xml
09:12:35.006 [main] INFO  t.n.algoquant.util.config.AQConfig,209 - loading config file: file:/home/runner/work/algoquant-examples/algoquant-examples/target/model-results/PortfolioOptimComparisons2Demo/work/config/config.default.xml


09:12:35.205 [main] INFO  t.n.algoquant.util.config.AQConfig,95 - current directory: /home/runner/work/algoquant-examples/algoquant-examples/target/model-results/PortfolioOptimComparisons2Demo/work
09:12:35.205 [main] INFO  t.n.algoquant.util.config.AQConfig,169 - current config:
09:12:35.223 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - nm.license: /home/runner/work/algoquant-examples/algoquant-examples/license/nm.lic
09:12:35.224 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - DATA_FOLDER: ./test-data
09:12:35.224 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - DATA_GAINCAPITAL: ${DATA_FOLDER}/processed/gaincapital/extracted-from-raw-arrange-by-dates
09:12:35.225 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - DATA_BLOOMBERG: ${DATA_FOLDER}/processed/bloomberg
09:12:35.225 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - DATA_YAHOO: ${DATA_FOLDER}/yahoo
09:12:35.225 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - DATA_SGX: ${DATA_FOLDER}/raw/sgx
09:12:35.228 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - DATA_HOLIDAYS: ./log/holidays
09:12:35.229 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - DATA_COMPUSTAT_HK: ${DATA_FOLDER}/[database configuration redacted]/hk
09:12:35.229 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - PDF_OUTPUT_FOLDER: ./log/
09:12:35.230 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - PDF_PLOT_WIDTH: 800
09:12:35.230 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - PDF_PLOT_HEIGHT: 600
09:12:36.386 [main] INFO  t.n.a.d.h.yahoo.YahooEODCacheFactory,196 - reading Yahoo! data for ^IRX from the Internet from 2016-06-29T00:00 to 2018-08-04T16:00
09:12:36.502 [main] INFO  t.n.a.d.h.yahoo.YahooEODCacheFactory,196 - reading Yahoo! data for ^GSPC from the Internet from 2015-06-01T00:00 to 2019-01-15T16:00
09:12:36.722 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2016-07-29T00:00
09:12:36.725 [main] INFO  t.n.a.m.s.r.RebalancePortfolioTechnical,153 - dropped products with no data (13): [low_risk_1(CNY), low_risk_2(CNY), low_risk_3(CNY), low_risk_4(CNY), low_risk_5(CNY), risk_1(CNY), risk_2(CNY), risk_3(CNY), risk_4(CNY), risk_5(CNY), risk_6(CNY), risk_7(CNY), risk_8(CNY)]
09:12:36.735 [main] WARN  t.n.a.m.s.r.RebalancePortfolio,194 - failed to compute new positions: input caches become empty after filtering
java.lang.IllegalArgumentException: input caches become empty after filtering
	at tech.nmfin.algoquant.data.cache.util.Cache2ReturnsMatrix.s(bkb:15)
	at tech.nmfin.algoquant.data.cache.util.Cache2ReturnsMatrix.<init>(bkb:191)
	at tech.nmfin.algoquant.model.strategy.rebalance.RebalancePortfolioTechnical.s(r:340)
	at tech.nmfin.algoquant.model.strategy.rebalance.RebalancePortfolioTechnical.getPositions(r:196)
... excerpt shortened; complete stdout is in the release archive ...
	at tech.nmfin.portfoliooptimization.lai2010.Lai2010NPEBModel.optimalWeights(gj:13)
	at tech.nmfin.portfoliooptimization.Lai2010OptimizationAlgorithm.getOptimalWeights(ri:51)
	at tech.nmfin.algoquant.model.portfoliooptimization.corvalan2005.Corvalan2005Diversifier.getOptimalWeights(yb:58)
	at tech.nmfin.algoquant.model.strategy.rebalance.RebalancePortfolioTechnical.getPositions(r:19)
	at tech.nmfin.algoquant.model.strategy.rebalance.RebalancePortfolio.s(z:196)
	at tech.nmfin.algoquant.model.strategy.rebalance.RebalancePortfolio.onEvent(z:11)
	at tech.nmfin.algoquant.execution.component.simulator.message.handler.ChannelMessageHandlerFactories$1$1.handle(hmb:200)
	at tech.nmfin.algoquant.execution.component.simulator.message.dispatcher.SimpleChannelMessageDispatcher.dispatch(pfb:187)
	at tech.nmfin.algoquant.execution.component.simulator.StrategyComponent.dispatch(ybb:150)
	at dev.nm.nmutils.circuit.controller.SingleThreadedController.run(uwb:137)
	at tech.nmfin.algoquant.execution.component.simulator.SimpleSimulator.run(ujb:132)
	at tech.nmfin.algoquant.execution.simulation.template.SimTemplateUtils.run(kq:48)
	at tech.nmfin.algoquant.model.portfoliooptimization.simulation.PortfolioOptimizationSimulation.run(ym:767)
	at tech.nmfin.algoquant.model.portfoliooptimization.simulation.OptimizationAlgorithmComparisonSimulation.s(ne:437)
	at tech.nmfin.algoquant.model.portfoliooptimization.simulation.OptimizationAlgorithmComparisonSimulation.s(ne:163)
	at java.util.LinkedHashMap$LinkedEntrySet.forEach(LinkedHashMap.java:671)
	at tech.nmfin.algoquant.model.portfoliooptimization.simulation.OptimizationAlgorithmComparisonSimulation.run(ne:193)
	at tech.nmfin.algoquant.demo.model.portfoliooptimization.PortfolioOptimComparisons2Demo.lambda$run$1(PortfolioOptimComparisons2Demo.java:230)
	at java.lang.Iterable.forEach(Iterable.java:75)
	at tech.nmfin.algoquant.demo.model.portfoliooptimization.PortfolioOptimComparisons2Demo.run(PortfolioOptimComparisons2Demo.java:135)
	at tech.nmfin.algoquant.demo.model.portfoliooptimization.PortfolioOptimComparisons2Demo.demo_csv_0000(PortfolioOptimComparisons2Demo.java:276)
	at tech.nmfin.algoquant.demo.model.portfoliooptimization.PortfolioOptimComparisons2Demo.demo_csv_0010(PortfolioOptimComparisons2Demo.java:286)
	at tech.nmfin.algoquant.demo.model.portfoliooptimization.PortfolioOptimComparisons2Demo.main(PortfolioOptimComparisons2Demo.java:436)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at ModelDemoRunner.main(ModelDemoRunner.java:38)
09:12:39.455 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2018-07-29T00:00
09:12:39.457 [main] WARN  t.n.a.m.s.r.RebalancePortfolio,194 - failed to compute new positions: lag (1) must be < length of the time series (1)
java.lang.IllegalArgumentException: lag (1) must be < length of the time series (1)
	at dev.nm.misc.ArgumentAssertion.assertTrue(meb:430)
	at dev.nm.stat.timeseries.linear.univariate.sample.SampleAutoCovariance.evaluate(dz:50)
	at dev.nm.stat.timeseries.linear.univariate.sample.SampleAutoCorrelation.evaluate(er:63)
	at dev.nm.stat.random.sampler.resampler.bootstrap.block.PattonPolitisWhite2009$1.forLagOrder(ss:43)
	at dev.nm.stat.random.sampler.resampler.bootstrap.block.PattonPolitisWhite2009ForObject.getOptimalLag(xd:437)
	at dev.nm.stat.random.sampler.resampler.bootstrap.block.PattonPolitisWhite2009ForObject.getOptimalBlockLength(xd:305)
	at dev.nm.stat.random.sampler.resampler.bootstrap.block.PattonPolitisWhite2009ForObject.<init>(xd:190)
	at dev.nm.stat.random.sampler.resampler.bootstrap.block.PattonPolitisWhite2009ForObject.<init>(xd:204)
	at dev.nm.stat.random.sampler.resampler.bootstrap.block.PattonPolitisWhite2009.<init>(ss:212)
	at dev.nm.stat.random.sampler.resampler.bootstrap.block.PattonPolitisWhite2009.<init>(ss:228)
	at dev.nm.stat.random.sampler.resampler.multivariate.GroupResampler.<init>(qp:27)
	at tech.nmfin.portfoliooptimization.lai2010.ceta.npeb.resampler.GroupResamplerFactory$1.<init>(zf:168)
	at tech.nmfin.portfoliooptimization.lai2010.ceta.npeb.resampler.GroupResamplerFactory.newResampler(zf:185)
	at tech.nmfin.portfoliooptimization.lai2010.ceta.npeb.NPEBPortfolioMomentsEstimator.<init>(vj:12)
	at tech.nmfin.portfoliooptimization.lai2010.Lai2010NPEBModel.optimalWeights(gj:13)
	at tech.nmfin.portfoliooptimization.Lai2010OptimizationAlgorithm.getOptimalWeights(ri:51)
	at tech.nmfin.algoquant.model.portfoliooptimization.corvalan2005.Corvalan2005Diversifier.getOptimalWeights(yb:58)
	at tech.nmfin.algoquant.model.strategy.rebalance.RebalancePortfolioTechnical.getPositions(r:19)
	at tech.nmfin.algoquant.model.strategy.rebalance.RebalancePortfolio.s(z:196)
	at tech.nmfin.algoquant.model.strategy.rebalance.RebalancePortfolio.onEvent(z:11)
	at tech.nmfin.algoquant.execution.component.simulator.message.handler.ChannelMessageHandlerFactories$1$1.handle(hmb:200)
	at tech.nmfin.algoquant.execution.component.simulator.message.dispatcher.SimpleChannelMessageDispatcher.dispatch(pfb:187)
	at tech.nmfin.algoquant.execution.component.simulator.StrategyComponent.dispatch(ybb:150)
	at dev.nm.nmutils.circuit.controller.SingleThreadedController.run(uwb:137)
	at tech.nmfin.algoquant.execution.component.simulator.SimpleSimulator.run(ujb:132)
	at tech.nmfin.algoquant.execution.simulation.template.SimTemplateUtils.run(kq:48)
	at tech.nmfin.algoquant.model.portfoliooptimization.simulation.PortfolioOptimizationSimulation.run(ym:767)
	at tech.nmfin.algoquant.model.portfoliooptimization.simulation.OptimizationAlgorithmComparisonSimulation.s(ne:437)
	at tech.nmfin.algoquant.model.portfoliooptimization.simulation.OptimizationAlgorithmComparisonSimulation.s(ne:163)
	at java.util.LinkedHashMap$LinkedEntrySet.forEach(LinkedHashMap.java:671)
	at tech.nmfin.algoquant.model.portfoliooptimization.simulation.OptimizationAlgorithmComparisonSimulation.run(ne:193)
	at tech.nmfin.algoquant.demo.model.portfoliooptimization.PortfolioOptimComparisons2Demo.lambda$run$1(PortfolioOptimComparisons2Demo.java:230)
	at java.lang.Iterable.forEach(Iterable.java:75)
	at tech.nmfin.algoquant.demo.model.portfoliooptimization.PortfolioOptimComparisons2Demo.run(PortfolioOptimComparisons2Demo.java:135)
	at tech.nmfin.algoquant.demo.model.portfoliooptimization.PortfolioOptimComparisons2Demo.demo_csv_0000(PortfolioOptimComparisons2Demo.java:276)
	at tech.nmfin.algoquant.demo.model.portfoliooptimization.PortfolioOptimComparisons2Demo.demo_csv_0010(PortfolioOptimComparisons2Demo.java:286)
	at tech.nmfin.algoquant.demo.model.portfoliooptimization.PortfolioOptimComparisons2Demo.main(PortfolioOptimComparisons2Demo.java:436)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at ModelDemoRunner.main(ModelDemoRunner.java:38)
09:12:39.571 [main] INFO  t.n.a.m.p.s.OptimizationAlgorithmComparisonSimulation,179 - Finished algorithm 'lai2010-corvalan' (window=P6M, rebalance=P6M)
09:12:40.766 [main] INFO  t.n.a.e.r.SimulationResultsExporter,766 - Saving to /home/runner/work/algoquant-examples/algoquant-examples/target/model-results/PortfolioOptimComparisons2Demo/work/log/PortfolioOptimComparisons2Demo.2026.10.18,09.12.40.measures.csv...
09:12:40.791 [main] INFO  t.n.a.e.r.SimulationResultsExporter,766 - Saving to /home/runner/work/algoquant-examples/algoquant-examples/target/model-results/PortfolioOptimComparisons2Demo/work/log/PortfolioOptimComparisons2Demo.2026.10.18,09.12.40.executions.csv...
09:12:40.796 [main] INFO  t.n.a.e.r.SimulationResultsExporter,766 - Saving to /home/runner/work/algoquant-examples/algoquant-examples/target/model-results/PortfolioOptimComparisons2Demo/work/log/PortfolioOptimComparisons2Demo.2026.10.18,09.12.40.trades.csv...
09:12:40.802 [main] INFO  t.n.a.e.r.ValueHistoriesExporter,64 - Saving to /home/runner/work/algoquant-examples/algoquant-examples/target/model-results/PortfolioOptimComparisons2Demo/work/log/PortfolioOptimComparisons2Demo.2026.10.18,09.12.40.computed.weights.csv...
09:12:40.808 [main] INFO  t.n.a.e.r.ValueHistoriesExporter,64 - Saving to /home/runner/work/algoquant-examples/algoquant-examples/target/model-results/PortfolioOptimComparisons2Demo/work/log/PortfolioOptimComparisons2Demo.2026.10.18,09.12.40.actual.weights.csv...
09:12:40.812 [main] INFO  t.n.a.e.r.ValueHistoriesExporter,64 - Saving to /home/runner/work/algoquant-examples/algoquant-examples/target/model-results/PortfolioOptimComparisons2Demo/work/log/PortfolioOptimComparisons2Demo.2026.10.18,09.12.40.profit.csv...

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

java.lang.IllegalArgumentException: input caches become empty after filtering
java.lang.IllegalArgumentException: input caches become empty after filtering
java.lang.IllegalArgumentException: input caches become empty after filtering
java.lang.IllegalArgumentException: input caches become empty after filtering
java.lang.IllegalArgumentException: input caches become empty after filtering
java.lang.IllegalArgumentException: input caches become empty after filtering
java.lang.IllegalArgumentException: input caches become empty after filtering
java.lang.IllegalArgumentException: lag (1) must be < length of the time series (1)
java.lang.IllegalArgumentException: lag (1) must be < length of the time series (1)
java.lang.IllegalArgumentException: lag (1) must be < length of the time series (1)
java.lang.IllegalArgumentException: lag (1) must be < length of the time series (1)
java.lang.IllegalArgumentException: input caches become empty after filtering
java.lang.IllegalArgumentException: The ending index must not be smaller than the beginning index!
java.lang.IllegalArgumentException: The ending index must not be smaller than the beginning index!
java.lang.IllegalArgumentException: The ending index must not be smaller than the beginning index!
java.lang.IllegalArgumentException: The ending index must not be smaller than the beginning index!
java.lang.IllegalArgumentException: input caches become empty after filtering
java.lang.IllegalArgumentException: The ending index must not be smaller than the beginning index!

Charts from this run

Chart 1

Chart 2

Chart 3

Chart 4

Downloadable evidence

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

Export Bytes
files/charts/chart-01.png 74498
files/charts/chart-02.png 91207
files/log/20261018091234/Portfolio Optimization Algorithms (P6M,P6M,10000.00,0.50) monthly returns.png 88753
files/log/20261018091234/Portfolio Optimization Algorithms (P6M,P6M,10000.00,0.50).png 73737
files/log/PortfolioOptimComparisons2Demo.2026.10.18,09.12.40.actual.weights.csv 3706
files/log/PortfolioOptimComparisons2Demo.2026.10.18,09.12.40.computed.weights.csv 3708
files/log/PortfolioOptimComparisons2Demo.2026.10.18,09.12.40.executions.csv 4974
files/log/PortfolioOptimComparisons2Demo.2026.10.18,09.12.40.measures.csv 38958
files/log/PortfolioOptimComparisons2Demo.2026.10.18,09.12.40.profit.csv 65723
files/log/PortfolioOptimComparisons2Demo.2026.10.18,09.12.40.trades.csv 5724

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