2014-02-09 18 views
0

我正尝试在Matlab中使用简单的设置来实现分位数回归过程。 This page包含分位数回归作为线性程序的描述,并显示适当的矩阵和向量。我试图在Matlab中实现它,但我没有得到bhat-vector的最后一个元素。它应该在1左右,但我得到一个非常低的值(< 1e-10)。使用另一种算法,我得到1.0675的值。我哪里做错了?我猜A,B或F是错的。使用Matlab中的linprog进行分位数回归

我尝试过使用optimset,但我不认为这是问题所在。从数学到代码,我认为我犯了一个转换错误,我只是看不到在哪里。

% set seed 
rng(1); 
% set parameters 
n=30; 
tau=0.5; 
% create regressor and regressand 
x=rand(n,1); 
y=x+rand(n,1)/10; 
% number of regressors (1) 
m=size(x,2); 
% vektors and matrices for linprog 
f=[tau*ones(n,1);(1-tau)*ones(n,1);zeros(m,1)]; 

A=[eye(n),-eye(n),x; 
    -eye(n),eye(n),-x; 
    -eye(n),zeros(n),zeros(n,m); 
    zeros(n),-eye(n),zeros(n,m)]; 
b=[y; 
    y 
    zeros(n,1); 
    zeros(n,1)]; 
% get solution bhat=[u,v,beta] and exitflag (1=succes) 
[bhat,~,exflag]=linprog(f',A,b); 

回答

0

我使用配方(以PDF格式)一个我试图在这个问题来实现上述解决我的问题。如果您对代码感兴趣,我已将它放在Matlab函数中。

function [ bhat ] = qregressMatlab(y, x, tau) 
% bhat are the estimates 
% y is a vector of outcomes 
% x is a matrix with columns of explanatory variables 
% tau is a scalar for choosing the conditional quantile to be estimated 

n=size(x,1); 
m=size(x,2); 
% vectors and matrices for linprog 
f=[tau*ones(n,1);(1-tau)*ones(n,1);zeros(m,1)]; 
Aeq=[eye(n),-eye(n),x]; 
beq=y; 
lb=[zeros(n,1);zeros(n,1);-inf*ones(m,1)]; 
ub=inf*ones(m+2*n,1); 

% Solve the linear programme 
[bhat,~,~]=linprog(f,[],[],Aeq,beq,lb,ub); 

% Pick out betas from (u,v,beta)-vector. 
bhat=bhat(end-m+1:end); 

end