一元和多元核密度快速稳健估计工具
fastkde的Python项目详细描述
软件概述
fastkde计算任意维数的核密度估计 数据;它使用最近开发的kde做得如此迅速和可靠 技术。它的统计技巧和 科学状态的“r”kde包,它能做到10000次 对于二元数据更快(对于更高的 维度)。
使用此方法时,请引用下列论文:
示例用法:
对于标准pdf
#!python import numpy as np from fastkde import fastKDE import pylab as PP #Generate two random variables dataset (representing 100000 pairs of datapoints) N = 2e5 var1 = 50*np.random.normal(size=N) + 0.1 var2 = 0.01*np.random.normal(size=N) - 300 #Do the self-consistent density estimate myPDF,axes = fastKDE.pdf(var1,var2) #Extract the axes from the axis list v1,v2 = axes #Plot contours of the PDF should be a set of concentric ellipsoids centered on #(0.1, -300) Comparitively, the y axis range should be tiny and the x axis range #should be large PP.contour(v1,v2,myPDF) PP.show()
对于条件pdf
下面的代码从一个非平凡的关节生成示例 分布
fromfastkdeimportfastKDEimportpylabasPPimportnumpyasnp#***************************# Generate random samples#***************************# Stochastically sample from the function underlyingFunction() (a sigmoid):# sample the absicissa values from a gamma distribution# relate the ordinate values to the sample absicissa values and add# noise from a normal distribution#Set the number of samplesnumSamples=int(1e6)#Define a sigmoid functiondefunderlyingFunction(x,x0=305,y0=200,yrange=4):return(yrange/2)*np.tanh(x-x0)+y0xp1,xp2,xmid=5,2,305#Set gamma distribution parametersyp1,yp2=0,12#Set normal distribution parameters (mean and std)#Generate random samples of X from the gamma distributionx=-(np.random.gamma(xp1,xp2,int(numSamples))-xp1*xp2)+xmid#Generate random samples of y from x and add normally distributed noisey=underlyingFunction(x)+np.random.normal(loc=yp1,scale=yp2,size=numSamples)
现在我们有了x,y样本,下面的代码计算
有条件的
下图显示结果: 有条件PDF#***************************# Calculate the conditional#***************************pOfYGivenX,axes=fastKDE.conditional(y,x)
#***************************# Plot the conditional#***************************fig,axs=PP.subplots(1,2,figsize=(10,5))#Plot a scatter plot of the incoming dataaxs[0].plot(x,y,'k.',alpha=0.1)axs[0].set_title('Original (x,y) data')#Set axis labelsforiin(0,1):axs[i].set_xlabel('x')axs[i].set_ylabel('y')#Draw a contour plot of the conditionalaxs[1].contourf(axes[0],axes[1],pOfYGivenX,64)#Overplot the original underlying relationshipaxs[1].plot(axes[0],underlyingFunction(axes[0]),linewidth=3,linestyle='--',alpha=0.5)axs[1].set_title('P(y|x)')#Set axis limits to be the samexlim=[np.amin(axes[0]),np.amax(axes[0])]ylim=[np.amin(axes[1]),np.amax(axes[1])]axs[1].set_xlim(xlim)axs[1].set_ylim(ylim)axs[0].set_xlim(xlim)axs[0].set_ylim(ylim)fig.tight_layout()PP.savefig('conditional_demo.png')PP.show()
我该怎么做?
标准的python构建:python setup.py install
或
pip install fastkde
下载源代码
请联系travis a.o'brienTAOBrien@lbl.gov获取最新的 源的版本。
安装先决条件
此代码需要以下软件:
- python>;=2.7.3
- numpy>;=1.7
- scipy
- 赛顿
版权信息
LAWRENCE BERKELEY NATIONAL LABORATORY RESEARCH & DEVELOPMENT, NON-COMMERCIAL USE ONLY, LICENSE Copyright (c) 2015, The Regents of the University of California, through Lawrence Berkeley National Laboratory (subject to receipt of any required approvals from the U.S. Dept. of Energy). All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: (1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. (2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. (3) Neither the name of the University of California, Lawrence Berkeley National Laboratory, U.S. Dept. of Energy nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. (4) Use of the software, in source or binary form is FOR RESEARCH & DEVELOPMENT, NON-COMMERCIAL USE, PURPOSES ONLY. All commercial use rights for the software are hereby reserved. A separate commercial use license is available from Lawrence Berkeley National Laboratory. (5) In the event you create any bug fixes, patches, upgrades, updates, modifications, derivative works or enhancements to the source code or binary code of the software ("Enhancements") you hereby grant The Regents of the University of California and the U.S. Government a paid-up, non-exclusive, irrevocable, worldwide license in the Enhancements to reproduce, prepare derivative works, distribute copies to the public, perform publicly and display publicly, and to permit others to do so. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *** Copyright Notice *** FastKDE v1.0, Copyright (c) 2015, The Regents of the University of California, through Lawrence Berkeley National Laboratory (subject to receipt of any required approvals from the U.S. Dept. of Energy). All rights reserved. If you have questions about your rights to use or distribute this software, please contact Berkeley Lab's Innovation & Partnerships Office at IPO@lbl.gov. NOTICE. This software was developed under funding from the U.S. Department of Energy. As such, the U.S. Government has been granted for itself and others acting on its behalf a paid-up, nonexclusive, irrevocable, worldwide license in the Software to reproduce, prepare derivative works, and perform publicly and display publicly. Beginning five (5) years after the date permission to assert copyright is obtained from the U.S. Department of Energy, and subject to any subsequent five (5) year renewals, the U.S. Government is granted for itself and others acting on its behalf a paid-up, nonexclusive, irrevocable, worldwide license in the Software to reproduce, prepare derivative works, distribute copies to the public, perform publicly and display publicly, and to permit others to do so. ****************************