首页 > 解决方案 > 密度总和不等于 1

问题描述

我惊讶地发现概率密度之和不等于 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()

图形:

图表截图

标签: pythonpandasdataframeprobability-density

解决方案


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

例如,如果您将一些组件和总和求和为一个数字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

这就是您对任何分布进行归一化的方式(如果分布是连续的,则总和变为积分)

希望这会有所帮助


推荐阅读