一维核密度估计的应用一:推断总体服从的分布

来源:哔哩哔哩 时间:2023-08-04 20:34:39


(资料图)

#此代码是我手动搭建的

#此代码未使用核密度估计的专用工具包(KernelDensity)

#如需将此代码放在自己个人社交平台,请给个引用"B站 耿大哥讲算法" 

import numpy as npfrom import aucimport as pltimport matplotlib;("font",family='Microsoft YaHei')#样本X=[93,75,83,93,91,85,84,82,77,76,77,95,94,89,91,88,84,83,96,81,   79,97,78,75,67,69,68,84,83,81,75,66,85,70,94,84,83,82,80,78,   74,73,76,70,86,76,89,90,71,66,86,73,80,94,79,78,77,63,53,55]#计算最优带宽h=(X,ddof=1)*(4/(3*len(X)))**#计算数学期望print('E(X)='+str('%.2f'%(X)))#定义高斯核函数def K(x,xi):return 1/(2*)***(-((x-xi)/h)**2/2)#计算方差print('D(X)='+str('%.2f'%(h**2+(X,ddof=0))))'''#定义余弦核函数def k(x,xi):    if b-h<=a<=b+h:return /4*(/2*((x-xi)/h))    else:return 0#计算方差print('D(X)='+str('%.2f'%((1-8/**2)*h**2+(X,ddof=0))))#定义均匀核函数def k(x,xi):    if xi-h<=x<=xi+h:return    else:return 0#计算方差print('D(X)='+str('%.2f'%(h**2/3+(X,ddof=0))))'''#定义高斯核密度估计函数def f(x):return sum(K(x,xi) for xi in X)/(len(X)*h)#计算样本属于区间[a,b]概率def P(a,b):   x=(a,b,)   y=[f(i) for i in x]   return auc(x,y)print('P(80≤X≤100)='+str('%.3f'%P(80,100)))#绘制高斯核密度估计函数的图像x=(40,120,)y=[f(i) for i in x]('h='+str('%.3f'%h))(x,y,facecolor='green',alpha=)(x,y,'r-')('x')('核密度估计函数f(x)')()

关键词:

推荐内容

Copyright 2000-2021 by www.jiaoyu.dzwindows.com all rights reserved

备案号:皖ICP备2022009963号-12

邮箱 : 39 60 29 14 2@qq.com