My current interest is to use the GMM to estimate the mean reverting
model. It is straightforward to use his demo...
Wednesday, December 10, 2008
Monday, December 1, 2008
Multivariate Linear Regression
The trick is to calculating the covariance and dealing with the missing data.....
Key command:
% Estimate CAPM for each asset separately
[Param, Covar] = ecmmvnrmle(TestData, TestDesign);
% Estimate ideal standard errors for covariance parameters
[StdParam, StdCovar] = ecmmvnrstd(TestData,TestDesign, Covar, 'fisher');
% Estimate sample standard errors for model parameters
StdParam = ecmmvnrstd(TestData, TestDesign, Covar, 'hessian');
Key command:
% Estimate CAPM for each asset separately
[Param, Covar] = ecmmvnrmle(TestData, TestDesign);
% Estimate ideal standard errors for covariance parameters
[StdParam, StdCovar] = ecmmvnrstd(TestData,TestDesign, Covar, 'fisher');
% Estimate sample standard errors for model parameters
StdParam = ecmmvnrstd(TestData, TestDesign, Covar, 'hessian');
Thursday, October 30, 2008
Matlab Array tricks
I would like to say that paying attention on arrays of matlab when programming will
reward much! In regular massive vector calculation, it even can beat C++/C program if the latter is not strongly optimized.
reward much! In regular massive vector calculation, it even can beat C++/C program if the latter is not strongly optimized.
Sunday, October 26, 2008
Robust Linear Regression
1. Classical Pincinple Component Regression
First Peform PCA analysis on the x-variables. Then
try to load first k princinple component of the system.
2. Robust Pincinple Component Regression
Classical Pincinple Component Regressionv+ Robust Covariance
Estimation+ Projection Pursuit
See the underlying link for further suggestion and matlab code
First Peform PCA analysis on the x-variables. Then
try to load first k princinple component of the system.
2. Robust Pincinple Component Regression
Classical Pincinple Component Regressionv+ Robust Covariance
Estimation+ Projection Pursuit
See the underlying link for further suggestion and matlab code
Monday, September 8, 2008
A way to find jumps
Everyone knows that returns of our equity markets or other financial market are not following normal distribution. Everyone knows that jump will occur when
a big event strikes. But how to detect the jump?
The link I given here gave a self-consistence solution.
Cheers!
a big event strikes. But how to detect the jump?
The link I given here gave a self-consistence solution.
Cheers!
Sunday, February 24, 2008
USE MATLAB doing portfolio choice
Reading "Developing Portfolio Optimization Models" by Bob Taylor
1. Estimating Asset Return Moments
Use the Financial Toolbox function ecmnmle to deal with data sets that have missing values (represented as NaNs in MATLAB).
2. The portopt function in Financial Toolbox lets us determine directly which portfolios of assets lie along the efficient frontier given the means and covariances
of individual asset returns.
3. Finding a Stable Region
4. Checking:
Turnover refers to the change in portfolio holdings over time due to trading. A portfolio with annual turnover of 25% will replace a quarter of its assets over a one-year period. Since trading is costly, low turnover is a desirable feature of a portfolio strategy.
Evaluating the maximum drawdown of a portfolio is a good way to measure ex post risk.
5. Nevertheless, the potential to beat the market by an average 150 basis points with low turnover is an encouraging first step.
1. Estimating Asset Return Moments
Use the Financial Toolbox function ecmnmle to deal with data sets that have missing values (represented as NaNs in MATLAB).
2. The portopt function in Financial Toolbox lets us determine directly which portfolios of assets lie along the efficient frontier given the means and covariances
of individual asset returns.
3. Finding a Stable Region
4. Checking:
Turnover refers to the change in portfolio holdings over time due to trading. A portfolio with annual turnover of 25% will replace a quarter of its assets over a one-year period. Since trading is costly, low turnover is a desirable feature of a portfolio strategy.
Evaluating the maximum drawdown of a portfolio is a good way to measure ex post risk.
5. Nevertheless, the potential to beat the market by an average 150 basis points with low turnover is an encouraging first step.
Friday, February 22, 2008
"Too many input parameters" in fmincon
This is a typical problem when nonlinearconstraint
function and the objective function are coexisting
in Matlab 6.5.
The most convenient way is to define the same
parameters set for both nonlinearconstraint
function and the objective function despite
one of thee may not need so much parameters.
Just like:
[dccparameters,dccllf,EXITFLAG,OUTPUT,LAMBDA,GRAD]=fmincon('agdcc_mvgarch_likelihood',dccstarting, [], [],[],[],[],[],'mycon',options,A,B,C,D,...);
So function [logL, Rt, likelihoods, Qt]=agdcc_mvgarch_likelihood(params, A,B,C,D,...)
And my con also define as:
function [c,ceq] = mycon(dccp,A,B,C,D,...)
function and the objective function are coexisting
in Matlab 6.5.
The most convenient way is to define the same
parameters set for both nonlinearconstraint
function and the objective function despite
one of thee may not need so much parameters.
Just like:
[dccparameters,dccllf,EXITFLAG,OUTPUT,LAMBDA,GRAD]=fmincon('agdcc_mvgarch_likelihood',dccstarting, [], [],[],[],[],[],'mycon',options,A,B,C,D,...);
So function [logL, Rt, likelihoods, Qt]=agdcc_mvgarch_likelihood(params, A,B,C,D,...)
And my con also define as:
function [c,ceq] = mycon(dccp,A,B,C,D,...)
Friday, February 15, 2008
The Usage of fmincon in matlab
Try to understand what used in DCC_GARCH model
options=optimset('fmincon');
options = optimset(options , 'Display' , 'iter');
options = optimset(options , 'Diagnostics' , 'on');
options = optimset(options , 'LevenbergMarquardt' , 'on');
options = optimset(options , 'LargeScale' , 'off');
dccstarting=[ones(1,dccP)*.01/dccP ones(1,dccQ)*.97/dccQ];
fprintf(1,'\n\nEstimating the DCC model\n')
[dccparameters,dccllf,EXITFLAG,OUTPUT,LAMBDA,GRAD]=fmincon('dcc_mvgarch_likelihood',dccstarting,ones(size(dccstarting)),[1-2*options.TolCon],[],[],zeros(size(dccstarting))+2*options.TolCon,[],[],options,stdresid,dccP,dccQ);
fmincon('dcc_mvgarch_likelihood',dccstarting,ones(size(dccstarting)),[1-2*options.TolCon],[],[],zeros(size(dccstarting))+2*options.TolCon,[],[],options,stdresid,dccP,dccQ);
<=> (fun, x0, A,b,[],[],lb,[],[],options,stdresid,dccP,dccQ);
Notice options.TolCon is the precision (so here the constraint consider the precision)
1. Syntax
x = fmincon(fun,x0,A,b) starts at x0 and attempts to find a minimum x to the function described in fun subject to the linear inequalities A*x <= b. x0 can be a scalar, vector, or matrix.
x = fmincon(fun,x0,A,b,Aeq,beq) minimizes fun subject to the linear equalities Aeq*x = beq as well as A*x <= b. Set A=[] and b=[] if no inequalities exist.
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub) defines a set of lower and upper bounds on the design variables in x, so that the solution is always in the range lb <= x <= ub. Set Aeq=[] and beq=[] if no equalities exist. Set lb(i)=-Inf if x(i) is unbounded below, and set ub(i)=Inf if x(i) is unbounded above.
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon) subjects the minimization to the nonlinear inequalities c(x) or equalities ceq(x) defined in nonlcon. fmincon optimizes such that c(x) <= 0 and ceq(x) = 0. Set lb=[] and/or ub=[] if no bounds exist.
eg: function [c,ceq] = mycon(x)
c = ... % Compute nonlinear inequalities at x.
ceq = ... % Compute nonlinear equalities at x.
One can use ceq = []; if no constraints....
If use Heissian method:
function [c,ceq,GC,GCeq] = mycon(x)
c = ... % Nonlinear inequalities at x
ceq = ... % Nonlinear equalities at x
if nargout > 2 % nonlcon called with 4 outputs
GC = ... % Gradients of the inequalities
GCeq = ... % Gradients of the equalities
end
fmincon(@objfun,x0,[],[],[],[],[],[],@confun,options)
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options) minimizes with the optimization options specified in the structure options. Use optimset to set these options. Set nonlcon = [] if there are no nonlinear inequality or equality constraints.
[x,fval] = fmincon(...) returns the value of the objective function fun at the solution x.
[x,fval,exitflag] = fmincon(...) returns a value exitflag that describes the exit condition of fmincon.
[x,fval,exitflag,output] = fmincon(...) returns a structure output with information about the optimization.
[x,fval,exitflag,output,lambda] = fmincon(...) returns a structure lambda whose fields contain the Lagrange multipliers at the solution x.
[x,fval,exitflag,output,lambda,grad] = fmincon(...) returns the value of the gradient of fun at the solution x.
[x,fval,exitflag,output,lambda,grad,hessian] = fmincon(...) returns the value of the Hessian at the solution x. See Hessian.
2. use NARGOUT to determine the parameters
function [f,g] = myfun(x)
f = ... % Compute the function value at x
if nargout > 1 % fun called with two output arguments
g = ... % Compute the gradient evaluated at x
end
3. If have Global parameters used in the object function:
a) Parameterizing Your Function as a Nested Function
As an alternative to writing your function as an anonymous function, you can write a single M-file that
Accepts the additional parameters to your function as inputs.
Invokes the optimization function.
Contains your function as a nested function.
The following example illustrates how to write an M-file to find zeros of the x3 + bx+ c, for different values of the coefficients b and c.
function y = findzero(b, c, x0)
options = optimset('Display', 'off'); % Turn off Display
y = fsolve(@poly, x0, options);
function y = poly(x) % Compute the polynomial.
y = x^3 + b*x + c;
end
end
b) As an example, suppose you want to find the zeros of the function ellipj using fsolve. fsolve expects the objective function to take one input argument, but the ellipj function takes two, u and m. You can see this function by typing
type ellipj
You are solving for the variable u, whiled m is simply a second parameter to specify which Jacobi elliptic function. To look for a zero near u0 = 3 for m = 0.5, you can create a function handle to an anonymous function that captures the current value of m from the workspace. Then, when the solver fsolve calls this function handle, the parameter m exists and ellipj will be called with two arguments. You pass this function handle to fsolve with the following commands:
u0 = 3; m = 0.5;
options = optimset('Display','off'); % Turn off Display
x = fsolve(@(u) ellipj(u,m), u0, options)
options=optimset('fmincon');
options = optimset(options , 'Display' , 'iter');
options = optimset(options , 'Diagnostics' , 'on');
options = optimset(options , 'LevenbergMarquardt' , 'on');
options = optimset(options , 'LargeScale' , 'off');
dccstarting=[ones(1,dccP)*.01/dccP ones(1,dccQ)*.97/dccQ];
fprintf(1,'\n\nEstimating the DCC model\n')
[dccparameters,dccllf,EXITFLAG,OUTPUT,LAMBDA,GRAD]=fmincon('dcc_mvgarch_likelihood',dccstarting,ones(size(dccstarting)),[1-2*options.TolCon],[],[],zeros(size(dccstarting))+2*options.TolCon,[],[],options,stdresid,dccP,dccQ);
fmincon('dcc_mvgarch_likelihood',dccstarting,ones(size(dccstarting)),[1-2*options.TolCon],[],[],zeros(size(dccstarting))+2*options.TolCon,[],[],options,stdresid,dccP,dccQ);
<=> (fun, x0, A,b,[],[],lb,[],[],options,stdresid,dccP,dccQ);
Notice options.TolCon is the precision (so here the constraint consider the precision)
1. Syntax
x = fmincon(fun,x0,A,b) starts at x0 and attempts to find a minimum x to the function described in fun subject to the linear inequalities A*x <= b. x0 can be a scalar, vector, or matrix.
x = fmincon(fun,x0,A,b,Aeq,beq) minimizes fun subject to the linear equalities Aeq*x = beq as well as A*x <= b. Set A=[] and b=[] if no inequalities exist.
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub) defines a set of lower and upper bounds on the design variables in x, so that the solution is always in the range lb <= x <= ub. Set Aeq=[] and beq=[] if no equalities exist. Set lb(i)=-Inf if x(i) is unbounded below, and set ub(i)=Inf if x(i) is unbounded above.
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon) subjects the minimization to the nonlinear inequalities c(x) or equalities ceq(x) defined in nonlcon. fmincon optimizes such that c(x) <= 0 and ceq(x) = 0. Set lb=[] and/or ub=[] if no bounds exist.
eg: function [c,ceq] = mycon(x)
c = ... % Compute nonlinear inequalities at x.
ceq = ... % Compute nonlinear equalities at x.
One can use ceq = []; if no constraints....
If use Heissian method:
function [c,ceq,GC,GCeq] = mycon(x)
c = ... % Nonlinear inequalities at x
ceq = ... % Nonlinear equalities at x
if nargout > 2 % nonlcon called with 4 outputs
GC = ... % Gradients of the inequalities
GCeq = ... % Gradients of the equalities
end
fmincon(@objfun,x0,[],[],[],[],[],[],@confun,options)
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options) minimizes with the optimization options specified in the structure options. Use optimset to set these options. Set nonlcon = [] if there are no nonlinear inequality or equality constraints.
[x,fval] = fmincon(...) returns the value of the objective function fun at the solution x.
[x,fval,exitflag] = fmincon(...) returns a value exitflag that describes the exit condition of fmincon.
[x,fval,exitflag,output] = fmincon(...) returns a structure output with information about the optimization.
[x,fval,exitflag,output,lambda] = fmincon(...) returns a structure lambda whose fields contain the Lagrange multipliers at the solution x.
[x,fval,exitflag,output,lambda,grad] = fmincon(...) returns the value of the gradient of fun at the solution x.
[x,fval,exitflag,output,lambda,grad,hessian] = fmincon(...) returns the value of the Hessian at the solution x. See Hessian.
2. use NARGOUT to determine the parameters
function [f,g] = myfun(x)
f = ... % Compute the function value at x
if nargout > 1 % fun called with two output arguments
g = ... % Compute the gradient evaluated at x
end
3. If have Global parameters used in the object function:
a) Parameterizing Your Function as a Nested Function
As an alternative to writing your function as an anonymous function, you can write a single M-file that
Accepts the additional parameters to your function as inputs.
Invokes the optimization function.
Contains your function as a nested function.
The following example illustrates how to write an M-file to find zeros of the x3 + bx+ c, for different values of the coefficients b and c.
function y = findzero(b, c, x0)
options = optimset('Display', 'off'); % Turn off Display
y = fsolve(@poly, x0, options);
function y = poly(x) % Compute the polynomial.
y = x^3 + b*x + c;
end
end
b) As an example, suppose you want to find the zeros of the function ellipj using fsolve. fsolve expects the objective function to take one input argument, but the ellipj function takes two, u and m. You can see this function by typing
type ellipj
You are solving for the variable u, whiled m is simply a second parameter to specify which Jacobi elliptic function. To look for a zero near u0 = 3 for m = 0.5, you can create a function handle to an anonymous function that captures the current value of m from the workspace. Then, when the solver fsolve calls this function handle, the parameter m exists and ellipj will be called with two arguments. You pass this function handle to fsolve with the following commands:
u0 = 3; m = 0.5;
options = optimset('Display','off'); % Turn off Display
x = fsolve(@(u) ellipj(u,m), u0, options)
Subscribe to:
Comments (Atom)