检验样本是否服从泊松分布

16 篇文章 0 订阅
3 篇文章 2 订阅

本文以一个订单数据为例,研究顾客购买次数的分布规律,尝试从中估计总体的分布,以对后续的订单数据进行预测或进行业绩的对比。

# 环境准备
import numpy as np
import pandas as pd
import scipy.stats as stats
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']

一、数据预处理

df = pd.read_csv('orders.csv')
df.info()
df.head()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 8568 entries, 0 to 8567
Data columns (total 2 columns):
 #   Column      Non-Null Count  Dtype 
---  ------      --------------  ----- 
 0   customerId  8568 non-null   int64 
 1   orderDate   8568 non-null   object
dtypes: int64(1), object(1)
memory usage: 134.0+ KB
customerIdorderDate
05902010-10-13
15432012-02-20
25572011-07-15
35572011-07-15
45572011-07-15

可以看到订单日期字段是 object 对象,这里就是字符串。因此要先将其转化成 pandas 中的 DataTime 对象,然后再添加年份字段方便后续的分组聚合运算。

df.orderDate = df.orderDate.astype('datetime64')
df['year'] = df.orderDate.map(lambda dt: dt.year)
df.head()
customerIdorderDateyear
05902010-10-13 00:00:002010
15432012-02-20 00:00:002012
25572011-07-15 00:00:002011
35572011-07-15 00:00:002011
45572011-07-15 00:00:002011

二、变量分析

首先通过数据透视表,查看不同年份的购买次数分布。然后再查看单个顾客累计购买次数分布。

counts = pd.pivot_table(
    df, values='orderDate', index=['customerId'], columns=['year'], aggfunc='count'
).fillna(0).astype(int)
counts['all'] = counts.sum(axis=1)
df.head()
customerId2009201020112012all
140408
2745016
3543416
42831427
503126
plt.figure(figsize=(12,5))
counts.plot(kind='box')

在这里插入图片描述

plt.figure(figsize=(12,5))
plt.subplot(121)
plt.hist(counts.sum(axis=1), bins=30)
plt.xlabel('times'); plt.ylabel('counts')

plt.subplot(122)
counts.sum(axis=1).plot(kind='kde')
plt.xlim(-2,125); plt.ylim(0,0.04); plt.xlabel('times')

在这里插入图片描述

三、总体分布估计

根据预览的分布密度,并且由其统计学意义,猜测购买次数近似服从泊松分布。下面进行验证。

# 计算分布参数
lambda_ = counts.drop('all', axis=1).sum(axis=1).mean()
# 分布密度函数
def poisson(k, lambda_=lambda_):
    return np.e**(-lambda_) * lambda_**k / np.math.factorial(k)
# 计算总体分布
predict = [poisson(k) for k in range(60)]

# 可视化每个年份以及汇总的分布密度,与理想的总体分布进行对比
plt.figure(figsize=(12,5))
counts.drop('all', axis=1).plot(kind='kde', linestyle='--', linewidth=1)
counts['all'].plot(kind='kde', linewidth=1, color='lightblue', label='总和')
# 理想泊松分布:
plt.plot(range(60), predict, linewidth=1, color='green', label='泊松分布密度')
# 模拟的泊松分布:
test = pd.Series([stats.poisson.rvs(lambda_) for i in range(10000)])
test.plot(kind='kde', linewidth=1, linestyle='--', color='red', label='模拟的分布密度')
plt.legend(); plt.xlim(-5, 45)

在这里插入图片描述

四、结论与分析

由上图可以看到,首先总和的分布与理想的分布有差异,并且均值与时间跨度有关。具体表现为,一年内的次数分布,对比四年总和的次数分布,均值明显更小,集中趋势也更加显著。由于泊松分布为二项分布的极限分布,可以理解为,时间跨度影响了二项分布中的 n 参数,进而影响泊松分布中的 lambda 参数,亦即总体均值。因此结论得出的是,样本所在总体并不服从泊松分布,但是有明显的类似泊松分布的规律,由于其它未知变量的影响产生了偏移。

另外需要注意到,泊松分布的统计学解释认为每次抽样的条件相同。对应本例中的数据,即每次抽样中,其某个特定时间段(时间点)内购买的概率相同。但是由于数据中每个观测值来自不同的客户,因此不能保证这一点。

阅读原文

Fork Notebook

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值