Skip to content

Model MarkowitzDemo

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

MarkowitzDemo — 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

Trades off expected return against covariance-based portfolio risk. The historical optimizer example estimates inputs from Yahoo observations, chooses a risk parameter and computes weights. Mean estimates and covariance uncertainty are not eliminated by solving the quadratic program.

What this example teaches

Using this example. Builds a Markowitz allocation from Yahoo history and the risk parameter in main. Asset ordering in the return matrix must match the products associated with the resulting weights. The optimizer computes weights; converting them to executable trades is a separate step.

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

Implementation in this tutorial. Trades off expected return against covariance-based portfolio risk. The historical optimizer example estimates inputs from Yahoo observations, chooses a risk parameter and computes weights. Mean estimates and covariance uncertainty are not eliminated by solving the quadratic program.

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 18.11
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.MarkowitzDemo

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:56.590 [main] INFO  t.n.algoquant.util.config.AQConfig,194 - looking up configuration file: config.default.xml
09:12:56.596 [main] INFO  t.n.algoquant.util.config.AQConfig,194 - looking up configuration file: config/config.default.xml
09:12:56.596 [main] INFO  t.n.algoquant.util.config.AQConfig,209 - loading config file: file:/home/runner/work/algoquant-examples/algoquant-examples/target/model-results/MarkowitzDemo/work/config/config.default.xml


09:12:56.832 [main] INFO  t.n.algoquant.util.config.AQConfig,95 - current directory: /home/runner/work/algoquant-examples/algoquant-examples/target/model-results/MarkowitzDemo/work
09:12:56.832 [main] INFO  t.n.algoquant.util.config.AQConfig,169 - current config:
09:12:56.849 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - nm.license: /home/runner/work/algoquant-examples/algoquant-examples/license/nm.lic
09:12:56.849 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - DATA_FOLDER: ./test-data
09:12:56.852 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - DATA_GAINCAPITAL: ${DATA_FOLDER}/processed/gaincapital/extracted-from-raw-arrange-by-dates
09:12:56.853 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - DATA_BLOOMBERG: ${DATA_FOLDER}/processed/bloomberg
09:12:56.853 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - DATA_YAHOO: ${DATA_FOLDER}/yahoo
09:12:56.853 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - DATA_SGX: ${DATA_FOLDER}/raw/sgx
09:12:56.853 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - DATA_HOLIDAYS: ./log/holidays
09:12:56.853 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - DATA_COMPUSTAT_HK: ${DATA_FOLDER}/[database configuration redacted]/hk
09:12:56.853 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - PDF_OUTPUT_FOLDER: ./log/
09:12:56.854 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - PDF_PLOT_WIDTH: 800
09:12:56.854 [main] INFO  t.n.algoquant.util.config.AQConfig,11 - PDF_PLOT_HEIGHT: 600
09:12:58.127 [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:12:58.127 [main] INFO  t.n.a.d.h.yahoo.YahooEODCacheFactory,196 - reading Yahoo! data for 0019.HK from the Internet from 2005-05-31T00:00 to 2011-10-01T16:00
09:12:58.298 [main] INFO  t.n.a.d.h.y.YahooEODCsvZipFileWriter,30 - writing csv.zip file ./log/Yahoo/0019.HK.csv.zip
09:12:58.325 [main] INFO  t.n.a.d.h.y.YahooEODCsvZipFileWriter,81 - finished writing ./log/Yahoo/0019.HK.csv.zip
09:12:58.328 [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:12:58.328 [main] INFO  t.n.a.d.h.yahoo.YahooEODCacheFactory,196 - reading Yahoo! data for 0483.HK from the Internet from 2005-05-31T00:00 to 2011-10-01T16:00
09:12:58.498 [main] INFO  t.n.a.d.h.y.YahooEODCsvZipFileWriter,30 - writing csv.zip file ./log/Yahoo/0483.HK.csv.zip
09:12:58.526 [main] INFO  t.n.a.d.h.y.YahooEODCsvZipFileWriter,81 - finished writing ./log/Yahoo/0483.HK.csv.zip
09:12:58.527 [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:12:58.527 [main] INFO  t.n.a.d.h.yahoo.YahooEODCacheFactory,196 - reading Yahoo! data for 0321.HK from the Internet from 2005-05-31T00:00 to 2011-10-01T16:00
09:12:58.707 [main] INFO  t.n.a.d.h.y.YahooEODCsvZipFileWriter,30 - writing csv.zip file ./log/Yahoo/0321.HK.csv.zip
... excerpt shortened; complete stdout is in the release archive ...
	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.demo.model.portfoliooptimization.MarkowitzDemo.run(MarkowitzDemo.java:200)
	at tech.nmfin.algoquant.demo.model.portfoliooptimization.MarkowitzDemo.main(MarkowitzDemo.java:80)
	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)
Caused by: java.lang.IllegalArgumentException: dimension of less-than inequalities does not match that of the objective function
	at dev.nm.misc.ArgumentAssertion.assertTrue(meb:430)
	at dev.nm.solver.multivariate.constrained.problem.ConstrainedOptimProblemImpl1.<init>(jkb:37)
	at dev.nm.solver.multivariate.constrained.convex.sdp.socp.qp.problem.QPProblem.<init>(xib:137)
	at tech.nmfin.portfoliooptimization.markowitz.MarkowitzByQP.b(lh:310)
	at tech.nmfin.portfoliooptimization.markowitz.MarkowitzByQP.getOptimalW(lh:294)
	at tech.nmfin.portfoliooptimization.markowitz.MarkowitzByQP.setRiskAversionCoefficient(lh:314)
	at tech.nmfin.algoquant.model.portfoliooptimization.markowitz.Markowitz1952.getOptimalWeights(vh:144)
	... 16 common frames omitted
09:13:00.724 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2006-03-01T00:00
09:13:00.735 [main] INFO  t.n.a.m.s.r.RebalancePortfolioTechnical,107 - products with not enough data (1): [0483.HK @ HKEX]
09:13:00.763 [main] WARN  t.n.a.m.s.r.RebalancePortfolio,194 - failed to compute new positions: failed to compute optimal weights
java.lang.RuntimeException: failed to compute optimal weights
	at tech.nmfin.algoquant.model.portfoliooptimization.markowitz.Markowitz1952.getOptimalWeights(vh:121)
	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.demo.model.portfoliooptimization.MarkowitzDemo.run(MarkowitzDemo.java:200)
	at tech.nmfin.algoquant.demo.model.portfoliooptimization.MarkowitzDemo.main(MarkowitzDemo.java:80)
	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)
Caused by: java.lang.IllegalArgumentException: dimension of less-than inequalities does not match that of the objective function
	at dev.nm.misc.ArgumentAssertion.assertTrue(meb:430)
	at dev.nm.solver.multivariate.constrained.problem.ConstrainedOptimProblemImpl1.<init>(jkb:37)
	at dev.nm.solver.multivariate.constrained.convex.sdp.socp.qp.problem.QPProblem.<init>(xib:137)
	at tech.nmfin.portfoliooptimization.markowitz.MarkowitzByQP.b(lh:310)
	at tech.nmfin.portfoliooptimization.markowitz.MarkowitzByQP.getOptimalW(lh:294)
	at tech.nmfin.portfoliooptimization.markowitz.MarkowitzByQP.setRiskAversionCoefficient(lh:314)
	at tech.nmfin.algoquant.model.portfoliooptimization.markowitz.Markowitz1952.getOptimalWeights(vh:144)
	... 16 common frames omitted
09:13:00.765 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2006-06-01T00:00
09:13:01.005 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2006-09-01T00:00
09:13:01.079 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2006-12-01T00:00
09:13:02.603 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2007-03-01T00:00
09:13:03.732 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2007-06-01T00:00
09:13:05.014 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2007-09-01T00:00
09:13:05.037 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2007-12-01T00:00
09:13:05.076 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2008-03-01T00:00
09:13:05.185 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2008-06-01T00:00
09:13:05.214 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2008-09-01T00:00
09:13:05.256 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2008-12-01T00:00
09:13:06.989 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2009-03-01T00:00
09:13:08.180 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2009-06-01T00:00
09:13:10.505 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2009-09-01T00:00
09:13:11.853 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2009-12-01T00:00
09:13:11.880 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2010-03-01T00:00
09:13:12.892 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2010-06-01T00:00
09:13:12.961 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2010-09-01T00:00
09:13:12.993 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2010-12-01T00:00
09:13:13.051 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2011-03-01T00:00
09:13:13.158 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2011-06-01T00:00
09:13:13.728 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2011-09-01T00:00
09:13:13.865 [main] INFO  t.n.a.d.m.p.MarkowitzDemo,201 - report = Performance Report:
Product	Profit Loss	Commission	Information Ratio For Periods	Max Drawdown Percentage	Execution Count
0001.HK	-6819.425337676541	37174.63315501974	-0.2103619009173625	0.4296545296941963	22.0
0019.HK	77403.40105230222	16264.145855772758	-0.12600341448980962	0.190684050713682	22.0
0321.HK	-672165.6350004426	59117.05756295586	-0.003126493405660961	0.9829640241287784	22.0
0483.HK	3109914.369640106	75876.7880696143	0.7829449030080462	0.38789552511629116	22.0
Total	2508332.710354289	188432.62464336268	0.6990067798952058	0.5790959614498701	88.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

java.lang.RuntimeException: failed to compute optimal weights
Caused by: java.lang.IllegalArgumentException: dimension of less-than inequalities does not match that of the objective function
java.lang.RuntimeException: failed to compute optimal weights
Caused by: java.lang.IllegalArgumentException: dimension of less-than inequalities does not match that of the objective function
java.lang.RuntimeException: failed to compute optimal weights
Caused by: java.lang.IllegalArgumentException: dimension of less-than inequalities does not match that of the objective function
java.lang.RuntimeException: failed to compute optimal weights
Caused by: java.lang.IllegalArgumentException: dimension of less-than inequalities does not match that of the objective function

Downloadable evidence

Download MarkowitzDemo.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