python画累积分布图_用Python计算累积分布函数(CDF)

I want to calculate it from an array of points I have (discrete distribution), not with the continuous distributions that, for example, scipy has.

解决方案

(It is possible that my interpretation of the question is wrong. If the question is how to get from a discrete PDF into a discrete CDF, then np.cumsum divided by a suitable constant will do if the samples are equispaced. If the array is not equispaced, then np.cumsum of the array multiplied by the distances between the points will do.)

If you have a discrete array of samples, and you would like to know the CDF of the sample, then you can just sort the array. If you look at the sorted result, you'll realize that the smallest value represents 0% , and largest value represents 100 %. If you want to know the value at 50 % of the distribution, just look at the array element which is in the middle of the sorted array.

Let us have a closer look at this with a simple example:

import matplotlib.pyplot as plt

import numpy as np

# create some randomly ddistributed data:

data = np.random.randn(10000)

# sort the data:

data_sorted = np.sort(data)

# calculate the proportional values of samples

p = 1. * np.arange(len(data)) / (len(data) - 1)

# plot the sorted data:

fig = figure()

ax1 = fig.add_subplot(121)

ax1.plot(p, data_sorted)

ax1.set_xlabel('$p$')

ax1.set_ylabel('$x$')

ax2 = fig.add_subplot(122)

ax2.plot(data_sorted, p)

ax2.set_xlabel('$x$')

ax2.set_ylabel('$p$')

This gives the following plot where the right-hand-side plot is the traditional cumulative distribution function. It should reflect the CDF of the process behind the points, but naturally it is not the as long as the number of points is finite.

This function is easy to invert, and it depends on your application which form you need.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值