python对数正态分布函数_将对数正态分布的拟合PDF缩放到python中的histrogram
您要求的是预期直方图的图表 .
假设[a,b]是直方图的x个区间之一 . 对于大小为n的随机样本,间隔中的预期样本数为
(cdf(b) - cdf(a))*n
其中cdf(x)是累积分布函数 . 要绘制预期的直方图,您将计算每个bin的值 .
下面的脚本显示了在matplotlib直方图上绘制预期直方图的一种方法 . 它生成这个图:
import numpy as np
import scipy.stats
import matplotlib.pyplot as plt
# Generate log-normal distributed set of samples
np.random.seed(1234)
samples = np.random.lognormal(mean=1., sigma=.4, size=10000)
# Make a fit to the samples.
shape, loc, scale = scipy.stats.lognorm.fit(samples, floc=0)
# Create the histogram plot using matplotlib. The first two values in
# the tuple returned by hist are the number of samples in each bin and
# the values of the histogram's bin edges. counts has length num_bins,
# and edges has length num_bins + 1.
num_bins = 50
clr = '#FFE090'
counts, edges, patches = plt.hist(samples, bins=num_bins, color=clr, label='Sample histogram')
# Create an array of length num_bins containing the center of each bin.
centers = 0.5*(edges[:-1] + edges[1:])
# Compute the CDF at the edges. Then prob, the array of differences,
# is the probability of a sample being in the corresponding bin.
cdf = scipy.stats.lognorm.cdf(edges, shape, loc=loc, scale=scale)
prob = np.diff(cdf)
plt.plot(centers, samples.size*prob, 'k-', linewidth=2, label='Expected histogram')
# prob can also be approximated using the PDF at the centers multiplied
# by the width of the bin:
# p = scipy.stats.lognorm.pdf(centers, shape, loc=loc, scale=scale)
# prob = p*(edges[1] - edges[0])
# plt.plot(centers, samples.size*prob, 'r')
plt.legend()
plt.show()
注意:由于PDF是CDF的衍生物,您可以将cdf(b) - cdf(a)的近似值写为
cdf(b) - cdf(a) = pdf(m)*(b - a)
其中m是,例如,区间[a,b]的中点 . 然后,您提出的确切问题的答案是通过将PDF乘以样本大小和直方图区域宽度来缩放PDF . 脚本中有一些注释掉的代码,显示如何使用缩放的PDF绘制预期的直方图 . 但由于CDF也可用于对数正态分布,因此您也可以使用它 .
python对数正态分布函数_将对数正态分布的拟合PDF缩放到python中的histrogram相关推荐
- python回测函数_【手把手教你】动量指标的Python量化回测
我认为投资专业的学生只需要两门教授得当的课堂:如何评估一家公司,以及如何考虑市场价格.--巴菲特 01 引言 本文延续"手把手教你使用Python的TA-Lib"系列,以资金流量指 ...
- python为什么closed_为什么python类的函数被调用两次[关闭](Why a function of python class is called twice [closed])...
为什么python类的函数被调用两次[关闭](Why a function of python class is called twice [closed]) 我遇到了两次调用的python类函数的问 ...
- python 累积正态分布函数_为什么机器学习都围绕正态分布进行讨论?
作者 | Farhad Malik 译者 | Monanfei 责编 | 夕颜 出品 | AI科技大本营(ID: rgznai100) 为什么正态分布如此特殊?为什么大量数据科学和机器学习的文章都围绕 ...
- python绘制正态分布函数_学好正态分布有多重要?
作者 | Farhad Malik 译者 | Monanfei 责编 | 夕颜 出品 | AI科技大本营(ID: rgznai100) 为什么正态分布如此特殊?为什么大量数据科学和机器学习的文章都围绕 ...
- python随机生成正态分布函数_使用python实现正态分布函数
用来计算连续变量的发生率,说的很抽象,简单说就是单独拿出来没什么太大用,但并不是说这个没什么用,相反这个太重要了,这玩意能让你看清世界的真相 先看个图,像这样的线性就是正太分布 这是一个标准的正态分布 ...
- python scale标准化函数_归一化与标准化
(一) 特征缩放定义特征缩放是用来统一资料中的自变项或特征范围的方法,在资料处理中,通常会被使用在资料前处理这个步骤.因为在原始的资料中,各变数的范围大不相同. (二) 特征缩放的目的对于大多数的机器 ...
- python列表平均值函数_如何计算列表的平均值-统计信息和Python的均值函数详细解释
python列表平均值函数 Mathematics and programming go hand in hand. If you are a programmer, at some point yo ...
- 两边同时取对数求复合函数_取对数求导法的例题 取对数求导法
首先 自然对数 就是对e求对数 即ln 对数运算有几个规律 ln(x*y)=lnx lny ln(x/y)=lnx-lny ln(x^y)=y*lnx 这样一来 你应该就明白了吧 lny=ln{[(x ...
- python的random函数_关于random()的详细介绍
这篇文章主要介绍了Python随机生成数模块random使用实例,本文直接给出示例代码,需要的朋友可以参考下代码如下:#!/usr/bin/env python#coding=utf-8import ...
- python 使用异常函数_您如何测试Python函数引发异常?
python 使用异常函数 This article elaborates on how to implement a test case for a function that raises an ...
最新文章
- 大学计算机英语专业感想感知,非英语专业大学生英语感知学习风格研究
- ***不是已知元素 原因可能是网站中存在编译错误
- 外挂安装eclipse插件
- 利用ASP.NET MVC 的默认类型绑定器---将Jquery datatables中的数据强类型绑定到实体类中
- 解救小哈(dfs或bfs)
- [译] 你的站点如你所想的移动友好吗?
- 理解 __doPostBack--1
- 身份证OCR识别SDK
- Python 输出到文件两种方式
- oracle设计案例,Oracle课程设计案例精编
- 夏令营导师自荐信计算机专业,北大夏令营的自荐信
- python设置端口控制ie浏览器_[Python爬虫] 之二:Selenium 调用IEDriverServer打开IE浏览器安装配置...
- postgreSql数据库学习使用
- 记一次Kafka warning排查过程
- Qt5 学习之路及嵌入式开发教程21:QML基础
- 数字驱动,智能发展 | 的卢深视三维全栈技术亮相宁波智博会
- 四大天王 -- ContentProvider
- 计算机使用的显示器有哪些重要参数,电脑显示器知识:显示器参数主要看什么?(2)...
- android sd卡列目录文件_(科普)详解Android系统SD卡各类文件夹名称
- Web Sockets