finance final report
DESCRIPTION
Final report of a project in mathematical finance dealing with an algorithmic trading program under market regime switching.Useful for undergraduate students in finance (incl. financial engineering) and economics.Own work.TRANSCRIPT
-
PORTFOLIO OPTIMIZATION UNDER PHASE LOCKING MIE479 CAPSTONE PROJECT
Ivan Peng (996824842) Jennifer Wong (996831772) Song Yang (996808085)
Submitted to Professor Roy Kwon,
Department of Mechanical and Industrial Engineering,
Faculty of Applied Science and Engineering,
University of Toronto
7 December 2012
-
ABSTRACT
We develop a variance-based regime switching mean-variance
optimization model, which captures phase locking effects in the
market. Models were developed for two-, three-, four-, and five-
regime implementations in MATLAB. Comparison of the results
indicated that three-regime models were optimal for variance-based
MVO. The three-regime model provided sufficient granularity to
capture market changes accurately, without having additional
redundant states or being computationally laborious.
-
1 BACKGROUND & MOTIVATION
Portfolio design is a fundamental problem in finance. One of the most widely used optimization strategies is
Markowitzs mean-variance quadratic optimization model, commonly abbreviated MVO. The MVO
formulation is based on numerous assumptions, one of which is that asset correlations remain fixed over
time in a single state. This assumption, however, often breaks down in the real world, since asset
correlations can change in a phenomenon known as phase locking.
One example of when phase locking can occur is during a financial crisis. During such times, the majority of
market assets tend to decrease in value, and assets which were previously uncorrelated suddenly become
positively correlated. MVO does not account for this change, and fails to recognize the unexpected change
in the market state. The model fails to protect investors during times of increased risk, leading to the
possibility of great financial loss.
Phase locking can be visualized using the concept of regime switching. Newmann, in 1997, defined regime
switching as a situation in which stock market returns are drawn from two different distributions, with
some well-defined stochastic process determining the likelihood that each return is drawn from a given
distribution. Regime switching can be modeled using Markov Chains, an example of which is presented in
Figure 1. In this example, the market is modeled as having three
distinct regimes, or states, representing a bull market, bear
market, and recession. Probabilities of moving from some state x
at time t to some state y at time t+1 are shown. These are known
as the transition probabilities.
Two questions naturally arise when considering this model:
1. How are the transition probabilities determined?
2. How many regimes (states) should be used in the
model? Does there exist an optimal, or ideal
number of states?
2 LITERATURE SURVEY Given its potentially catastrophic consequences, a great deal of
effort has been directed towards incorporating phase locking into
portfolio optimization models. The earliest researchers in the field
were Goldfeld & Quandt (1973) and Hamilton(1989).
As previously indicated, one fundamental factor to consider is the number of regimes used in the model.
Hamilton (1989) proposed a maximum likelihood estimation algorithm, with which he modeled two
distinct regimes - stable vs. volatile inflation/interest rates. Harris (2000) utilized a Bayesian Markov-chain
Figure 1 - schematic representation of a three-regime model
-
Monte Carlo estimation method, which also resulted in two regimes - a recessive state and a growth state.
Lo (2010) also interpreted the market as having two regimes, one during a stable economy, the other
during market catastrophe.
Billio, Getmansky, and Pelizzon (2006) proposed a model with three regimes, corresponding to normal
market, bear market, and bull market. Furthermore, they applied the phase locking framework to a multi-
factor model with arbitrage pricing theory, as opposed to MVO. This strategy of using factor models instead
of MVO was also adapted by Lo (2010). Ma et al. (2011) also concluded that three was the optimal number
of regimes, using the Bayesian information criterion.
Selecting and using meaningful data is another key concern. Hamilton (1989) observed regime changes in
the US gross national product, and subsequently identified two distinct regimes. Harris (2000) also
identified two regimes using Australian financial market data. Billio, Getmansky, and Pelizzon (2006)
identified three regimes based on S&P 500 returns. Similarly, Ma et al. (2011) also identified three regimes
utilizing a portfolio of nine sector exchange-traded funds in the US. Ang and Bekaert (2002) took a more
international approach and conducted tests using assets from the UK, US, and Germany.
Ang and Bekaert (2002) were also the first to include in their implementation the transaction costs of
rebalancing a portfolio between regime changes. Previous implementations had always assumed little or
no transaction costs. They found that it was not always beneficial to rebalance asset weights in a portfolio
after a regime change.
3 OBJECTIVE In this paper, we aim to identify the optimal number of states for a volatility-based regime-switching
model. From previous literature review, Ma et al. (2011) have concluded using the Bayesian Information
Criterion (BIC) that the optimal number of regimes is 3. Given the lack of restriction in their dataset, and
the small range of states, we will go about confirming that this is indeed the optimal number of states by
experiment, allowing us to deduce a generalized statement of market behavior.
4 OUR MODEL & THE IMPLEMENTATION THEREOF To retrieve as real data as possible, an SNP500-like data set was obtained. 74 stocks from all the important
sectors across the market were obtained, ranging from Basic Goods, to Financial and Technology sectors.
The daily prices for each of these stocks were pulled from Yahoo/Google Finance, the log returns were
generated to normalize. To see a list of our stocks in ticker, please see stockdataexec.m.
Model Formulation
Given the set of daily log returns, we seek to express this as simply as a mean centered around some noise.
Thus:
[1]
-
The subscript St denotes the mean and noise for each state St = 1..k. These states are unobservable, which
qualifies it as a Hidden Markov Model, but we can determine the probability of a certain observed return
residing in a certain state at any given point in time. At each time step, we have the probability of the return
jumping to another state modeled after a Markov Chain, which is a memory-less and only depend on its
previous state. We call this the probability transition matrix, which is formally defined as:
[ | ] [2]
To optimize the parameters, we use the likelihood function of observing the y = (y1, y2, yt). This is
expressed as:
| | | [3]
where f(y1|) is the pdf of y1, usually a normal distribution. We seek to maximize this parameter. Because
this is difficult to work with, we take the log of the function, as the log function still maintains convexity and
allows for easier optimization. Adding the separation for Markov states into the function, we seek to
maximize:
( ) ( [ ] | )
[4]
where [ ] is the unconditional probability for being in state j. For k=2, the unknown parameters
are defined as = (p11, p22, 1, 2, 1, 2).
Following Hamilton and Susmel (1994), we can simplify in this recursive manner:
| [ | ] [ | ] | [5]
where [ | ] is the probability transition matrix, and | is the pdf of yt. We rearrange and
calculate recursively:
[ | ] | |
|
[6]
at t =1, this becomes:
[ | ] |
|
|
| [7]
This is the Hamilton filtered probability, which describes the probability of being in a certain state at a
certain time t, given the observed data and parameters. However, this is a filtered probability and only
provides a snapshot at a certain time. We smooth out the probability, as the filtered probabilities may
return a variable which may not have indicated a jump in state. To do so, we condition each probability
over all time. Even though an efficient method has been established in the Viterbi Algorithm, this
computation is still intensive, so an approximation of full sampling inferences. We condition the filtered
-
probability at time t in either direction of some range r, in this case T/10. The approximation for smoothed
probabilities follows:
[ |
[ |
[8]
Upon determination of the smoothed probabilities, we want to implement mean-variance optimization
(MVO) to determine optimal portfolio holdings. We set an arbitrary threshold probability which will group
the returns into different states, and then run MVO on each individual state. This threshold is set to 0.8, and
any states probability above that at a certain point in time will be categorized into that state. Any
probability that is not any state is denoted as a transition state and is discarded.
Implementation in MATLAB
Following is a sample describing the code, setting up the main parameters and calling the functional code
for optimization.
There are 5 variables that set the optimization: dep, indep, k, S, and advOpt. The dep variable is simply the
observed log return variable generated from stock data. The indep variable is the indicator function for
each data point which goes into creating the log likelihood function. k denotes the number of states. S is a
matrix denoting which variables are to be optimized. In our case of S, we are assigning a one-factor model
indicating that the mean and noise both are unknown and not fixed. The advanced options set the noise to
follow an i.i.d. Normal distribution. Upon setting all these parameters, the MS_Regress_Fit function calls the
fminsearch function in MATLAB and we observe the smoothed probabilities. This is run for different k for
us for analysis.
Upon reaching k=4, the probability transition matrix was noticed to switch between multiple states. To
restrict the model, we force the probability transition matrix to a matrix that we want and then
run again. The probability matrix is set to [
] and the constrained optimization
function, fmincon, is used in MATLAB. The code to set this up is shown in the following figure:
dep=r5; constVec=ones(length(dep),1); indep=constVec; if (exist('k') == 0) k = 3; end S=[1 1]; advOpt.distrib='Normal'; advOpt.std_method=1;
[Spec_Out]=MS_Regress_Fit(dep,indep,k,S,advOpt);
Figure 2: Code which sets up the optimization process.
-
The complementary parameters in the advOpt cell describe the variable, which we will optimize. Running
the MVO script is only done on the 3-state model. The splitting of states is done by the following code:
The weights and variances are then used to plot the efficient frontiers for each state.
One more aspect of analysis is the performance and behavior of different sectors compared to the overall
average. We want to detect how and where each sector has a change in regime, over the average. This will
allow the investor to more precision in balancing their portfolios. The code to implement such is as
outlines:
dep=r5;
constVec=ones(length(dep),1); indep=constVec; k=4; S=[1 1]; advOpt.distrib='Normal'; advOpt.std_method=1; advOpt.optimizer='fmincon'; advOpt.constCoeff.nS_Param{1} = {'e'}; for i=1:length(indep) advOpt.constCoeff.S_Param{i} = {'e','e','e','e'}; end advOpt.constCoeff.covMat{1}(1,1) ={'e'}; advOpt.constCoeff.covMat{2}(1,1) ={'e'}; advOpt.constCoeff.covMat{3}(1,1) ={'e'}; advOpt.constCoeff.covMat{4}(1,1) ={'e'}; advOpt.constCoeff.p={0.97,0.03,0.01,0; 0.01,0.96,0.02,0.01;
0.01,0.01,0.95,0.04; 0.01,0,0.02,0.95};
[Spec_Out]=MS_Regress_Fit(dep,indep,k,S,advOpt);
Figure 3: An example code for constrained optimization
[PrS, mu1 mu2 mu3 cov1 cov2 cov3 ] = plotState(sp,r');
[w v ER] = runMVO(cov,mu); [w1 v1 ER1] = runMVO(cov1,mu1); [w2 v2 ER2] = runMVO(cov2,mu2); [w3 v3 ER3] = runMVO(cov3,mu3);
Figure 4: Code implementation for MVO for a 3-state model
-
To run this code, unzip the package MIE479_PengWongYang_Code.zip to a specified folder to open up in
MATLAB. Then, run master_master.m. This will execute all the individual components used in our analysis
of our project implementation. To view each component separately, step through the master_master.m
script. The total run time is around 4.5 hours on a standard laptop.
5 RESULTS & DISCUSSION
We define an asset to be in state i when the smoothed probability for state i is greater than our chosen
threshold value of 80%. For periods when no smoothed probabilities are above the threshold value, they
are grouped under the transition state.
for i=1:length(r2) figure; dep=r2(:,i); constVec=ones(length(dep),1); indep=constVec; k=3; S=[1 1]; advOpt.distrib='Normal'; advOpt.std_method=1;
[Spec_Out]=MS_Regress_Fit(dep,indep,k,S,advOpt); smoothProb_3(:,i) = Spec_Out.smoothProb(:,3); end
Figure 5: The main body of implementation for sector analysis.
Figure 6 - smoothed probabilities for two-regime model
-
We begin with the simplest model, a two-regime implementation where assets are classified by volatility.
Figure 6 shows a two-regime model where Regime 1 represents a stable market (blue) and Regime 2
represents a volatile market (red/green). The returns are in Regime 1 (stable market) with probability
0.7221, in Regime 2 (volatile market) with probability 0.1795, and in the transition state with probability
0.0984. We expect the transition state probability to increase for 3-regime and 4-regime models because as
the number of regime increases, the amount of time spent switching between regimes also increases.
Upon completion, we determine the optimal portfolio holdings of the stocks in each state. The efficient
frontier is mapped out with a standard MVO optimization problem, minimizing variance with an expected
goal return in mind. The efficient frontiers are plotted out below. Most of the efficient frontiers in the
higher volatility states are in the negative, and so in such a case, we would switch to a risk-free asset to
hedge ourselves from loss.
Figure 7 - asset returns for two-regime model
Figure 8 - asset returns for three-regime model
-
The procedure of using smoothed probabilities to separate returns by states was repeated for 3-regime, 4-
regime, and 5-regime models. The 3-regime model also produced logical results where states can be
distinctly identified as stable, moderately volatile, and highly volatile, as shown in Figure 10. However, for
the 4-regime model, the smoothed probabilities in each state exhibits unusual behaviour:
Figure 10 - asset returns for three-regime model
Figure 9 Efficient Frontiers for an MVO with state separation. All states that are not the stable state are in the negative for returns.
-
The graph shows many solid areas in red, which were not present previously in models with fewer regimes.
Upon closer examination, the red areas actually represent very frequent jumps between Regimes 2 and 3
(in green and red respectively). This is not a meaningful result, as assets should not switch that frequently
between states. It is also not clear what the difference between Regimes 2 and 3 is (Figure 12), since
volatility is similar for both states.
Furthermore, the probability of being in Regime 4 is very low, at 0.0621. This is undesirable, as we want to
capture the optimal number of regimes with reasonable transitional probabilities. Therefore, we propose
that we have exceeded the optimal number of regimes and that a four-regime model contains redundant
states.
Further investigation in the 4-regime model shows that the reason for states 2 and 3 alternating frequently
is in the transition probabilities matrix:
Figure 11 - smoothed probabilities for four-regime model
Figure 12 asset returns for four-regime model
-
Transition Probabilities Matrix for the four-regime model: [
]
The probabilities of remaining in Regime 2 or remaining in Regime 3 are both 0.00, while the probability of
entering Regime 3 from 2 and vice versa are 0.95 and 1.00 respectively. This causes the two states to
alternate frequently. We attempt to overcome this by implementing a restricted four-regime model,
where we force p23 = p32 = 0. The restricted 4-regime model has more balanced probabilities of being in
each state compared to the unrestricted case, but still appears to spend significantly more time in the
transition state, which we have determined is over-qualifying the data.
To further demonstrate that we have exceed the optimal number of regimes, smoothed probabilities were
plotted for a five-regime model, and it was concluded that there are 2 redundant states: one in the green
regions where it alternates frequently between states 2 and 3, and another in the magenta regions where it
alternates frequently between states 4 and 5 (Figure 14).
Our observed results are summarized in the table below:
Figure 13 - smoothed probabilities for four-regime model
Figure 14 - smoothed probabilities for five-regime model
-
Probability of
being in State
i=1,2,3,4
2-regime
model
3-regime model 4-regime
model
(Unrestricted)
4-regime
model
(Restricted)
State 1 72.21% 47.17% 44.78% 40.78%
State 2 17.95% 32.85% 11.32% 26.55%
State 3 - 6.75% 11.60% 8.90%
State 4 - - 6.21% 9.39%
Transition State 9.84% 13.23% 26.09% 14.38%
Table 1. The probabilities of being in each state for the 2-regime, 3-regime, and 4-regime models (unrestricted and restricted)
Due to the nature of our setup of the model, we have noticed the returned optimal states are very volatility-
dominated: the mean returns for all states are very close to 0 and not very different, but the volatility is
distinctly increasing in each state. The means may be centered around 0 because of our choice in using
daily log returns as opposed to weekly log returns. Regardless, we have noticed that this distinctly
captures recessional periods of time very well, which was our original goal in setting defining the objective
in our interim report. The highest volatility states consistently line up with our data set in the dotcom
bubble crash in 2001 and the subprime mortgage crisis of 2008. These differing states of volatilities
indicate to the investor that during these times, returns will be most likely be negative, as demonstrated in
the MVO. In this case, we would most likely change our portfolio and mostly invest in the risk-free rate
before switching back when we notice that this in the stable regime.
Another aspect analyzed is the performance of individual sectors. While choosing our data, we sought to
group each stock by its sector, and analyzed its stock performance. Here is an example of how the
Technology sector did compared to the Average:
-
Figure 15: Comparison of most volatile states between average and Technology Sector
From this, we observe that the technology sector has a likelihood of spending a lot less time in the most
volatile state as compared to the overall portfolio. Therefore, it appears a lot less likely that this sector is
the guilty culprit in forcing the regime switches in times of recession. However, this is all we can say about
this graph; our original goal was to hopefully more adequately observe the dot-com bubble, but we cannot
conclude anything around that time from this graph.
Our results indicated that a three-regime model was ideal for volatility-based regimes. Compared to the
two-regime model, the three-regime model was able to provide greater detail in differentiating between
various market conditions. This increased granularity, or resolution, makes the model more sensitive to
volatility changes which might have otherwise been ignored by the two-regime model. On the other hand,
we found that four- and five- regime models provided too much detail, resulting in unnecessarily frequent
regime switches and a number of redundant regimes. Implementations of models with many regimes also
led to an increase in computational complexity.
The three-regime model we implemented remains to be improved in many ways. In our current
implementation, the returns are represented in a very nave manner, with a constant mean in addition to a
noise term. This can be improved using ARCH (Equation 9), the autoregressive conditional
heteroskedasticity model, or the GARCH (Equation 10), the generalized version.
[9]
-
[10]
Another possible improvement on this model is to incorporate the transaction costs into our model.
Previous work with transaction costs in this subject has simply been a calculation of how to rebalance; we
seek to add it in as a constraint when determining the probability of switching. When implemented as such,
there is a certain cost function which penalizes the investor for switching. Therefore, the switching
between states should decrease dramatically, and a switch would only occur if the realized returns were
sure to overcome the transaction costs. This avenue of research has been largely unexplored from our
observations in our literature review. A critical result of adding transaction costs is the necessity of being
able to predict how long the market will remain in a certain state. For example, a portfolio rebalance during
a regime change may be warranted if the new regime is expect to endure, but a rebalance would be
meaningless if the new regime is temporary.
6 CONCLUSION Creating a portfolio optimization model which captures phase locking effects is of critical importance. In
order to do so, changing asset correlations, or regime switching, can be modeled as a Markov Chain, where
each state represents a regime, with corresponding transition probabilities. Through our implementation
of numerous volatility-based regime switching models, each using a different number of states, we were
able to conclude that three is the optimal quantity of regimes. A three-regime implementation maintains
high model resolution (i.e. being more sensitive to changes in volatility) without leading to redundant
states or excessive computational complexity.
-
ACKNOWLEDGEMENTS
We would like to thank Professor Roy Kwon for his valuable insights and guidance during the design,
implementation, and evaluation of our model.
-
REFERENCES G. Harris. Regime Switching Vector Autoregressions: A Bayesian Markov Chain Monte Carlo Approach. Australian
Mutual Provident Society, Sydney, NSW, 2000.
A. Lo. Hedge funds. Princeton, NJ: Princeton University Press, 2010, pp. 18-22, 211-212.
A. Ang and G. Bekaert. International asset allocation with Regime Shifts. The Review of Financial Studies, vol. 15, no.
4, pp. 1137-1187, 2002.
A. Ang and A. Timmerman. Regime Changes and Financial Markets, NEBR Working Paper No. 17182, pp. 1, 2011.
http://www.nber.org/papers/w17182.pdf?new_window=1
J. D. Hamilton. A New Approach to the Economic Analysis of Nonstationary Time Series and the Business Cycle.
Econometrica, vol. 57, no. 2, pp. 357-384, Mar. 1989.
M. Billio, M. Getmansky, L. Pelizzon. Phase-Locking and Switching Volatility in Hedge Funds. Internet:
http://www.unive.it/media/allegato/DIP/Economia/Working_papers/Working_papers_2006/WP_DSE_billio_getman
sky_pelizzon_54_06.pdf, Nov. 2006. [Sept. 28, 2012]
Timmermann, A. Moments of Markov Switching Models. Journal of Econometrics, 96, 75-111, 2000.
http://elmu.umm.ac.id/file.php/1/jurnal/J-a/Journal%20of%20Econometrics/Vol96.Issue1.May2000/
2094.pdf
Y. Ma, L. MacLean, K. Xu, Y. Zhao. A Portfolio Optimization Model with Regime-Switching Risk Factors for Sector
Exchange Traded Funds. University of Dalhousie, Halifax, NS, Mar. 2011.
http://www.unive.it/media/allegato/DIP/Economia/Working_papers/Working_papers_2006/WP_DSE_billio_getmansky_pelizzon_54_06.pdfhttp://www.unive.it/media/allegato/DIP/Economia/Working_papers/Working_papers_2006/WP_DSE_billio_getmansky_pelizzon_54_06.pdf -
Appendix: MATLAB Code master_master.m
% This is the master script to run all of our results. This will call the % maximum likelihood estimator for all our simulations conducted for our % graph in our presentation.
% Note to Prof Kwon: This whole process takes a long time to run, as it is % all our simulations, so run at your discretion! We have attached our % figures and results in this package.
clear; close all; R = importdata('Data2_daily.mat'); sec = importdata('Data_Sectors.mat');
close all; % Run for 2 states. k = 2; master2;
% Now run for 3 states. We will run MVO for this state as well. k = 3; master2; figure plotMVO;
% 4 states k = 4; master2;
% 5 states k = 5; master2;
% Constrained optimization for 4 states % Set prob transition matrix to known; then optimize master3;
% Do an analysis of each sector and look at their smoothed probabilities. sector_smooth_prob;
master2.m
% This script executes a simple 2-state markov model to test the % probability transitioning. % This assumes R and sec have been generated already % Generate the returns, means, and covariances of the R matrix tic [r mu cov] = getRetCov(R);
% Transpose it to have columns to denote each sector return, and rows to % denote time.
-
r5 = mean(r); r5 = r5';
% Plot smooth probabilities for all sectors dep=r5;
constVec=ones(length(dep),1); indep=constVec; k=3;
S=[1 1];
advOpt.distrib='Normal'
advOpt.std_method=1; [Spec_Out]=MS_Regress_Fit(dep,indep,k,S,advOpt);
sp = Spec_Out.smoothProb; time = toc;
master3.m
% This script executes a simple 2-state markov model to test the % probability transitioning. % This assumes R and sec have been generated already % Generate the returns, means, and covariances of the R matrix tic [r mu cov] = getRetCov(R); r2 = SecRet(r,sec);
% Transpose it to have columns to denote each sector return, and rows to % denote time. r2 = r2'; r5 = mean(r2,2);
% Plot smooth probabilities for all sectors
dep=r5; constVec=ones(length(dep),1); indep=constVec; k=4; S=[1 1];
advOpt.distrib='Normal'; advOpt.std_method=1; advOpt.optimizer='fmincon'; advOpt.constCoeff.nS_Param{1} = {'e'}; %advOpt.constCoeff.S_Param{1} = {'e','e'}; for i=1:length(indep) advOpt.constCoeff.S_Param{i} = {'e','e','e','e'}; end advOpt.constCoeff.covMat{1}(1,1) ={'e'}; advOpt.constCoeff.covMat{2}(1,1) ={'e'}; advOpt.constCoeff.covMat{3}(1,1) ={'e'}; advOpt.constCoeff.covMat{4}(1,1) ={'e'}; advOpt.constCoeff.p={0.97,0.03,0.01,0; 0.01,0.96,0.02,0.01; 0.01,0.01,0.95,0.04;
0.01,0,0.02,0.95};
-
[Spec_Out]=MS_Regress_Fit(dep,indep,k,S,advOpt); toc
sector_smooth_prob.m
% This function runs the prob of transition for k = 3 for each sector, then % takes the smoothed probabilities and observes the differences between % each sector.
[r mu cov] = getRetCov(R); r2 = SecRet(r,sec);
% Transpose it to have columns to denote each sector return, and rows to % denote time. r2 = r2'; smoothProb_3 = zeros(size(r2));
for i=1:length(r2) dep=r2(:,i); constVec=ones(length(dep),1); indep=constVec; k=3; S=[1 1]; advOpt.distrib='Normal';
advOpt.std_method=1; [Spec_Out]=MS_Regress_Fit(dep,indep,k,S,advOpt); smoothProb_3(:,i) = Spec_Out.smoothProb(:,3); end
ms_regress_fit.m
function [Spec_Output]=MS_Regress_Fit(dep,indep,k,S,advOpt)
% Error checking lines checkInputs(); % checking if inputs variables are OK % building constCoeff for the cases when it is not specified build_constCoeff(); % checking if all fields are specified and make sense check_constCoeff(); % checking sizes of fields in constCoeff checkSize_constCoeff(); % Pre calculations before calling the optimizer preCalc_MSModel(); % Initialization of optimization algorithm warning('off'); options=optimset('fmincon'); options=optimset(options,'display','off'); dispOut=advOpt.printIter; % Defining linear contraints in model A=[]; % inequality constrain (not used) b=[]; % inequality constrain (not used) % equality constraint (each collum of Coeff.p must sum to 1) beq=ones(k,1); Aeq=zeros(k,numel(param0));
-
for i=1:k idx=Coeff_Tag.p(:,i);
for j=1:numel(idx) if idx(j)==0 continue; else Aeq(i,idx(j))=1; end end end for i=1:k if all(Aeq(i,:)==0) Aeq(i,:)=0; % fixing equality restrictions for when using contrained
estimation in Coeff.p beq(i,:)=0; end end
param0=param0'; % changing notation for param0
% Call to optimization function switch advOpt.optimizer case 'fminsearch' options=optimset('fminsearch'); options=optimset(options,'display','off'); options=optimset(options,'MaxIter',500*numel(param0)); options=optimset(options,'MaxFunEvals',500*numel(param0));
[param]=fminsearch(@(param)MS_Regress_Lik(dep,indep_nS,indep_S,param,k,S,advOpt,dis
pOut),param0,options);
case 'fminunc' options=optimset('fminunc'); options=optimset(options,'display','off');
[param]=fminunc(@(param)MS_Regress_Lik(dep,indep_nS,indep_S,param,k,S,advOpt,dispOu
t),param0,options);
case 'fmincon' options=optimset('fmincon'); options=optimset(options,'display','off');
[param]=fmincon(@(param)MS_Regress_Lik(dep,indep_nS,indep_S,param,k,S,advOpt,dispOu
t),param0, ... A,b,Aeq,beq,lB,uB,[],options);
end % Calculation of Covariance Matrix [V]=getvarMatrix_MS_Regress(dep,indep_nS,indep_S,param,k,S,std_method,advOpt); param_std=sqrt(diag((V)));
-
% Controls for covariance matrix. If found imaginary number for variance, replace
with % Inf. This will then be showed at output param_std(isinf(param_std))=0; param_pvalues=2*(1-tcdf(abs(param./param_std),nr-numel(param)));
if ~isreal(param_std) for i=1:numel(param) if ~isreal(param_std(i)) param_std(i)=Inf; end end end
typeCall='se_calculation';
[Coeff_SE]=param2spec(param_std,Coeff_Tag,constCoeff,typeCall); [Coeff_pValues]=param2spec(param_pvalues,Coeff_Tag,constCoeff,typeCall); % After finding param, filter it to the data to get estimated output [sumlik,Spec_Output]=MS_Regress_Lik(dep,indep_nS,indep_S,param,k,S,advOpt,0);
% calculating smoothed probabilities Prob_t_1=zeros(nr,k); Prob_t_1(1,1:k)=1/k; % This is the matrix with probability of s(t)=j conditional on
the information in t-1
for i=2:nr Prob_t_1(i,1:k)=(Spec_Output.Coeff.p*Spec_Output.filtProb(i-1,1:k)')'; end
filtProb=Spec_Output.filtProb;
P=abs(Spec_Output.Coeff.p); smoothProb=zeros(nr,k); smoothProb(nr,1:k)=Spec_Output.filtProb(nr,:); % last observation for starting
filter
for i=nr-1:-1:1 % work backwards in time for smoothed probs for j1=1:k for j2=1:k
smooth_value(1,j2)=smoothProb(i+1,j2)*filtProb(i,j1)*P(j2,j1)/Prob_t_1(i+1,j2); end smoothProb(i,j1)=sum(smooth_value); end end
% Calculating Expected Duration of regimes stateDur=1./(1-diag(Spec_Output.Coeff.p)); Spec_Output.stateDur=stateDur;
% passing values to output structure Spec_Output.smoothProb=smoothProb; Spec_Output.nObs=size(Spec_Output.filtProb,1); Spec_Output.nEq=nEq;
-
Spec_Output.Number_Parameters=numel(param); Spec_Output.advOpt.distrib=distrib; Spec_Output.advOpt.std_method=std_method; Spec_Output.Coeff_SE=Coeff_SE; Spec_Output.Coeff_pValues=Coeff_pValues; Spec_Output.AIC=2*numel(param)-2*Spec_Output.LL; Spec_Output.BIC=-2*Spec_Output.LL+numel(param)*log(Spec_Output.nObs*nEq);
% ploting probabilities if advOpt.doPlots doPlots(); end
% Sending output to matlab's screen disp(' '); if advOpt.printOut doOutScreen() end
plotMVO.m:
[PrS, mu1 mu2 mu3 cov1 cov2 cov3 ] = plotState(sp,r');
[w v ER] = runMVO(cov,mu); [w1 v1 ER1] = runMVO(cov1,mu1); [w2 v2 ER2] = runMVO(cov2,mu2); [w3 v3 ER3] = runMVO(cov3,mu3);
plot(ER,v,'bo-'); hold on plot(ER1,v1,'b-'); plot(ER2,v2,'r-'); plot(ER3,v3, 'g-'); hold off legend('MVO without Regime-Switching', 'State 1', 'State 2', 'State 3') title('MVO') xlabel('Expected Return') ylabel('Variance')
plotState.m:
function [ PrS, mu1, mu2, mu3, cov1, cov2, cov3,] = plotState( sp, r2 ) % sp - smooth probabilities for s states % r2 - sector returns
[m,n] = size(sp); % m - time, n - # states [t,s] = size(r2); % t - time, s - # sectors
% 3 States % Band value to determine if sector is in a state b = 0.8; sp2 = sp > b;
-
s1 = sp2(:,1); s2 = sp2(:,2); s3 = sp2(:,3);
% Probability in each state PrS = sum(sp2)/m;
% Get returns while in states r3_1 = zeros(t,s); r3_2 = zeros(t,s); r3_3 = zeros(t,s);
for i = 1:s r3_1(:,i) = s1.*r2(:,i); r3_2(:,i) = s2.*r2(:,i); r3_3(:,i) = s3.*r2(:,i); end
% Remove zeros (returns in other states) r3_1 = r3_1(any(r3_1,2),:); r3_2 = r3_2(any(r3_2,2),:); r3_3 = r3_3(any(r3_3,2),:);
% Get mean and covariance for stocks in states mu1 = mean(r3_1); mu2 = mean(r3_2); mu3 = mean(r3_3);
[t1,~] = size(r3_1); [t2,~] = size(r3_2); [t3,~] = size(r3_3);
cov1 = zeros(s,s); for i=1:s for j = 1:i cov1( j, i ) = ( 1/( t1-1 ) )*sum( (r3_1(:,i) - mu1(i)).*(r3_1(:,j) -
mu1(j)) ); cov1( i, j ) = cov1( j, i ); end end
cov2 = zeros(s,s); for i=1:s for j = 1:i cov2( j, i ) = ( 1/( t2-1 ) )*sum( (r3_2(:,i) - mu2(i)).*(r3_2(:,j) -
mu2(j)) ); cov2( i, j ) = cov2( j, i ); end end
cov3 = zeros(s,s); for i=1:s for j = 1:i
-
cov3( j, i ) = ( 1/( t3-1 ) )*sum( (r3_3(:,i) - mu3(i)).*(r3_3(:,j) -
mu3(j)) ); cov3( i, j ) = cov3( j, i ); end end
% Run MVO to get optimal weights in states mu1 = mu1'; mu2 = mu2'; mu3 = mu3';
getRetCov.m
function [ returns means covariance] = getRetCov( stockData ) %PROCESSRETURNS - Processes returns for matrix of stock data % % MIE479 % % Inputs: % stockData - n x t matrix where n is the number of assets % t is number of dates % % Outputs: % returns = logarithmic return of asset i at time t (n x t-1 matrix) % means = mean return of asset i ( n x 1 matrix ) % covariance = n x n matrix containing constant covariance over T between assets
[n, t] = size( stockData ); returns = zeros(n, t-1);
for i = 1:n for j = 1:t-1 returns(i, j) = log( stockData(i, j+1) / stockData(i, j) ); end end
%means = prod(1 + returns, 2).^(1/(t - 1)) - 1; means = zeros(n, 1); for i = 1:n means(i) = sum( returns(i,:))/(t-1); end
covariance = zeros(n,n); for i=1:n for j = 1:i covariance( j, i ) = ( 1/( t-1 ) )*sum( (returns(:,i) -
means(i)).*(returns(:,j) - means(j)) ); covariance( i, j ) = covariance( j, i ); end end