def test1DNormalDist(self): # prepare data U = dists.Normal(1.85, .3, 0, 3) trainSamples = np.array([U.rvs(500)]).T testSamples = np.array([U.rvs(1000)]).T # build parameter set dist = KDEDist(trainSamples, kernelType=KernelType_GAUSSIAN, bandwidthOptimizationType= BandwidthOptimizationType_MAXIMUMLIKELIHOOD, bounds=U.getBounds()) # fig = plt.figure() # plotDensity1d(U) # plotDensity1d(dist) print("quad = %s" % (quad(lambda x: dist.pdf([x]), 0, 3), )) print("mean = %g ~ %g" % (U.mean(), dist.mean())) print("var = %g ~ %g" % (U.var(), dist.var())) print("KL = %g" % U.klDivergence(dist, testSamples, testSamples)) print("CE = %g" % dist.crossEntropy(testSamples)) print("MSE = %g" % dist.l2error(U, testSamples, testSamples)) plt.show()
def test2DNormalMoments(self): mean = 0 var = 0.5 U = dists.J([dists.Normal(mean, var, -2, 2), dists.Normal(mean, var, -2, 2)]) trainSamples = U.rvs(10000) dist = KDEDist(trainSamples) # ----------------------------------------------- self.assertTrue(np.abs(U.mean() - dist.mean()) < 1e-2, "KDE mean wrong") self.assertTrue(np.abs(U.var() - dist.var()) < 1e-2, "KDE variance wrong")