python分位数回归模型_分位数回归及其Python源码

分位数回归及其Python源码

天朗气清,惠风和畅。赋闲在家,正宜读书。前人文章,不得其解。代码开源,无人注释。你们不来,我行我上。废话少说,直入主题。o( ̄︶ ̄)o

我们要探测自变量

与因变量

的关系,最简单的方法是线性回归,即假设:

我们通过最小二乘方法 (OLS: ordinary least squares),

的可靠性问题,我们同时对残差

做了假设,即:

为均值为0,方差恒定的独立随机变量。

即为给定自变量

下,因变量

的条件均值。

假如残差

不满足我们的假设,或者更重要地,我们不仅仅想要知道

的在给定

下的条件均值,而且想知道是条件中位数(更一般地,条件分位数),那么OLS下的线性回归就不能满足我们的需求。分位数回归(Quantile Regression)[2]解决了这些问题,下面我先给出一个分位数回归的实际应用例子,再简述其原理,最后再分析其在Python实现的源代码。

1. 一个例子:收入与食品消费

这个例子出自statasmodels:Quantile Regression.[3] 我们想探索家庭收入与食品消费的关系,数据出自working class Belgian households in 1857 (the Engel data).我们用Python包statsmodels实现分位数回归。

1.1 预处理

%matplotlib inline

import numpy as np

import pandas as pd

import statsmodels.api as sm

import statsmodels.formula.api as smf

import matplotlib.pyplot as plt

data = sm.datasets.engel.load_pandas().data

data.head()

income foodexp

0420.157651255.839425

1541.411707310.958667

2901.157457485.680014

3639.080229402.997356

4750.875606495.560775

1.2 中位数回归 (分位数回归的特例,q=0.5)

mod = smf.quantreg('foodexp ~ income', data)

res = mod.fit(q=.5)

print(res.summary())

QuantReg Regression Results

==============================================================================

Dep. Variable: foodexp Pseudo R-squared: 0.6206

Model: QuantReg Bandwidth: 64.51

Method: Least Squares Sparsity: 209.3

Date: Mon, 21 Oct 2019 No. Observations: 235

Time: 17:46:59 Df Residuals: 233

Df Model: 1

==============================================================================

coef std err t P>|t| [0.025 0.975]

------------------------------------------------------------------------------

Intercept 81.4823 14.634 5.568 0.000 52.649 110.315

income 0.5602 0.013 42.516 0.000 0.534 0.586

==============================================================================

The condition number is large, 2.38e+03. This might indicate that there are

strong multicollinearity or other numerical problems.

由结果可以知道

<

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值