密度不等于1

2024-04-27 13:08:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我惊讶地发现概率密度之和不等于1。是否有调整使其等于1

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter
plt.style.use('seaborn-deep')

#input file is a flat file that contains portfolio holdings and characteristics
input_file = r'\\CP\file.xls'

df = pd.read_excel(input_file,header=6)

#number of lines in Fund is 123
df_Fund=df[(df['Port. Weight']>0)]

#number of lines in Bench is 214
df_Bench=df[(df['Bench. Weight']>0)]

#Delta distribution
x = df_Fund['Delta']
y = df_Bench['Delta']

plt.hist([x,y],bins=10, density=True, range=(0,100), label=['Fund','Bench'])
plt.legend(loc='upper right')
plt.gca().yaxis.set_major_formatter(PercentFormatter(1))
plt.title='Delta Breakdown'
plt.show()

图表:

screenshot of graph


Tags: ofimportnumberdfinputmatplotlibisas
2条回答

如果你想让它和为一,那么你就除以总和

例如,如果将某些组件求和,则求和为一个数X

x_0 + x_1 + x_2 + ... = X

所以如果你这样做的话,你就可以把每个成分除以你得到的总量

(x_0/X) + (x_1/X) + (x_2/X) + ... = (x_0+x_1+x_2...)/X = X/X = 1

这就是对任何分布进行正态化的方法(如果分布是连续的,那么和就变成了整数)

希望这能有所帮助

documentation

density bool, default: False

If True, draw and return a probability density: each bin will >display the bin's raw count divided by the total number of counts >and the bin width (density = counts / (sum(counts) * >np.diff(bins))), so that the area under the histogram integrates to >1 (np.sum(density * np.diff(bins)) == 1).

If stacked is also True, the sum of the histograms is normalized to 1.

密度也不受料仓宽度的影响。由于它看起来像一个大约10的binning,我希望您的数据总和为0.1,而不是1

解释图表的方法是“对于50到60之间的每x,概率为1.75%”

因此,为了将其“调整”为1,您可以使用1的bin大小

bins=range(100)

或者——正如其他答案中提到的那样——将概率标准化

相关问题 更多 >