python计算累计收益率的函数_在Python中计算累积密度函数的导数

Is it the case that the exact derivative of a cumulative density function is the probability density function (PDF)? I am calculating the derivative using the numpy.diff(), is this correct? See below code below:

import scipy.stats as s

import matplotlib.pyplot as plt

import numpy as np

wei = s.weibull_min(2, 0, 2) # shape, loc, scale - creates weibull object

sample = wei.rvs(1000)

shape, loc, scale = s.weibull_min.fit(sample, floc=0)

x = np.linspace(np.min(sample), np.max(sample))

plt.hist(sample, normed=True, fc="none", ec="grey", label="frequency")

plt.plot(x, wei.cdf(x), label="cdf")

plt.plot(x, wei.pdf(x), label="pdf")

plt.plot(x[1:], np.diff(wei.cdf(x)), label="derivative")

plt.legend(loc=1)

plt.show()

If so, how do I scale the derivative to be equivalent to the PDF?

解决方案

The derivative of the CDF is the PDF.

Here is an approximation of the derivative of the CDF:

dx = x[1]-x[0]

deriv = np.diff(wei.cdf(x))/dx

import scipy.stats as s

import matplotlib.pyplot as plt

import numpy as np

wei = s.weibull_min(2, 0, 2) # shape, loc, scale - creates weibull object

sample = wei.rvs(1000)

shape, loc, scale = s.weibull_min.fit(sample, floc=0)

x = np.linspace(np.min(sample), np.max(sample))

dx = x[1]-x[0]

deriv = np.diff(wei.cdf(x))/dx

plt.hist(sample, normed=True, fc="none", ec="grey", label="frequency")

plt.plot(x, wei.cdf(x), label="cdf")

plt.plot(x, wei.pdf(x), label="pdf")

plt.plot(x[1:]-dx/2, deriv, label="derivative")

plt.legend(loc=1)

plt.show()

yields

Note that the x-locations associated with deriv have been shifted

by dx/2 so the approximation is centered between the values used to compute it.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值