open-sourcemultistagestochasticoptimization withr-long ...modern portfolio theory...

140
Open-Source Multistage Stochastic Optimization with R - Long-term Financial Management Ronald Hochreiter Winter School on Stochastic Programming 2017

Upload: others

Post on 14-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Open-Source Multistage Stochastic Optimizationwith R - Long-term Financial Management

Ronald Hochreiter

Winter School on Stochastic Programming 2017

Page 2: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Introduction

Giorgio Consigli will talk about a complete multi-stage stochasticoptimization model in Finance, so this talk focus on generalapproaches to Open-Source Multistage StochasticOptimization with R, i.e.

I Scenario Computation: Generation, Reduction, SelectionI Multi-stage Modeling & Scenario Tree Computation

In Finance data (i.e. scenarios and scenario trees) is much moreimportant than the optimization model, which is oftenstraightforward.

Page 3: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

What is great about stochastic optimization

I Tradition in the field of OR.I Great and lovely people.I Great conferences.

Page 4: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

What is bad about stochastic optimization

I Almost no open-source solutions available.I Many (if not all) papers are complete non-reproducible.I Most PhD students reinvent the wheel (not by choice).I No one uses R.

Page 5: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

R

I Software for Statistical Computing & Data ScienceI http://www.r-project.org/I Example of successful open-source softwareI Maturity and stabilityI R Ecosystem: R Core and R PackagesI RStudio http://www.rstudio.com/ and ShinyI R scientists have reproducibility in mindI Probably the only reason not to use R: Optimization

Page 6: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Optimization Modeling & R

Facts about OR/Optimization and R:

I Statisticians love to build optimization models matrix-wise.I CRAN (Optimization Task View) is focussed on solvers.I Optimization under Uncertainty is not covered.I OR people use MatLab/Julia, Python, and/or C++.

Strategy: Simplify and do it - and implement as R packages.

Issue: It is impossible to design a one fits all product/package!

Solution: Split into simple, extensible, lightweight packages.

Page 7: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Optimization modeling gap

I The gap between Academia and the Real-World is all tooobvious.

I This gap is of the reasons why it is hard to sell MSP models tothe industry.

Page 8: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Optimization modeling gap - Academia

I Everything is done for one (or two) research paper(s).I Focus on one specific solution method or one specific solver.

I The model is created in a solver-readable matrix format.I Data and model is mixed (messed) up.I Need mathematical proofs to impress non-scientists.

I PhD students are forced to do the implementation.I If a deadline is missed: you send the project to the next

conference or special issue of a journal.

Page 9: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Optimization modeling gap - Real-World

I Data and model should be separated.I Different groups and persons are working on it.

I Simplifications are crucialI to communicate and maintain the model, and toI implement optimization results into your business process.

I No one cares about how it is solved, i.e.I dirty heuristics are ok, no proofs necessary.

I If a deadline is missed: you lose money, clients, reputation,your wife & life, and so on. . .

Common Issue: Optimization is considered to be a side-product(“someone has to do it”).

Page 10: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Benefits of getting older

Page 11: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Benefits of getting older

One of the main benefits of getting older is that you eventually endup finding a student or colleague who implements something youalways fantasized about!

Page 12: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Benefits of getting older

One of the main benefits of getting older is that you eventually endup finding a student or colleague who implements something youalways fantasized about!

Laura Vana Florian Schwendinger

Page 13: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Portfolio optimization modeling with ROML

Laura Vana, Florian Schwendinger, Ronald Hochreiter

Winter School on Stochastic Programming 2017

Page 14: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Portfolio optimization modeling

A simple and contemporary way to modeland solve complex portfolio optimizationproblems with R.

Page 15: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

R packages

I AML (algebraic modeling language) now available in R: ROML(R Optimization Modeling Language) package.

I Access to different solvers for various problem classes based onthe ROI (R Optimization Infrastructure) package (Hornik et al.,2016).

I Portfolio optimization modeling language is built on top of thegeneralized AML: ROML.portfolio package.

All available open-source on https://r-forge.r-project.org/

Page 16: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Modern Portfolio Theory

Calculate an optimal portfolio x out of a assets given a vector ofexpected returns M and a co-variance matrix C subject to furtherconstraints X , i.e. the well-known Markowitz approach:

minimize x x C xT

subject to x ×M ≥ µx ∈ X .

Issues with this approach:

I Uncertainty is just implicitly modeled → deterministic!I QP framework too rigid and specific (OR perspective).I General extensions (CVaR, . . . ) are put on top of this base.

Page 17: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Stochastic Programming - Uncertainty

Stochastic Programming naturally separates the objective andsubjective part of a decision problem.

1. Optimization model specifies the event space at eachdecision stage to integrate objective real-world constraints anddynamics, i.e. the event handling - quantitative aspects of thesolution.

2. Uncertainty model is chosen independently from optimizationmodel to reflect subjective beliefs of the decision taker -qualitative aspects of the solution.

Page 18: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Stochastic Portfolio Optimization

Objective and subjective parts within portfolio optimization:

I X - set of regulatory & organizational constraints.I S - asset return uncertainty model.

Flexible to integrate any risk measure (VaR, Omega, . . . ):

I `x - loss distribution for some portfolio x , i.e. `x =⟨x ,S

⟩.

(Bi-criteria) optimization meta-model:

maximize x Return(`x )minimize x Risk(`x )subject to x ∈ X

Page 19: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

ROML.portfolio - Syntax

Minimum variance portfolio:

m <- model()m$variable(portfolio, lb = 0)m$minimize(markowitz(portfolio))m$subject_to(budget_norm(portfolio))

Page 20: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

ROML.portfolio - Syntax

Changing the risk-measure to 95% CVaR?

m <- model()m$variable(portfolio, lb = 0)m$minimize(cvar(portfolio, 0.95))m$subject_to(budget_norm(portfolio))

Different solver required (linear instead of quadratic), but this ishandled by other packages, i.e. ROML and ROI.

Page 21: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Keywords - Objective and Constraints

I Extensive set of complex and creative risk and return-riskmeasures implemented, e.g.,

I rewardI markowitzI cvarI madI omegaI sharpeI downside_varI downside_madI minimax_young

I Different constraints: cardinality, turnover, . . .I All standard functionalities of the generalized AML.

Page 22: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Examplesinstall.packages("ROML.portfolio",

repos="http://R-Forge.R-project.org")install.packages("ROML",

repos="http://R-Forge.R-project.org")library(ROML); library(ROML.portfolio)data(djia2013)

m <- model()m$variable(portfolio, lb = -1) # portfolio choice vectorm$maximize(reward(portfolio))m$subject_to(cvar(portfolio, 0.95) <= 0.02)m$subject_to(cvar(portfolio, 0.99) <= 0.03)m$subject_to(portfolio[2] + portfolio[10] +

portfolio[20] <= 0.5)m$subject_to(turnover(portfolio) <= 0.5)solution <- optimize(m, solver = "glpk",

data = list(returns = djia2013))

Page 23: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Examples

m <- model()m$variable(portfolio, lb = 0)m$maximize(omega(portfolio))m$subject_to(cardinality(portfolio) <= 7)m$subject_to(cvar(portfolio, 0.95) <= 0.02)m$subject_to(markowitz(portfolio) <= 0.03^2)solution <- optimize(m, solver = "",

data = list(returns = djia2013))

Page 24: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Thank you

Email [email protected] http://www.hochreiter.net/ronald/More infos http://finance-r.com/portfolio/

Page 25: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Modeling Multi-stage Decision OptimizationProblems under Uncertainty

Ronald Hochreiter

Winter School on Stochastic Programming 2017

Page 26: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Project Overview

I Initiated in February 2005 during two weeks together withTeemu Pennanen in Helsinki.

I Rooted in multi-stage stochastic programming and dependingon scenario trees.

I Presented at ICSP 2007, CMS 2009, OR 2011, and CMS 2012.I Many iterations, many flaws ironed out, . . .I New in 2014: Meta-modeling - not bound to a specific

language (R).

Page 27: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Project Aim

I Avoiding the reinvention of the wheel.I Simplifying the learning process for PhD students entering the

field.I Building an extensive multi-stage model library.I Getting the industry to adopt real multi-stage models.

Design strategy

Simplify and do it!

Page 28: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Multi-stage optimization under uncertainty

Design goal: Modeling language independent of

I optimization modeling approach:I Expectation-based convex multi-stage stochastic programming,I Worst-case optimization,I . . .

I underlying solution technique:I Tree-based deterministic equivalent formulation.I Primal/dual linear decision rules, upper/lower bounds.I . . .

I programming language:I R, Python, Julia, MatLab, . . .

Page 29: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Solution Method - Scenario Tree

(1) Modeler View (Stage)

(2) Stochastic View (Tree)

Root Stage Recourse Stage Terminal Stage

(3) Data View (Node)

Scenario tree-based three-layered approach

Page 30: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Solution Method - Scenario Tree-free

(1) Modeler View (Stage)

(2) Stochastic View (Upper/Lower Approximation)

Root Stage Recourse Stage Terminal Stage

Scenario tree-free approximation

Decision rules, . . .

Page 31: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

A simple multi-stage model

Multi-stage stochastic programming example [Heitsch el al. 2006]:

I Compute optimal purchase over time under cost uncertainty.I Uncertain prices are given by Vt .I Decisions xt amounts to be purchased at each time period t.I Objective: minimize expected costs such that a prescribed

amount a is achieved at T .I State variable st : amount held at time t.

Page 32: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

A simple multi-stage model

Multi-stage stochastic programming example [Heitsch el al. 2006]:

minimize E(∑T

t=1 Vtxt)

subject to st − st−1 = xt ∀t = 2, . . . ,Ts1 = 0, sT = a,xt ≥ 0, st ≥ 0.

Page 33: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

The meta-model (based on AMPL syntax)

deterministic a: T;stochastic V, x, s, objective: 0..T;stochastic non_anticitpativity: 1..T;stochastic root_stage: 0;stochastic terminal_stage: T;

param a; param V;var x >= 0, s >= 0;

maximize objective: E(V * x);subject to non_anticitpativity: s - s(-1) = x;subject to root_stage: s = 0;subject to terminal_stage: s = a;

Page 34: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Model instance in R (modopt.multistage)

parameter(a); parameter(V);variable(x, lb=0); variable(s, lb=0)

maximize("objective", "E(V * x)")subject_to("non_anticitpativity", "s - s(-1) = x")subject_to("root_stage", "s = 0")subject_to("terminal_stage", "s = a")

deterministic("T", a)stochastic("0..T", V, x, s, "objective")stochastic("1..T", "non_anticipativity")stochastic("0", "root_stage")stochastic("T", "terminal_stage")

Page 35: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Meta-modeling language - stochastic additions

Based on AMPL with additional keywords for stochasticitydefinitions:

I deterministic object-list: stage-set;I stochastic object-list: stage-set;

In terms of scenario trees: stochastic parameters are defined on theunderlying tree node structure, and deterministic parameters aredefined on the stage structure, i.e. same value for all nodes in therespective stage.

I objective: expectation functional E(variable).I recourse definition: variable(recourse-depth).

Stage-sets can be overridden explicitly, e.g. E(wealth, T).

Page 36: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Meta-modeling language - further extensions

General extensions:

I objective: quantiles Q(variable, alpha).I objective/constraints: application-related risk measures

CVaR(variable, alpha),I probabilistic constraints P(variable <= alpha).

Worst case optimization:

I simplified description of uncertainty sets (ROME),I new objective function operator(s).

Page 37: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Stage-set parsing, node-set creation

I Node-sets Nt , include all tree nodes of stage t.I Add one stage-set for the whole horizon (0..T ).I Parse all stage-sets defined

I direcly with keywords stochastic and deterministic, andI within the objective functional E().

I For each stage-set - given one specific scenario tree - createappropriate node-sets containing all nodes of the respectivestages.

Page 38: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Multi-stage Scenario Tree Format

I No common standard for representing discretized stochasticprocesses available, mainly because of the lack of commercialinterest.

I To accommodate all different tree structures, and the implicitformulation of non-anticipativity constraints, a node-basedvector/matrix data format of scenario trees is proposed:

V (n, d) d-dimensional value of node nA(n) ancestor node of node nT (n) stage of node nP(n) probability to reach node n from its ancestor

Page 39: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Conversion example - Stage and node-sets

Example: Simple three-stage (t = 0, 1, 2) binary tree, (uni-variate)starting value: 10. Up 1 with p = 0.6 and down 1 with p = 0.4, i.e.n 0 1 2 3 4 5 6V[n] 10 11 9 12 10 10 8A[n] 0 0 1 1 2 2T[n] 1 1 2 2 2 2P[n] 1 0.6 0.4 0.6 0.4 0.6 0.4Z[n] 1 0.6 0.4 0.36 0.24 0.24 0.16

Node- and stage-sets: Using the above inventory example:Model Node-Set Stages Nodes(0..T) 0 0 1 2 0 1 2 3 4 5 60 1 0 0T 2 2 3 4 5 61..T 3 1 2 1 2 3 4 5 6

Page 40: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Conversion example - Variables and Parameters

I Add (reserved) scenario tree variables V, A, P, Z, T.I Replace stochastic parameters and variables by a node-set

definition, andI replace deterministic parameters and variables by stage-set

definitions.

deterministic a: T;stochastic V, x, s: 0..T;param a, V;var x >= 0, s >= 0;

param a[stageSet2], V[nodeSet0];var x[nodeSet0] >= 0, s[nodeSet0] >= 0;

Page 41: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Conversion example - Constraints

For each stochastic constraint add deterministic equivalentconstraints as nodes given the respective node set, i.e.

stochastic root_stage: 0;subject to root_stage: s = 0;

subject to root_stage{n in node_set1}: s[n] = 0;

Page 42: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Conversion example - Recourse constraints

Deterministic parameters in stochastic constraints make use of thestage mapping information T[n]:

stochastic terminal_stage: T;subject to terminal_stage: s = a;

subject to terminal_stage{n in node_set2}: s[n] = a[T[n]];

Page 43: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Conversion example - Recourse constraints

Recourse constraints make use of the anchestor information A[n].Higher depths are integrated recursively.

stochastic non_anticitpativity: 1..T;subject to non_anticitpativity: s - s(-1) = x;

subject to non_anticitpativity{n in node_set3}:s[n] - s[A[n]] = x[n];

Finally: No explicit tree formulation in the model anymore.

Page 44: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Conversion example - Objective function

Objective function replacements, i.e. adding tree information andbased replacing E() by sums using the stage probabilities Z[n]:

maximize objective_function: E(x, 0..T);

maximize objective_function:( sum{n in nodeSet0}: Z[n] * ( V[n] * x[n] ) );

Page 45: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

ALM example - Stage-based notation

maximizex E(wT ) + κ(γ − E( zT1−α))

subject to∑

a∈A xa ≤ β = w (t = 0)∀a ∈ A : xa ≤ Vax (−1)

a + ba − sa (t = 1, . . . ,T − 1)∑a∈A ba ≤

∑a∈A sa (t = 1, . . . ,T − 1)

w =∑

a∈A xa + f (t = 1, . . . ,T − 1)w =

∑a∈A Vax (−1)

a + f (t = T )z ≥ γ − w (t = T )

Page 46: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

ALM example - Meta-model (1)

param alpha; param beta; param kappa;param assets; set Asset := 1 .. assets;param V{Asset};

var x{Asset} >= 0, b{Asset} >= 0, s{Asset} >= 0, w >= 0;var f; var g; var z >= 0;

maximize objective: E(w)+ kappa * ( g - ( E(z / ( 1 - alpha )) ) );

subject to cvar: z >= g - w;subject to init_budget: ( sum{a in Asset} x[a] ) <= beta;subject to init_wealth: ( sum{a in Asset} x[a] ) == w;

Page 47: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

ALM example - Meta-model (1b)

param alpha; param beta; param kappa;param assets; set Asset := 1 .. assets;param V{Asset};

var x{Asset} >= 0, b{Asset} >= 0, s{Asset} >= 0;var w >= 0, f;

maximize objective: E(w) + kappa * CVaR(w, alpha);

subject to init_budget: ( sum{a in Asset} x[a] ) <= beta;subject to init_wealth: ( sum{a in Asset} x[a] ) == w;

Page 48: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

ALM example - Meta-model (2)

subject to trading{a in Asset}:x[a] <= ( V[a] * x(-1, a) ) + b[a] - s[a];

subject to buy_sell:( sum{a in Asset} b[a] ) <= ( sum{a in ASSET} s[a] );

subject to wealth: w <= ( sum{a in Asset} x[a] ) + f;subject to final_wealth:

w <= ( sum{a in Asset} V[a] * x(-1, a) ) + f;

Page 49: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

ALM example - Meta-model (3)

deterministic f: 1..T;

stochastic init_budget, init_wealth: 0;stochastic x, w: 0..T;stochastic b, s: 1..T-1;stochastic V, trading, buy_sell, wealth: 1..T;stochastic z, objective, cvar, final_wealth: T;

Page 50: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Thank you

Email [email protected] http://www.hochreiter.net/ronald/

Page 51: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Some remarks on Scenario Computation

Ronald Hochreiter

Winter School on Stochastic Programming 2017

Page 52: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Computation

Definitions often mixed up, so here is my take:

I Scenario Computation: Meta-level.I Scenario Generation: Generating a scenario structure out of

(mostly continuous) specification, e.g. build a tree directlyusing the definition of a stochastic process.

I Scenario Tree Generation: Generating a scenario tree structureout of sampled scenario paths.

I Scenario Reduction: Reduce the cardinality without modifyingthe structure of the scenario set, i.e. reducing a discreteprobablity distribution or the number of branches & nodes of ascenario tree.

I Scenario Selection: Directly pick a certain number of scenariosout of the original set.

Page 53: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical Example

Consider the following setup:

I Classical Markowitz Minimum Variance Portfolio OptimizationI All 30 Assets from the Dow Jones Industrial AverageI Data from the beginning of 2012 until the end of 2016I Compute weekly returns on a daily basis: 1253 scenariosI Asset means and Covariance matrix out of historical data

Page 54: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical Example

AAPL

KO

DDXOMIBM

JNJ

MCD

MRK

NKE

PFE

PG

UNH

VZ

WMT

Minimum Variance Portfolio

Page 55: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical Example

AAPL KO DD XOM IBM JNJ MCD MRK NKE PFE PG UNH VZ WMT

0.00

0.05

0.10

0.15

Minimum Variance Portfolio

Page 56: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical Example

Now check the effect of scenario generation, i.e. we sample out of amulti-variate normal distribution with the given mean vector andCovariance matrix.

I Our original data set had 1253 scenarios.I We sample from 100 to 900 with a stepsize of 100.

Page 57: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical Example

0.00

0184

0.00

0188

0.00

0192

0.00

0196

Sample Size

Obj

ectiv

e

100

200

300

400

500

600

700

800

900

Real objective value

Page 58: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical Example

Question: Is it appropriate to check the objective function only?Should this be considered as the only measure to indicate thequality of a scenario computation method?

Page 59: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical Example

Question: Is it appropriate to check the objective function only?Should this be considered as the only measure to indicate thequality of a scenario computation method?

I Some people check the distance of theapproximation/generation.

I However, most important: Decision

Page 60: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical Example

Asset Weight Difference (sample size n=100)

−0.

10−

0.05

0.00

0.05

0.10

Page 61: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical Example

Asset Weight Difference (sample size n=200)

−0.

10−

0.05

0.00

0.05

0.10

Page 62: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical Example

Asset Weight Difference (sample size n=300)

−0.

10−

0.05

0.00

0.05

0.10

Page 63: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical Example

Asset Weight Difference (sample size n=400)

−0.

10−

0.05

0.00

0.05

0.10

Page 64: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical Example

Asset Weight Difference (sample size n=500)

−0.

10−

0.05

0.00

0.05

0.10

Page 65: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical Example

Asset Weight Difference (sample size n=600)

−0.

10−

0.05

0.00

0.05

0.10

Page 66: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical Example

Asset Weight Difference (sample size n=700)

−0.

10−

0.05

0.00

0.05

0.10

Page 67: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical Example

Asset Weight Difference (sample size n=800)

−0.

10−

0.05

0.00

0.05

0.10

Page 68: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical Example

Asset Weight Difference (sample size n=900)

−0.

10−

0.05

0.00

0.05

0.10

Page 69: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Reduction

I Reduce the cardinality without modifying the structure.I This is what most people do: quite some pitfalls included!I Single-stage and two-stage case: Clustering, which comes in

two main flavors:I Partitional Clustering (k-Means, . . . )I Hierarchical Clustering

I Different distances possible.I Problem: It is a greedy strategy, so you never end up at a good

(probability) distance approximation.

Page 70: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Reduction - Open Source

Due to popular request Scenario Reduction has been added to theopen-source R package scenarios. Implemented algorithms:

I k-Means most people use thisI More sophisticated partitioning algorithmI Hierachical join

Available Distances:

I `1 / Wasserstein / . . .I `2 / Fortet Mourier of order 2 / . . .

Page 71: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Reduction - Numerical example

Consider the following setup:

I All 30 Assets from the Dow Jones Industrial AverageI Data from the beginning of 2012 until the end of 2016I Compute weekly returns on a daily basis: 1253 scenariosI Stochastic Portfolio Optimization using MAD and CVaR(0.05)

Page 72: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Reduction - Numerical example

We reduce the scenario set to sizes 100 to 900 with stepsize 100using two different algorithms:

I Partitioning (P), andI Hierarchical (H)

with the `1 (1) and `2 (2) distance.

Page 73: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Reduction - Numerical example

−0.

030

−0.

035

−0.

040

−0.

045

Reduction Size

CV

aR

H1H2P1P2

100

200

300

400

500

600

700

800

900

Objective - CVaR

Page 74: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Reduction - Numerical example

0.01

20.

013

0.01

40.

015

0.01

60.

017

0.01

8

Reduction Size

MA

D

H1H2P1P2

100

200

300

400

500

600

700

800

900

Objective - MAD

Page 75: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical ExampleAsset Weight Difference (sample size n=100)

−0.

10−

0.05

0.00

0.05

0.10

CVaR, `1, Partitional (left), Hierarchical (right)

Page 76: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical ExampleAsset Weight Difference (sample size n=200)

−0.

10−

0.05

0.00

0.05

0.10

CVaR, `1, Partitional (left), Hierarchical (right)

Page 77: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical ExampleAsset Weight Difference (sample size n=300)

−0.

10−

0.05

0.00

0.05

0.10

CVaR, `1, Partitional (left), Hierarchical (right)

Page 78: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical ExampleAsset Weight Difference (sample size n=400)

−0.

10−

0.05

0.00

0.05

0.10

CVaR, `1, Partitional (left), Hierarchical (right)

Page 79: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical ExampleAsset Weight Difference (sample size n=500)

−0.

10−

0.05

0.00

0.05

0.10

CVaR, `1, Partitional (left), Hierarchical (right)

Page 80: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical ExampleAsset Weight Difference (sample size n=600)

−0.

10−

0.05

0.00

0.05

0.10

CVaR, `1, Partitional (left), Hierarchical (right)

Page 81: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical ExampleAsset Weight Difference (sample size n=700)

−0.

10−

0.05

0.00

0.05

0.10

CVaR, `1, Partitional (left), Hierarchical (right)

Page 82: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical ExampleAsset Weight Difference (sample size n=800)

−0.

10−

0.05

0.00

0.05

0.10

CVaR, `1, Partitional (left), Hierarchical (right)

Page 83: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical ExampleAsset Weight Difference (sample size n=900)

−0.

10−

0.05

0.00

0.05

0.10

CVaR, `1, Partitional (left), Hierarchical (right)

Page 84: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical ExampleAsset Weight Difference (sample size n=100)

−0.

10−

0.05

0.00

0.05

0.10

MAD, `1, Partitional (left), Hierarchical (right)

Page 85: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical ExampleAsset Weight Difference (sample size n=200)

−0.

10−

0.05

0.00

0.05

0.10

MAD, `1, Partitional (left), Hierarchical (right)

Page 86: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical ExampleAsset Weight Difference (sample size n=300)

−0.

10−

0.05

0.00

0.05

0.10

MAD, `1, Partitional (left), Hierarchical (right)

Page 87: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical ExampleAsset Weight Difference (sample size n=400)

−0.

10−

0.05

0.00

0.05

0.10

MAD, `1, Partitional (left), Hierarchical (right)

Page 88: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical ExampleAsset Weight Difference (sample size n=500)

−0.

10−

0.05

0.00

0.05

0.10

MAD, `1, Partitional (left), Hierarchical (right)

Page 89: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical ExampleAsset Weight Difference (sample size n=600)

−0.

10−

0.05

0.00

0.05

0.10

MAD, `1, Partitional (left), Hierarchical (right)

Page 90: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical ExampleAsset Weight Difference (sample size n=700)

−0.

10−

0.05

0.00

0.05

0.10

MAD, `1, Partitional (left), Hierarchical (right)

Page 91: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical ExampleAsset Weight Difference (sample size n=800)

−0.

10−

0.05

0.00

0.05

0.10

MAD, `1, Partitional (left), Hierarchical (right)

Page 92: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Generation - Numerical ExampleAsset Weight Difference (sample size n=900)

−0.

10−

0.05

0.00

0.05

0.10

MAD, `1, Partitional (left), Hierarchical (right)

Page 93: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Selection

Page 94: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

(Open-Source) Multi-stage Scenario Generation

Ronald Hochreiter

Winter School on Stochastic Programming 2017

Page 95: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Issues

Overcomplication

In order to publish papers in research journals, multi-stage treegeneration methods are often presented in an utterly complicatedway, although the actual algorithms are often pretty simple andstraightforward.

Page 96: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Issues

Overcomplication

In order to publish papers in research journals, multi-stage treegeneration methods are often presented in an utterly complicatedway, although the actual algorithms are often pretty simple andstraightforward.

Available (open-source) code

Almost no open-source code is available and the binaries (ifavailable) are also hard to use and somewhat restricted (e.g. to acertain OS).

Page 97: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

IssuesOvercomplication

In order to publish papers in research journals, multi-stage treegeneration methods are often presented in an utterly complicatedway, although the actual algorithms are often pretty simple andstraightforward.

Available (open-source) code

Almost no open-source code is available and the binaries (ifavailable) are also hard to use and somewhat restricted (e.g. to acertain OS).

The reinvention of the wheelDue to the two previously mentioned issues - overcomplication andnon-availability of code - most researchers start everything fromscratch.

Page 98: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Issues

Heuristic tree-building

In case of multi-stage trees, the heuristic technique is moreimportant than the chosen distance!

Believe it or not

I Take Moment Matching (MM) and Wasserstein distance (WS)minimization, and two tree building methods, i.e.

I node-stage tree/approximation (NST/A) and scenariomerging/forward (SM/F).

I The NST/A-WS and NST/A-MM trees will be much closerthan the NST/A-MM and SM/F-MM trees!

Page 99: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Multi-stage Scenario Tree Generation

(1) Modeler View (Stage)

(2) Stochastic View (Tree)

Root Stage Recourse Stage Terminal Stage

(3) Data View (Node)

Scenario tree-based three-layered approach

Page 100: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Multi-stage Scenario Tree Generation

In general, there are two strategies, which can be used to design amulti-stage scenario tree generator:

I Direct scenario tree sampling. Based on historical data oreconometric model use sampling or node-wise approximationmethods to build the scenario tree iteratively from the rootnode to the terminal stage.

I Scenario path simulation and optimal treeapproximation. Use pre-sampled scenario paths to build anoptimal approximation of this data set. Different methods areavailable, which try to preserve the time-dependency of theunderlying process.

Page 101: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Tree Optimization - Distance Concept

Optimal approximations need to be based on some distance concept.The choice of this distance has different origins. It may be

I based on subjective taste, e.g. Moment Matching: Wallace etal., . . .

I selected due to theoretical (stability) considerations -probability metric minimization: Pflug et al., Römisch et al.,. . .

I predetermined by chosen approximation method, e.g. Sampling:(R)QMC - Pennanen et al., SAA - Shapiro et al., . . .

Note. Once the appropriate distance has been selected, theproblem of choosing the appropriate heuristic to approximate thechosen distance is prevalent.

Page 102: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Multi-stage Scenario Tree Structure

Different scenario tree methods allow for different scenario treestructures. In general, there are two different types possible:

I Nodes per stage. The scenario structure is represented by avector denoting the successors of each node in the respectivestage, e.g. N(5, 3, 2) results in a scenario tree with 30 scenariosand 1 + 5 + 15 + 30 = 51 nodes.

I Nodes at stage. The scenario structure is represented by avector containing the total number of nodes in each stage, e.g.S(10, 20, 30) results in a scenario tree with 30 scenarios and1 + 10 + 20 + 30 = 61 nodes.

The first is common for root-to-terminal iterative node-per-nodetechniques, while the second is used by generation methods, whichtry to find the optimal per-node branching structure in addition toapproximating tree values.

Page 103: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Multi-stage Scenario Tree Format

I No common standard for representing discretized stochasticprocesses available, mainly because of the lack of commercialinterest.

I To accommodate all different tree structures, and the implicitformulation of non-anticipativity constraints, a node-basedvector/matrix data format of scenario trees is used:

V (n, d) d-dimensional value of node nA(n) ancestor node of node nT (n) stage of node nP(n) probability to reach node n from its ancestor

Page 104: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Multi-stage Scenario Tree Generation

Node-based scenario generation:

I NST: Node (by Node)-Stage (by Stage) Tree.I NST/B: Bootstrapping.I NST/A: Approximation.I NST/P: Process estimation & sampling.

Scenario-based scenario generation:

I SM: Scenario Merging - forward (SM/F) and backward(SM/B).

I SC/F: Forward-based Scenario Clustering.I FL/B: Backward-based multi-dimensional Facility Location

Tree Generation.

Page 105: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

NST: Node (by Node)-Stage (by Stage) Tree

Page 106: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

NST/B - Node-Stage Tree/Bootstrapping

Input: Historical return time-series S of length `, stage-skip size k,node-based tree structure N(n1, . . . , nT ).

For each stage t = 1, ..., T:For each node in stage t:

Sample random n(t) values fromS(t*k : l) (& all predecessor values)

I Advantage: Simple, for highly multi-variate structures thehistorical dependence can be kept cheaply.

I Disadvantage:I Trees might differ significantly in the univariate case.I NST/B cannot be used for a huge amount of stages, if the

time-series are not long enough.

Page 107: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

NST/B - Node-Stage Tree/BootstrappingBootstrapping problem with small trees for IBM prices, N(3,3),k = 20:

Page 108: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

NST/A - Node-Stage Tree/Approximation

Input: Historical return time-series S of length `, stage-skip size k,node-based tree structure N(n1, . . . , nT ).

For each stage t = 1, ..., T:For each node in stage t:

Calculate an optimal single-stage approximation ofn(t) values from S(t*k : l) (& all predecessor values)

I Advantage: For highly multi-variate structures the historicaldependence might be kept - depending on the approximation.

I Disadvantage: Cannot be used for a huge amount of stages, ifthe time-series are not long enough, needs an appropriateapproximation method (distance and heuristic).

Page 109: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

NST/A - Node-Stage Tree/Approximation

I Approximation: K-Means.I N(3,3) and N(10,10), k = 30.

Page 110: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

NST/P - Node-Stage Tree/Process

Input: Process, estimated parameters, node-based tree structureN(n1, . . . , nT ).

Example: A univariate process ξ might be normally distributed andfollows an additive recursion, i.e.

ξ0 = µ0, ξ1 ∼ N(µ0, σ20), ξt = bξt−1 + εt , εt ∼ N(µ, σ2),

whereµ = µ0(1 − b), σ = σ0(1 − b2),

and ξt and εt are independent. ξ is a stationary Gaussian Markovprocess.

I Advantage: Simple, flexible (choice of process).I Disadvantage: Loss of time dependency, generally unfavorable

convergence behavior.

Page 111: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario-based scenario generation

Estimating stock returns, sampling 200 paths (length 100):

I Econometric time-series models ((V)AR(I)MA, (G)ARCH, . . . )I Specialized models (e.g. Wilkie model for actuarial use)I Custom (company-specific) scenario generators

Page 112: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Merging - forward and backward

Input: Set of simulated paths, stage-based tree structureS(s1, . . . , sT ), one specific distance function.

Calculate distance of each scenario to each other with chosen distance.for t = 1, ..., T (forward) or t = T, ..., 1(backward)

while there are more nodes than $s_t$Merge two scenarios closest to each other.

I Advantage: Rather straightforward, quite efficient and flexible(distance calculation only once at the beginning), sameprocedure for forward and backward generation.

I Disadvantage: Distance calculation can be computationallyexpensive.

Page 113: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Merging - forward and backward

Simple example: 4 scenarios - build S(2, 3, 4) tree.

Page 114: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Merging - forward and backward

Simple example: 4 scenarios - build S(2, 3, 4) tree.

Page 115: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Merging - forward and backward

Simple example: 4 scenarios - build S(2, 3, 4) tree.

Page 116: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Merging - F (black) and B (red)

Using time-series simulations, building a S(10, 20, 30) tree:

Page 117: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Merging - F (black) and B (red)

Using time-series simulations, building a S(10, 20, 30, 40, 50) tree:

Page 118: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

SC/F - Forward-based Scenario Clustering

Advantage:

I Rather fast computation (approximation problems are small -only the first stage approximation might take some time).

I Can be used for scenario trees with a large amount of stages(more than 20, up to 500).

Disadvantage:

I Thinning out: at some stage, there might occur non-branchingscenarios.

I Implementation is quite tricky.

Page 119: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

SC/F - Forward-based Scenario Clustering

Step One: Simulate trajectories.

Page 120: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

SC/F - Forward-based Scenario Clustering

Step Two: Calculate approximation of first stage.

Page 121: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

SC/F - Forward-based Scenario Clustering

Step Three: For subsequent stages, use clustered paths only.

Page 122: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

SC/F - Forward-based Scenario Clustering

Step Four: Iterative stage-wise clustering.

Page 123: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

FL/B: Multi-dimensional Facility Location

Advantage:

I The first approximation needs to be done by an optimal facilitylocation approximation of dimension T × d .

I Very good convergence results.

Disadvantage:

I Underlying concept of facility location might be considered asan overkill.

I Choice of distance and approximation heuristic.I Implementation is quite tricky.

Page 124: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Simulated paths and facility location view (for 2 stages)

Important decision: point in time of discrete stages, i.e. in thisexample:

I Simulated daily future values: January - April.I Two Stages: February, 1st and April, 1st.

Page 125: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Simulated paths and facility location view (for 2 stages)

Page 126: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Simulated paths and facility location view (for 2 stages)

Input: 400 Scenarios. Requested Output: 1-12-40 scenario tree.

Page 127: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Facility location - Step One: Clustering

Page 128: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Facility location - Step Two: Projection

Page 129: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Facility location - Step Two: Projection

Page 130: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Facility location - Step Three: Clustering

Page 131: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Facility location - Step Four: Tree buildup

Page 132: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Facility location - Final scenario tree

Final scenario tree in

Facility location view Scenario tree view

Page 133: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Facility location - Each stage adds a dimension

I Simulated daily future values: January - April.I Three Stages: February 1st, March 1st, and April 1st.

Page 134: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Open-Source Implementation

Open Source R Package for multi-stage stochastic programs:

I Learning environment.I Prototype testing environment.I Open-source, free.

Not designed for production.

I Reducing scenario generation to the bare minimum.I Provide clear, extensible code.

Page 135: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Open-Source Implementation

It’s implemented in R, but MatLab users rejoice:

I matlab.read.treeI matlab.write.tree

Furthermore, R can be used in batch mode on the shell, i.e. easilyintegrated into other workflows. However:

I using a completely open-source alternative makes sense.I working with S3 scenario (tree) classes is very convenient.

Page 136: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Open-Source Implementation

1. Install R - http://www.r-project.org/2. Install RStudio IDE - http://www.rstudio.com/

On the R shell, simply type

install.packages("devtools")library(devtools)install_github("scenarios", "rhochreiter")

and you are ready to build multi-stage scenario trees.

Page 137: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Easy tree generation and plotting

tree1 <- nst.p_inventory(c(5, 3, 2), 4, 0.6, 0.8)plot(tree1)

3.0

3.5

4.0

4.5

5.0

Stage

Val

ue

0 1 2 3

Page 138: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Easy tree generation and plotting

tree2 <- nst.p_inventory(c(10, 5, 5), 4, 0.6, 0.8)plot(tree2)

2.5

3.0

3.5

4.0

4.5

5.0

Stage

Val

ue

0 1 2 3

Page 139: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Scenario Simulation

b <- block.bootstrap(returns, 30, 20)plot(return2price(b, as.numeric(Ad(series[1]))))

0 200 600 1000

800

1000

1200

1400

1600

Index

His

toric

al V

alue

s

800

1000

1200

1400

1600

Index

Sim

ulat

ed V

alue

s

0 4 8 13 18 23 28

Page 140: Open-SourceMultistageStochasticOptimization withR-Long ...Modern Portfolio Theory Calculateanoptimalportfoliox outofa assetsgivenavectorof expectedreturnsM andaco-variancematrixC subjecttofurther

Thank you

Email [email protected] http://www.hochreiter.net/ronald/Web http://www.github.com/rhochreiter/scenarios/