楼主: elite7502
26703 22

[问答] 如何使用MATLAB进行分位数回归? [推广有奖]

11
DFP2008 发表于 2014-12-23 22:34:41
谢谢分享程序。。。。。。

12
无他 发表于 2014-12-25 15:20:06
matlab对编程要求较高,相对来说stata上手容易些
可是matlab确实很好很强大

13
gwkfreedom 发表于 2015-2-11 20:14:13
liwenxue_137 发表于 2013-10-30 08:06
没有查过,我用的是R软件做的,有个quantreg分位数包,应有尽有
请问怎么用quantreg包做计数数据的分位数回归?

14
達爾之碌 发表于 2015-4-21 14:29:46
同样求教啊

15
liwenxue_137 发表于 2015-6-3 21:09:12
function b = rq_fnm(X, y, p)
% Construct the dual problem of quantile regression
% Solve it with lp_fnm
%
%
[m n] = size(X);
u = ones(m, 1);
a = (1 - p) .* u;
b = -lp_fnm(X', -y', X' * a, u, a)';

function y = lp_fnm(A, c, b, u, x)
% Solve a linear program by the interior point method:
% min(c * u), s.t. A * x = b and 0 < x < u
% An initial feasible solution has to be provided as x
%
% Function lp_fnm of Daniel Morillo & Roger Koenker
% Translated from Ox to Matlab by Paul Eilers 1999
% Modified by Roger Koenker 2000--
% More changes by Paul Eilers 2004


% Set some constants
beta = 0.9995;
small = 1e-5;
max_it = 50;
[m n] = size(A);

% Generate inital feasible point
s = u - x;
y = (A' \  c')';
r = c - y * A;
r = r + 0.001 * (r == 0);    % PE 2004
z = r .* (r > 0);
w = z - r;
gap = c * x - y * b + w * u;

% Start iterations
it = 0;
while (gap) > small & it < max_it
    it = it + 1;
   
    %   Compute affine step
    q = 1 ./ (z' ./ x + w' ./ s);
    r = z - w;
    Q = spdiags(sqrt(q), 0, n, n);
    AQ = A * Q;          % PE 2004
    rhs = Q * r';        % "
    dy = (AQ' \ rhs)';   % "
    dx = q .* (dy * A - r)';
    ds = -dx;
    dz = -z .* (1 + dx ./ x)';
    dw = -w .* (1 + ds ./ s)';
   
    % Compute maximum allowable step lengths
    fx = bound(x, dx);
    fs = bound(s, ds);
    fw = bound(w, dw);
    fz = bound(z, dz);
    fp = min(fx, fs);
    fd = min(fw, fz);
    fp = min(min(beta * fp), 1);
    fd = min(min(beta * fd), 1);
   
    % If full step is feasible, take it. Otherwise modify it
    if min(fp, fd) < 1
        
        % Update mu
        mu = z * x + w * s;
        g = (z + fd * dz) * (x + fp * dx) + (w + fd * dw) * (s + fp * ds);
        mu = mu * (g / mu) ^3 / ( 2* n);
        
        % Compute modified step
        dxdz = dx .* dz';
        dsdw = ds .* dw';
        xinv = 1 ./ x;
        sinv = 1 ./ s;
        xi = mu * (xinv - sinv);
        rhs = rhs + Q * (dxdz - dsdw - xi);
        dy = (AQ' \ rhs)';
        dx = q .* (A' * dy' + xi - r' -dxdz + dsdw);
        ds = -dx;
        dz = mu * xinv' - z - xinv' .* z .* dx' - dxdz';
        dw = mu * sinv' - w - sinv' .* w .* ds' - dsdw';
        
        % Compute maximum allowable step lengths
        fx = bound(x, dx);
        fs = bound(s, ds);
        fw = bound(w, dw);
        fz = bound(z, dz);
        fp = min(fx, fs);
        fd = min(fw, fz);
        fp = min(min(beta * fp), 1);
        fd = min(min(beta * fd), 1);
        
    end
   
    % Take the step
    x = x + fp * dx;
    s = s + fp * ds;
    y = y + fd * dy;
    w = w + fd * dw;
    z = z + fd * dz;
    gap = c * x - y * b + w * u;
    %disp(gap);
end

function b = bound(x, dx)
% Fill vector with allowed step lengths
% Support function for lp_fnm
b = 1e20 + 0 * x;
f = find(dx < 0);
b(f) = -x(f) ./ dx(f);

这个是大师Roger Koenker写的分位数回归包---matlab程序 内点(Frisch-Newton)算法。

A basic version of the interior point (Frisch-Newton) algorithm for quantile regression developed for the R quantreg package is also available for matlab. A C++ translation of the algorithm is also available from Ron Gallant's libcpp library. This algorithm is described in Koenker and Portnoy (Statistical Science, 1997).

16
liwenxue_137 发表于 2015-6-3 21:12:42
我用的R软件,有线性规划、内点2个算法,还有一种算法忘记了

17
hollyshiit 发表于 2016-4-29 09:34:05
赞&#128077;

18
wenhaoaho 发表于 2016-5-18 00:08:49
liwenxue_137 发表于 2013-11-10 19:48
function b = rq_fnm(X, y, p)
% Construct the dual problem of quantile regression
% Solve it with l ...
请问这个是什么方法呢,如何初始化变量呢?matlab小白求指教

19
matlab-007 发表于 2016-7-24 14:19:41
matlab自带的一些常用分布的分布律或概率密度。 如果把分布函数名的后缀cdf改为inv,便得到了相应分布函数的反函数.这些常用分布的分布函数及其反函数对于实际应用很方便。

20
wxg319 发表于 2016-10-12 23:59:24
matlab 的分位数程序包 楼上提供有几个,速度最快的是rq_fnm ,其计算速度是quantreg的计算时间的1/40倍,其stata的运行时间都比quantreg快得多,由此推断stata也是采用内点法。也有非线性的分位数回归,没有去实验速度。
MATLAB code for quantile regression
Here are a couple MATLAB functions that perform nonlinear quantile regression.
ipqr.m, which uses an interior point method of Koenker and Park (1996, J. Econometrics). This function requires a second supporting function, ipqr_objfunc.m.
mmqr.m, which uses a Majorize-Minimize method of Hunter and Lange (2000, J. Comp. Graph. Statistics).
可以翻墙下载。

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-26 16:47