【问题标题】:Quantile regression with linprog in Matlab在 Matlab 中使用 linprog 进行分位数回归
【发布时间】:2014-03-06 14:25:14
【问题描述】:

我正在尝试使用 Matlab 中的简单设置来实现分位数回归过程。 This page 包含将分位数回归描述为线性程序,并显示适当的矩阵和向量。我试图在 Matlab 中实现它,但我没有得到正确的 bhat 向量的最后一个元素。它应该在 1 左右,但我得到的值非常低(A、bf 是错误的。

我尝试过使用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);

【问题讨论】:

    标签: matlab regression linear-programming quantile-regression


    【解决方案1】:

    我通过使用上面我试图在问题中实施的公式(在 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
    

    【讨论】:

    • 嗨。我已尝试运行您的代码,但它的行为似乎不像我预期的那样。例如,使用x = -5+10*[0:0.001:1]'y = normrnd(x,1),使用函数调用qregress( y, [ones(size(x)), x], 0.5 ) 我没有恢复正确的-5 截距,代码返回接近0。我提交`
    • 我必须承认我没有安装Matlab了,所以我恐怕帮不上忙。
    猜你喜欢
    • 2019-07-11
    • 2018-07-16
    • 2017-08-10
    • 1970-01-01
    • 2014-05-27
    • 2018-03-05
    • 2020-08-01
    • 2013-05-25
    • 1970-01-01
    相关资源
    最近更新 更多