Maximization problem

I was wondering if someone could help me with maximisation problem in MATLAB.
I need to maximise sum[-ln(v(i)) - u^2(i)/v(i)], where v(i) = S^2(i) and the following formula is given for S^2:
S^2(n) = w + A*u^2(n-1) + B*S^2(n-1).
I have data set for u(i) for i=1....n. i.e. u(i)'s are given.
Additional info:
w = G*V; V = w/(1-A-B).
A + B + G = 1.
Basically, I need to find optimal parameter values for w, A and B which maximise the sum I gave above.
My data set is too long, so I can't use Excel and I would really appreciate it if someone could help me out with the code. Thanks.

 Respuesta aceptada

Matt Tearle
Matt Tearle el 24 de Mayo de 2011

2 votos

If I'm interpreting correctly, your v (aka S^2) is recursively defined. So presumably you have some way to determine u(0) and S(0)^2. Based on that, you can write a function to determine v(k) either as a matrix calculation or using a loop.
v = @(x) (diag(ones(n,1),0)+diag(-x(3)*ones(n-1,1),-1))\(x(1)+[x(2)*u0^2+x(3)*v0;x(2)*u(1:end-1).^2]);
Now define your objective function
f = @(x) sum(-log(v(x)) - u.^2./v(x));
Then apply a minimization routine
xmin = fminsearch(f,x0)
were x0 is your initial guess for x = [w;A;B].
EDIT TO ADD Based on comments below, here's a fuller solution:
n = 123;
v0 = 0;
u = %get u;
u0 = u(1,1);
v = @(x) (diag(ones(n,1),0)+diag(-x(3)*ones(n-1,1),-1))\(x(1)+[x(2)*u0^2+x(3)*v0;x(2)*u(1:end-1).^2]);
f = @(x) -sum(-log(v(x)) - u.^2./v(x));
x0 = [0.001; 0.0626; 0.8976];
xmin = fmincon(f,x0,[0,1,1],1,[],[],[-Inf;0;0],[Inf;1;1],[],optimset('Algorithm','interior-point'))
-f(xmin)
If you have Global Optimization Toolbox, you can do this multiple times:
ms = MultiStart;
problem = createOptimProblem('fmincon','x0',x0,...
'objective',f,'lb',[-Inf;0;0],'ub',[Inf;1;1],'Aineq',[0,1,1],'bineq',1,'options',optimset('Algorithm','interior-point'));
xmin = run(ms,problem,30)
-f(xmin)
(change the 30 to whatever you consider sufficient). If you don't have Global Opt TB, you could program it yourself. Make a grid of initial points, loop over them, and keep the best solution. Or do a loop and use a random x0 each time.

7 comentarios

Julia
Julia el 24 de Mayo de 2011
The function works for some data but not for all. The following is my adjusted code:
n = 123;
v0 = 0;
u = xlsread('vars_for_ports', 'b2:b124');
u0 = u(1,1);
v = @(x) (diag(ones(n,1),0)+diag(-x(3)*...
ones(n-1,1),-1))\(x(1)+[x(2)*u0^2+x(3)*v0;x(2)*...
u(1:end-1).^2]);
f = @(x) -sum(-log(v(x)) - u.^2./v(x));
x0 = [0.001; 0.0626; 0.8976];
xmin = fminsearch(f,x0)
_________________________________________
Since it is a maximization problem and not minimization, I made a minor change in function f. My problem however is that all the optimal values MUST be positive and when I run it, it sometimes gives me negative values. So, where would you recommend I make the adjustment for this constraint? I tried playing with function v but nothing worked.
Matt Tearle
Matt Tearle el 24 de Mayo de 2011
I don't have access to your data, so I'm guessing here, but my experiments seem to indicate that you can run into problems with some parameter values that give imaginary values of v(x). Is there a constraint on w, A, and B that should, theoretically, avoid this? I notice you mention constraints above -- what is V? Is this a fixed/known value? If so, there's a simple constraint. Are there others (eg the parameters all have to be positive)?
Julia
Julia el 25 de Mayo de 2011
Basically the whole thing is a GARCH model (I don't know if you heard about it) which helps to estimate volatility. The formula goes like this:
S(n)^2 = G*V + A*u(n-1)^2 + B*S(n-1)^2,
where w = G*V. The constraint is that G + A + B = 1 since these are weights of sorts, and they are all positive.
The way the book does it is they let w, A and B to be some initial values and then they run optimisation. When they find optimal values, they calculate G = 1 - A - B and V = w/G. But obviously it doesn't work well for all data.
V is not known from the start. This is why w replaces G*V.
I actually went the long way and tried to do it in Excel but the same thing happens - it works for some data and doesn't work for other even though I impose the constraints (at least I think I do). It works perfectly for some data but then it either doesn't work at all for other data or gives me w, A, B values which make absolutely no sense. Also, sometimes the choice of initial parameters matters. For one data set I was playing around with initial parameters for about 15 minutes until miraculously I got the optimisation to work. I know that MATLAB can also be sensitive to choice of initial parameters (I learnt this when I was extracting the yield curve via Nelson-Siegel and cubic splines) but I still thought that MATLAB would perhaps be more efficient.
For now, I am going to make educated guesses about parameters and proceed with calculations of Value at Risk because I have to submit my project tomorrow evening. I hate the idea because even the small change in parameter could lead to some serious changes in my further calculations but I have no choice. If you, however, have any ideas at all how we could implement those constraints I would really appreciate it if you'd tell me because I'm going to set up my further calculations in such a way that if I have to change a couple of numbers the whole thing will change.
Matt Tearle
Matt Tearle el 25 de Mayo de 2011
This isn't a matter of Excel or MATLAB or any other implementation -- it's a problem of mathematics. Global optimization problems are hard.
But knowing some constraints helps. Are there any constraints on V? From what you've said, A & B have to be positive. G has to be positive also, but maybe that can be achieved with w and V both negative -- is that allowed? (I have *heard* of GARCH, but that's about it -- I have no idea what these variables represent.) Given that G is positive, we also have that A + B < 1. So that gives us something to work with, at least.
See original answer above for updated solution.
Julia
Julia el 25 de Mayo de 2011
V has to be positive. A, B and G lie between 0 and 1. In practice A and B are NEVER zero and G could be zero but I really don't want it to be zero.
Your updated code ran quite well with my data. I had a problem with only one asset. The A, B, w seem ok but the resulting V that I get seems to be too high but I think the problem is in the series rather than code. I actually found a garchfit function in Econometrics toolbox and managed to use it on my data. I get nice values for all data sets except one - the same one that I have problems with when I run your code. I get G = 0 which is possible but this means that the model is no longer GARCH but EWMA which is a special case of GARCH and this means that I'm going to have to explain a whole lot more during my presentation than I want to.
The estimates from garchfit, although nice, are quite different to the ones obtained with your code but it's likely due to some assumptions used by garchfit with default settings. I think I'm going to stick to the values I got with your code but it would be interesting to see if there's any effect on the first data set if I were to let G >=0 rather than G>0. I was looking at your code and I read the help file on fmincon. I understand the first constraint, which was A + B < 1, but the second one was a bit weird. I'm not sure where to impose G>=0. If you have a minute, please have a look at it again, but if not, then it's ok - you're probably sick of me by now :).
Julia
Julia el 25 de Mayo de 2011
Actually, never mind that. Everything's perfect. Thanks again for your help.
Matt Tearle
Matt Tearle el 25 de Mayo de 2011
OK... but just for the record: the 7th and 8th arguments of fmincon ([-Inf;0;0] & [Inf;1;1]) are the constraints on the values of w, A, and B. If V>0 then w>0 also, so you can change these to [0;0;0] & [Inf;1;1].
When I run it, I don't get G=0. If you want to force >0 rather than >=0, you can use realmin instead of 0 in the bounds.

Iniciar sesión para comentar.

Más respuestas (1)

Michael Johnston
Michael Johnston el 24 de Mayo de 2011

0 votos

It's somewhat confusing the way you've written the question (e.g., it's not clear if by S^2(n) you mean S^2*n or S(n)^2 or what). But basically it looks like a simple constrained optimization problem. Look through the documentation, give it your best shot. If you get stuck, come back and post your code and the error.

1 comentario

Julia
Julia el 24 de Mayo de 2011
It's S(n)^2 and it is a simple optimisation problem. The thing is I'm pretty bad at Matlab and I'm under time pressure. I guess it's just gonna be a very long night with Excel then. God help me.

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 24 de Mayo de 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by