Skip to content

Model MarkowitzDemo

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

MarkowitzDemo — execution tutorial

All model results · Pinned source · Javadoc

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 39114006111
UTC publication 2026-10-18 09:11 UTC
Source commit 0097e73c040bdaaddac570e2fe694d9836b676ee
Data mode original main defaults
Exit code 0
Elapsed seconds 23.6
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:07:21,713 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
09:07:21,715 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
09:07:21,715 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [jar:file:/home/runner/work/algoquant-examples/algoquant-examples/target/lib/algoquant-dist-3.3.0-SNAPSHOT.jar!/logback.xml]
09:07:21,762 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@17d99928 - URL [jar:file:/home/runner/work/algoquant-examples/algoquant-examples/target/lib/algoquant-dist-3.3.0-SNAPSHOT.jar!/logback.xml] is not of type file
09:07:21,829 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
09:07:21,830 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
09:07:21,841 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
09:07:21,851 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
09:07:21,913 |-INFO in ch.qos.logback.core.joran.action.TimestampAction - Using current interpretation time, i.e. now, as time reference.
09:07:21,914 |-INFO in ch.qos.logback.core.joran.action.TimestampAction - Adding property to the context with key="bySecond" and value="2026.10.18,09.07.21" to the LOCAL scope
09:07:21,915 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
09:07:21,918 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [FILE1]
09:07:21,932 |-INFO in ch.qos.logback.core.rolling.FixedWindowRollingPolicy@3834d63f - Will use zip compression
09:07:21,932 |-WARN in ch.qos.logback.core.rolling.FixedWindowRollingPolicy@3834d63f - Large window sizes are not allowed.
09:07:21,932 |-WARN in ch.qos.logback.core.rolling.FixedWindowRollingPolicy@3834d63f - MaxIndex reduced to 21
09:07:21,938 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
09:07:21,942 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE1] - Active log file name: log/algoquant.2026.10.18,09.07.21.log
09:07:21,942 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE1] - File property is set to [log/algoquant.2026.10.18,09.07.21.log]
09:07:21,943 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
09:07:21,943 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
09:07:21,944 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE1] to Logger[ROOT]
09:07:21,944 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
09:07:21,945 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@1ae369b7 - Registering current configuration as safe fallback point

09:07:22.110 [main] INFO  t.n.algoquant.util.config.AQConfig,194 - looking up configuration file: config.default.xml
09:07:22.116 [main] INFO  t.n.algoquant.util.config.AQConfig,194 - looking up configuration file: config/config.default.xml
09:07:22.119 [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:07:22.435 [main] INFO  t.n.algoquant.util.config.AQConfig,148 - 09:07:21,713 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
09:07:21,715 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
09:07:21,715 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [jar:file:/home/runner/work/algoquant-examples/algoquant-examples/target/lib/algoquant-dist-3.3.0-SNAPSHOT.jar!/logback.xml]
... 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:35)
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:07:26.441 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2006-03-01T00:00
09:07:26.480 [main] INFO  t.n.a.m.s.r.RebalancePortfolioTechnical,107 - products with not enough data (1): [0483.HK @ HKEX]
09:07:26.488 [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:35)
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:07:26.494 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2006-06-01T00:00
09:07:26.867 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2006-09-01T00:00
09:07:27.006 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2006-12-01T00:00
09:07:29.189 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2007-03-01T00:00
09:07:30.755 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2007-06-01T00:00
09:07:33.034 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2007-09-01T00:00
09:07:33.099 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2007-12-01T00:00
09:07:33.165 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2008-03-01T00:00
09:07:33.482 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2008-06-01T00:00
09:07:33.524 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2008-09-01T00:00
09:07:33.561 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2008-12-01T00:00
09:07:35.057 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2009-03-01T00:00
09:07:37.434 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2009-06-01T00:00
09:07:39.942 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2009-09-01T00:00
09:07:41.239 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2009-12-01T00:00
09:07:41.270 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2010-03-01T00:00
09:07:43.620 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2010-06-01T00:00
09:07:43.669 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2010-09-01T00:00
09:07:43.698 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2010-12-01T00:00
09:07:43.723 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2011-03-01T00:00
09:07:43.755 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2011-06-01T00:00
09:07:44.193 [main] INFO  t.n.a.m.s.r.RebalancePortfolio,173 - Calculating weights for rebalancing point at 2011-09-01T00:00
09:07:44.543 [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	-73191.63934183214	39118.10766321179	-0.2487202830881555	0.4558403058010594	22.0
0019.HK	-10045.425067666336	16751.09939491026	-0.31960368502572295	0.2059301437570172	22.0
0321.HK	-506380.7486037654	67368.41040137618	0.1030250272217886	0.8787988513830454	22.0
0483.HK	3045899.4746919647	81035.40944482393	0.7407698669827318	0.39196051888376926	22.0
Total	2456281.6616787007	204273.0269043221	0.6880982967836381	0.5797616851378838	88.0

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