def test3(self): print("===== formatter test3 ===========================") arr = numpy.asarray([k for k in range(120)], dtype=float) arr = arr.reshape((10, 12)) fmtinit(max=4) print(fmt(arr)) print(fmt(arr, max=None)) print(fmt(arr, tail=1)) print(fmt(arr, tail=3)) print(fmt(arr[1, :], tail=1)) print(fmt(arr[1, :], tail=3)) print(fmt(arr[1, :], max=8, tail=2, linelength=200)) print(fmt(arr[1, :], max=8, tail=3, linelength=200)) print(fmt(arr[1, :], max=8, tail=4, linelength=200)) print(fmt(arr[1, :], max=8, tail=5, linelength=200))
def testcr2(self): print("===== cyclic results 2 ================") mdl = PhaseModel(cyclic=1.0) x = numpy.array([0, 1, 9, 10], dtype=float) y = numpy.array([1, 0, 10, 9], dtype=float) problem = Problem(model=mdl, xdata=x, ydata=y) Tools.printclass(problem) r0 = numpy.linspace(-0.7, 1.5, 12) print(fmt(r0, max=None)) r1 = problem.cyclicCorrection(r0) print(fmt(r1, max=None)) self.assertTrue(all(numpy.abs(r1) <= 0.5))
def makeModel(self, nin, ndout, ndhid, npt): x, y = self.makeData(nin, npt, ndout) m1 = SoftMaxModel(ndim=nin, ndout=ndhid, offset=True) m1.setLimits(-10, 10) p1 = numpy.random.random(m1.npars) * 20 - 10 print(m1.npars, fmt(m1.result(x, p1))) m2 = SoftMaxModel(ndim=ndhid, ndout=ndout, offset=True) m2.setLimits(-10, 10) m = m1 | m2 pp = numpy.random.random(m.npars) * 20 - 10 print(m.npars, fmt(m.result(x, pp))) problem = ClassicProblem(model=m, xdata=x, ydata=y) return problem
def testcr4(self): print("===== cyclic results 4 ================") mdl = PhaseModel(cyclic={0: 1.0}) x = numpy.array([0, 1, 9, 10], dtype=float) y = numpy.array([1, 0, 10, 9], dtype=float) problem = Problem(model=mdl, xdata=x, ydata=y) Tools.printclass(problem) r0 = numpy.linspace(-0.8, 1.5, 12).reshape(2, 6).transpose() print(fmt(r0, max=None)) r1 = problem.cyclicCorrection(r0) print(fmt(r1, max=None)) assertAAE(r0[:, 1], r1[:, 1]) self.assertTrue(all(numpy.abs(r1[:, 0]) < 0.5))
def dotest5(self, step=10): print("====test5====step=%d========================" % step) plot = self.doplot nn = 10 x = numpy.linspace(0, 2, nn, dtype=float) ym = 0.3 + 0.5 * x nf = 0.1 numpy.random.seed(2345) noise = numpy.random.randn(nn) y = ym + nf * noise limits = [-1, 2] model = PolynomialModel(1) model.setLimits(lowLimits=limits[0], highLimits=limits[1]) s = 0.0 s2 = 0.0 mr = 10 for k in range(mr): dis = GaussErrorDistribution(scale=0.5) ns = PhantomSampler(x, model, y, distribution=dis, verbose=0, seed=k + 12 * step) ns.step = step logE = ns.sample() par2 = ns.parameters logz2 = ns.logZ dlz2 = ns.logZprecision s += logz2 s2 += logz2 * logz2 print("pars ", fmt(par2), " logZ ", fmt(logz2), " +- ", fmt(dlz2)) logz = s / mr dlz = math.sqrt(s2 / mr - logz * logz) print("Average ", fmt(logz), " +- ", fmt(dlz))
def test2( self ): print( " Test 2 DecisionTreeModel" ) ND = 10 NP = 100 m = DecisionTreeModel( ndim=ND, kdim=[0,1,2,3,4,5,6], depth=3 ) print( m ) numpy.random.seed( 12345 ) xdata = numpy.random.rand( NP, ND ) print( fmt( xdata ) ) par0 = numpy.arange( m.npars, dtype=float ) + 1 res = m.result( xdata, par0 ) part = m.partial( xdata, par0 ) for k in range( NP ) : print( fmt( res[k] ), fmt( part[k,:], max=None ) ) self.assertTrue( part[k,int(res[k]-0.99)] == 1 ) self.assertTrue( numpy.sum( part[k,:] ) == 1 )
def test1Model11( self ): print( " Test SplinesDynamicModel shrink" ) xx = numpy.linspace( 0, 10, 501, dtype=float ) kn1 = numpy.linspace( 0, 10, 16, dtype=float ) m1 = BasicSplinesModel( knots=kn1 ) p1 = numpy.zeros( 18, dtype=float ) p1[7:9] = 1.0 kn2 = numpy.append( numpy.append( kn1[:7], [5.0] ), kn1[9:] ) m2 = BasicSplinesModel( knots=kn2 ) p2 = numpy.zeros( 17, dtype=float ) p2[7] = 1.4 print( fmt( kn1, max=None ) ) print( fmt( kn2, max=None ) ) if self.doplot : plt.plot( xx, m1.result( xx, p1 ) ) plt.plot( xx, m2.result( xx, p2 ) ) plt.show()
def test4(self): print("\n SalesmanProblem Test User distance\n") np = 10 pars = numpy.arange(np * np, dtype=int) problem = self.initProblem(np=np, distance=self.square) print(problem) mdis = problem.minimumDistance() print("mindis ", mdis) dis = problem.result(pars) sdis = numpy.sum(dis) print(fmt(dis, max=None), fmt(sdis)) self.assertAlmostEqual(sdis, 990, 6) problem = self.initProblem(np=np, distance=self.square, scale=3) dis = problem.result(pars) sdis = numpy.sum(dis) print(fmt(dis, max=None), fmt(sdis)) self.assertAlmostEqual(sdis, 990 / 3, 6)
def test1(self): print("\n SalesmanProblem Test Manhattan\n") np = 10 pars = numpy.arange(np * np, dtype=int) problem = self.initProblem(np=np, distance="manhattan") print(problem) mdis = problem.minimumDistance() print("mindis ", mdis) dis = problem.result(pars) sdis = numpy.sum(dis) print(fmt(dis, max=None), fmt(sdis)) self.assertAlmostEqual(sdis, 198, 6) problem = self.initProblem(np=np, distance="manhattan", scale=3) dis = problem.result(pars) sdis = numpy.sum(dis) print(fmt(dis, max=None), fmt(sdis)) self.assertAlmostEqual(sdis, 198 / 3, 6)
def test6(self): npt = 401 t, y = self.makeData() t = numpy.linspace(0, 100, npt, dtype=float) ym = 2 * numpy.sin(2 * math.pi * numpy.exp(t / 60) + 1) * numpy.exp( -0.02 * t) y = numpy.random.seed(12345) y = ym + numpy.random.randn(npt) * 0.05 kn = [0, 25, 50, 60, 70, 80, 90, 100] kn = [ 0.000, 17.254, 33.456, 42.755, 61.243, 62.538, 74.369, 89.854, 93.388, 100.000 ] m = BasicSplinesModel(knots=kn) ftr = Fitter(t, m, fixedScale=0.05) par = ftr.fit(y) lz0 = ftr.getLogZ(limits=[-10, +10]) print(fmt(par, max=None), fmt(lz0)) m = BasicSplinesModel(knots=kn) ftr = Fitter(t, m) par = ftr.fit(y) lz0 = ftr.getLogZ(limits=[-10, +10]) print(fmt(par, max=None), fmt(lz0)) m = BasicSplinesModel(knots=kn) ftr = Fitter(t, m) par = ftr.fit(y) lz0 = ftr.getLogZ(limits=[-10, +10], noiseLimits=[0.01, 1]) print(fmt(par, max=None), fmt(lz0))
def plot1(self): x = numpy.linspace(0, 10, 101, dtype=float) y = numpy.sin(x) kn1 = numpy.linspace(0, 10, 11, dtype=float) sm1 = BasicSplinesModel(knots=kn1) ftr = Fitter(x, sm1) par1 = ftr.fit(y) print(fmt(par1, max=None)) plt.plot(x, y, 'k.') plt.plot(x, sm1.result(x, par1)) plt.show()
def bstest6(self, kn, order=3): print("==== test 6 ====================") x = numpy.linspace(min(kn), max(kn), 101, dtype=float) n = 0 cc = ['k-', 'b-', 'r-', 'g-', 'c-', 'm-'] sm = BasicSplinesModel(knots=kn, order=order) bm = BSplinesModel(knots=kn, order=order) par = numpy.ones(bm.npars, dtype=float) ysm = sm.result(x, par) pts = sm.partial(x, par) ybm = bm.result(x, par) ptb = bm.partial(x, par) print(fmt(numpy.sum(pts, 0), max=None)) print(fmt(numpy.sum(ptb, 0), max=None)) if self.doplot: for k in kn: plt.plot([k, k], [0, 1], 'k:') plt.plot(x, ybm, 'b-') plt.plot(x, ysm + 1.1, 'k-') for i in range(bm.npars): # plt.plot( x, ptb[:,i] - pts[:,i], cc[2] ) plt.plot(x, ptb[:, i], cc[2]) plt.plot(x, pts[:, i] + 1.1, cc[3]) plt.show() assertAAE(ysm, ybm) if order > 0: assertAAE(pts, ptb)
def test4(self): print("======= Neural Net Utilities test 4 ========================") con = NeuralNetUtilities.ConnectWithBias(ndim=2, ndout=3) atn = NeuralNetUtilities.Logistic() print(con.ndim, con.ndout, con.nraster) self.assertTrue(con.nraster == 6) xx = numpy.array([[1, 2], [2, 3], [3, 4], [4, 5]], dtype=float) par = numpy.arange(9, dtype=float) + 1 print(fmt(xx)) print(fmt(par, max=None)) res = atn.result(con.result(xx, par), par) print("result ", res.shape) print(fmt(res)) partial = atn.derivative(con.partial(xx, par), par) print("partial ", partial.shape) print(fmt(partial, max=None))
def stdstarttest(self, mystarteng, np=4, nwalker=10): problem = self.initProblem(np=np) errdis = DistanceCostFunction() n2 = np * np pars = numpy.arange(n2, dtype=int) fi = numpy.arange(n2, dtype=int) sl = WalkerList(problem, nwalker, pars, fi) steng = mystarteng(sl, errdis, verbose=5) for wlkr in steng.walkers: logL = errdis.logLikelihood(problem, wlkr.allpars) wid = wlkr.id print("Walker :", wid, wlkr.allpars, fmt(logL)) steng.execute(wid, -math.inf) nwlk = steng.walkers[wid] print(" ", nwlk.allpars, fmt(nwlk.logL)) psort = numpy.sort(nwlk.allpars) assertAE(psort, pars)
def test4(self): print("******ASTROPY MODEL 4***********************") gm = modeling.models.Gaussian1D() # print( gm.__class__ ) # print( gm ) apm = AstropyModel(gm) apm.setPrior(0, ExponentialPrior(scale=10)) apm.setPrior(1, UniformPrior(limits=[-10, 10])) apm.setPrior(2, JeffreysPrior(limits=[0.1, 10])) bfm = PolynomialModel(1) bfm.setPrior(0, UniformPrior(limits=[-5, 5])) m = apm + bfm p = numpy.asarray([1.2, -0.1, 30, 1.0, 0.3], dtype=float) N = 201 x = numpy.arange(N, dtype=float) / 25 - 2 y = (x - 0.70) / 0.4 y *= -0.5 * y y = 8.0 * numpy.exp(y) y += 0.2 * x + 1.0 numpy.random.seed(13456) y += 0.5 * numpy.random.randn(N) ns = NestedSampler(x, m, y) ns.distribution.setLimits([0.01, 10]) ns.verbose = 2 evi = ns.sample(plot=self.doplot) print("NS pars ", fmt(ns.parameters)) print("NS stdv ", fmt(ns.stdevs)) print("NS scal ", fmt(ns.scale))
def test2(self): print("\n SalesmanProblem Test Euclidic\n") np = 10 pars = numpy.arange(np * np, dtype=int) problem = self.initProblem(np=np) print(problem) mdis = problem.minimumDistance() print("mindis ", mdis) dis = problem.result(pars) sdis = numpy.sum(dis) print(fmt(dis, max=None), fmt(sdis)) print(sdis, (90 + 9 * 9.05538514 + 12.72792206)) self.assertAlmostEqual(sdis, (90 + 9 * 9.05538514 + 12.72792206), 6) problem = self.initProblem(np=np, weights=True) dis = problem.result(pars) sdis = numpy.sum(dis) print(fmt(dis, max=None), fmt(sdis)) print(sdis, 2 * (90 + 9 * 9.05538514 + 12.72792206)) self.assertAlmostEqual(sdis, 2 * (90 + 9 * 9.05538514 + 12.72792206), 6)
def histo(self, x, pr, fun=None): print(pr) print(fmt(x)) print(fmt(fun[0])) print(fmt(fun[1])) if self.doplot: num_bins = 80 # the histogram of the data n, bins, patches = plt.hist(x, num_bins, facecolor='green', alpha=0.5) # add a 'best fit' line if fun is not None: plt.plot(fun[0], fun[1], 'r--') plt.xlabel('Error') plt.ylabel('Probability') plt.title('Histogram of ' + str(pr)) # Tweak spacing to prevent clipping of ylabel plt.subplots_adjust(left=0.15) plt.show()
def testBernoulliErrorDistribution(self): print( "====== Test Bernoulli Error Distribution ======================") poly = LogisticModel(fixed={0: 1}) param = numpy.asarray([0.5, 1.0], dtype=float) data = numpy.asarray([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1], dtype=int) data = numpy.asarray([1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0], dtype=int) print("x : ", fmt(self.x, max=None)) print("Data : ", fmt(data, max=None)) print("yfit : ", fmt(poly.result(self.x, param), max=None)) problem = ClassicProblem(model=poly, xdata=self.x, ydata=data) ped = BernoulliErrorDistribution() print(str(ped)) self.assertFalse(ped.acceptWeight()) logL = ped.logLikelihood(problem, param) print("logL = %8.3f" % (logL)) logL = ped.logLikelihood(problem, param) mok = problem.result(param) altL = ped.logLikelihood_alt(problem, param) lLdata = ped.logLdata(problem, param) print("Ldata: ", fmt(lLdata, max=None)) logL0 = numpy.sum(lLdata) print("logL = %8.3f ldata %8.3f alt %8.3f" % (logL, logL0, altL)) assertAAE(logL, altL) assertAAE(logL, logL0) print("logL = %8.3f %8.3f" % (logL, altL)) assertAAE(logL, altL) fitIndex = [0, 1] dL = ped.partialLogL(problem, param, fitIndex) nL = ped.numPartialLogL(problem, param, fitIndex) aL = ped.partialLogL_alt(problem, param, fitIndex) print("partial = ", dL) print("altpart = ", aL) print("numpart = ", nL) assertAAE(dL, nL, 5) print(" ", fmt([4.0 / (k + 1) for k in range(9)], max=None)) for i in range(10): param = numpy.asarray([i - 4, 1], dtype=float) print(fmt(param[0]), " : ", end="") for k in range(9): param[1] = 4.0 / (k + 1) print(" %8.3f" % ped.logLikelihood(problem, param), end="") print("")
def dofit(self, ns, pp, plot=False): logE = ns.sample(plot=plot) par = ns.parameters std = ns.standardDeviations mlp = ns.samples.maxLikelihoodParameters scale = ns.scale scdev = ns.stdevScale print("truth ", fma(pp)) print("par ", fma(par)) print("st dev ", fma(std)) print("ML par ", fma(mlp)) print("scale ", fmt(scale)) # ns.report( ) self.assertTrue(all(numpy.abs(par - mlp) < 2 * std)) self.assertTrue(all(numpy.abs(par - pp) < 2 * std))
def test2(self): print("===== formatter test2 ===========================") arr = numpy.asarray([k for k in range(36)], dtype=float) arr = arr.reshape((3, 12)) fmtinit(max=None) print(fmt(arr, max=None)) fmtinit(linelength=60) print(fmt(arr, max=None)) fmtinit(linelength=80, format={"float64": " %7.2f"}) print(fmt(arr, max=None)) print("arr", fmt(arr, indent=4, format=" %7.2f")) print(fmt(arr, max=2)) alist = [1, 2, 3, 4, 5] print(fmt(alist)) self.assertTrue(isinstance(alist, list)) print(fmt(3), fmt(3.4))
def test1(self): print("===== formatter test1 ===========================") arr = numpy.asarray([k for k in range(120)], dtype=float) arr = arr.reshape((3, 40)) print(fmt(arr, max=None)) print("arr", fmt(arr[1], indent=4, format=" %7.2f", max=20)) print(fmt(arr.reshape((3, 4, 10)), max=2)) alist = [1, 2, 3, 4, 5] print(fmt(alist)) self.assertTrue(isinstance(alist, list)) print(fmt(3), fmt(3.4))
def XXXtest6(self): print("====test6 CategoricalProblem============================") x = numpy.array([[1, 4, 2, 1, 3, 2, 4, 2, 3, 2, 1, 2, 3]], dtype=float).transpose() y = numpy.array([1, 2, 2, 3, 3, 2, 1, 1, 2, 3, 3, 2, 2]) # w = numpy.array( [0.9,1.0,1.1,1.2], dtype=float ) mdl = DecisionTreeModel(ndim=1, depth=2, kdim=[0, 0, 0], split=0.5, itypes=[0]) p = [0.1, 0.5, 0.6, 0.4, 0.2, 0.3, 0.7, 0.3, 0.7, 0.2, 0.1, 0.3] print(mdl.fullName()) problem = CategoricalProblem(model=mdl, xdata=x, ydata=y) print(problem.ncateg, problem.npars, fmt(p, max=None)) print(fmt(problem.ydata, max=None)) print(fmt(x, max=None)) print(fmt(mdl.sortXdata(x), max=None)) lst = [k for k in range(len(p))] ym = problem.result(p) yl = problem.result(lst) dy = problem.partial(p) print(fmt(ym, max=None)) print(fmt(yl, max=None)) print(fmt(dy, max=None)) res = problem.residuals(p) print(fmt(res, max=None))
def test5a(self): print("====test5a==discard===================") plot = self.doplot nn = 10 x = numpy.linspace(0, 2, nn, dtype=float) ym = 0.3 + 0.5 * x nf = 0.1 numpy.random.seed(2345) noise = numpy.random.randn(nn) y = ym + nf * noise limits = [-1, 2] model = PolynomialModel(1) model.setLimits(lowLimits=limits[0], highLimits=limits[1]) s = 0.0 s2 = 0.0 mr = 5 for k in [1, 5, 10, 20, 40]: dis = GaussErrorDistribution(scale=0.5) ns = NestedSampler(x, model, y, distribution=dis, verbose=0, discard=k) logE = ns.sample() par2 = ns.parameters logz2 = ns.logZ dlz2 = ns.logZprecision s += logz2 s2 += logz2 * logz2 print(fmt(k), " pars ", fmt(par2), " logZ ", fmt(logz2), " +- ", fmt(dlz2)) logz = s / mr dlz = math.sqrt(s2 / mr - logz * logz) print("Average ", fmt(logz), " +- ", fmt(dlz))
def test2(self): plot = self.doplot ndata = 101 c0 = 3.2 c1 = -0.1 c2 = 0.3 c3 = 1.1 c4 = 2.1 ss = 0.2 x = numpy.linspace(-1, +1, ndata, dtype=float) y = (x - c1) / c2 numpy.random.seed(12345) y = c0 * numpy.exp( -0.5 * y * y) + x * c3 + c4 + ss * numpy.random.randn(ndata) print("Testing Nonlinear Fitters with RobustShell.") print(fmt([c0, c1, c2, c4, c3])) modl2 = GaussModel() modl2 += PolynomialModel(1) # lmfit = CurveFitter( x, modl2 ) lmfit = LevenbergMarquardtFitter(x, modl2) par2 = lmfit.fit(y) std2 = lmfit.stdevs print(fmt(par2, max=5)) print(fmt(std2, max=5)) # make some outliers ko = [10 * k + 4 for k in range(10)] ko = numpy.asarray(ko) y[ko] += numpy.linspace(-5, 2, 10) if plot: plt.plot(x, y, 'k.') plt.plot(x, modl2(x), 'k-') # lmfit = CurveFitter( x, modl2 ) lmfit = LevenbergMarquardtFitter(x, modl2) # lmfit.setParameters( initpar2 ) rf = RobustShell(lmfit) # rf.setVerbose( 1 ) print(str(rf)) par1 = rf.fit(y) std1 = rf.stdevs print(fmt(par1, max=5)) print(fmt(std1, max=5)) print(fmt(par2, max=5)) print(fmt(rf.weights[ko], max=20)) if plot: plt.plot(x, modl2(x), 'g-') plt.plot(x, rf.weights, 'g-') assertAAE(par2, par1, 1) assertAAE(std2, std1, 1) # lmfit = CurveFitter( x, modl2 ) lmfit = LevenbergMarquardtFitter(x, modl2) # lmfit.setParameters( initpar2 ) rf = RobustShell(lmfit, onesided="negative") # rf.setVerbose( 1 ) print(rf) par3 = rf.fit(y) std3 = rf.stdevs print(fmt(par3, max=5)) print(fmt(std3, max=5)) print(fmt(par2, max=5)) print(fmt(rf.weights[ko], max=20)) if plot: plt.plot(x, modl2(x), 'r-') plt.plot(x, rf.weights, 'r-') plt.show() assertAAE(par2, par3, 1) assertAAE(std2, std3, 1)
def test1(self): """ # test slope fit """ print("\n Robust fitter Test 1 \n") plot = self.doplot ndata = 101 aa = 5.0 # average offset bb = 2.0 # slope ss = 0.3 # noise Normal distr. x = numpy.linspace(-1, +1, ndata, dtype=float) numpy.random.seed(12345) y = aa + bb * x + ss * numpy.random.randn(ndata) model = PolynomialModel(1) fitter = Fitter(x, model) print("Testing Straight Line fit") par = fitter.fit(y) std = fitter.stdevs chisq = fitter.chisq print("truth " + fmt(aa) + fmt(bb)) print("params " + fmt(par)) print("stdevs " + fmt(std)) print("chisq " + fmt(chisq)) assertAAE(par, numpy.asarray([aa, bb]), 2) if plot: plt.plot(x, y, 'k*') plt.plot(x, model(x), 'k-') # make some outliers ko = [10 * k + 4 for k in range(10)] ko = numpy.asarray(ko) y[ko] += numpy.linspace(-5, 2, 10) romod = PolynomialModel(1) altfit = Fitter(x, romod) alt = altfit.fit(y) ast = altfit.stdevs altch = altfit.chisq print("params " + fmt(alt)) print("stdevs " + fmt(ast)) print("chisq " + fmt(altch)) if plot: plt.plot(x, y, 'b.') plt.plot(x, romod(x), 'b-') rf = RobustShell(altfit) Tools.printclass(rf) alt = rf.fit(y) ast = altfit.stdevs altch = altfit.chisq print(rf) print("params " + fmt(alt)) print("stdevs " + fmt(ast)) print("chisq " + fmt(altch) + fmt(rf.scale)) print("weight " + fmt(rf.weights[ko], max=None)) if plot: plt.plot(x, romod(x), 'g-') plt.plot(x, rf.weights, 'g-') assertAAE(par, alt, 1) assertAAE(std, ast, 1) rf = RobustShell(altfit, kernel=Cosine(), domain=4.0) alt = rf.fit(y) ast = altfit.stdevs altch = altfit.chisq print(rf) print("params " + fmt(alt)) print("stdevs " + fmt(ast)) print("chisq " + fmt(altch) + fmt(rf.scale)) print("weight " + fmt(rf.weights[ko], max=None)) if plot: plt.plot(x, romod(x), 'r-') plt.plot(x, rf.weights, 'r-') assertAAE(par, alt, 1) assertAAE(std, ast, 1) rf = RobustShell(altfit, kernel=Huber, domain=1.0) # rf.setNoiseScale( 0.3 ) alt = rf.fit(y) ast = altfit.stdevs altch = altfit.chisq print(rf) print("params " + fmt(alt)) print("stdevs " + fmt(ast)) print("chisq " + fmt(altch) + fmt(rf.scale)) print("weight " + fmt(rf.weights[ko], max=None)) if plot: plt.plot(x, romod(x), 'c-') plt.plot(x, rf.weights, 'c-') assertAAE(par, alt, 1) assertAAE(std, ast, 1) rf = RobustShell(altfit, kernel=Uniform) alt = rf.fit(y) ast = altfit.stdevs altch = altfit.chisq print(rf) print("params " + fmt(alt)) print("stdevs " + fmt(ast)) print("chisq " + fmt(altch) + fmt(rf.scale)) print("weight " + fmt(rf.weights[ko], max=None)) if plot: plt.plot(x, romod(x), 'm-') plt.plot(x, rf.weights, 'm-') plt.show() assertAAE(par, alt, 1) assertAAE(std, ast, 1)
def test5(self): print("5 Test Dynamic Modifiable Splines: evidence with fixed scale ") t, y = self.makeData() # npt = 201 # t = numpy.linspace( 0, 100, npt, dtype=float ) # ym = 2 * numpy.sin( 2 * math.pi * numpy.exp( t / 80 ) +1 ) * numpy.exp( -0.02 * t ) # y = numpy.random.seed( 12345 ) # y = ym + numpy.random.randn( npt ) * 0.05 knots = [0, 100] mxk = 15 mdl = SplinesDynamicModel(knots=knots, dynamic=True, maxKnots=mxk, minDistance=0.04) mdl.setLimits(lowLimits=[-10.0], highLimits=[+10.0]) ep = EvidenceProblem(model=mdl, xdata=t, ydata=y) distr = ModelDistribution(scale=0.1) ns = NestedSampler(problem=ep, distribution=distr) ns.verbose = 2 ns.minimumIterations = 2000 ## Comment next if-statement out for a full run of NestedSampler if not self.dofull: ns.ensemble = 10 ns.minimumIterations = 500 evid = ns.sample(plot=self.doplot) if not self.doplot: return ## Plot the evolutie of knots and sample weights cc = ['k,', 'b,', 'r,', 'g,', 'c,', 'm,'] sl = ns.samples ka = numpy.zeros((mxk, len(sl)), dtype=float) plt.figure(1, figsize=[14, 8]) for k, s in enumerate(sl): n = len(s.model.knots) ka[:n, k] = s.model.knots for j in range(mxk): plt.plot(ka[j, :], cc[j % 6]) wgts = sl.getWeightEvolution() mw = max(wgts) plt.plot(100 * wgts / mw, 'k-') plt.xlabel("Iteration number") plt.ylabel("Knot position cq. sample weight") plt.show() plt.figure(2, figsize=[14, 8]) plt.plot(t, y, 'k.') cc = ['k-', 'b-', 'r-', 'g-', 'c-', 'm-', 'y-'] cp = ['k*', 'b*', 'r*', 'g*', 'c*', 'm*', 'y*'] for k in range(0, 2100, 200): kc = k % 7 s = sl[k] mdl = s.model kn = mdl.knots plt.plot(kn, [-1 + k / 1000] * len(kn), cp[kc]) plt.plot(t, mdl(t), cc[kc]) print(fmt(k)) print(fmt(kn, max=None)) print(fmt(mdl.parameters, max=None), fmt(s.logL)) m = BasicSplinesModel(knots=kn) ftr = Fitter(t, m, fixedScale=0.05) par = ftr.fit(y) lz0 = ftr.getLogZ(limits=[-10, +10]) print(fmt(par, max=None), fmt(lz0)) m = BasicSplinesModel(knots=kn) ftr = Fitter(t, m) par = ftr.fit(y) lz0 = ftr.getLogZ(limits=[-10, +10]) print(fmt(par, max=None), fmt(lz0)) m = BasicSplinesModel(knots=kn) ftr = Fitter(t, m) par = ftr.fit(y) lz0 = ftr.getLogZ(limits=[-10, +10], noiseLimits=[0.01, 1]) print(fmt(par, max=None), fmt(lz0)) plt.show()
def XXXtestNestedSampler2( self ) : print( "========================" ) print( " Test Nested Sampler 2" ) print( "========================" ) ND = 10 NP = 100 numpy.random.seed( 12345 ) xdata = numpy.random.rand( NP, ND ) * 10 print( fmt( xdata ) ) y = 0.5 * xdata[:,1] print( xdata.shape, y.shape ) y += numpy.where( xdata[:,4] > 3, 5, 0 ) y += numpy.where( xdata[:,6] > 8, 0, 3 ) y += numpy.where( xdata[:,4] > 7, 2, 0 ) noise = numpy.random.randn( NP ) y += 0.02 * noise pm = DecisionTreeModel( ndim=ND, kdim=[0], depth=0 ) print( pm ) lolim = [-10] hilim = [+10] pm.setLimits( lowLimits=lolim, highLimits=hilim ) Tools.printclass( pm ) ed = ModelLikelihood( limits=[0.01,100] ) engs = ["birth", "death", "struct"] # engs = ["birth", "struct"] ns = NestedSampler( xdata, pm, y, ensemble=100, distribution=ed, engines=engs, seed=2031967 ) # ns.minimumIterations = 4000 ns.verbose = 2 # ns.weed = 10000 logE = ns.sample( ) sl = ns.samples yfit = sl.average( xdata ) ksrt = numpy.argsort( yfit ) xxx = numpy.arange( NP, dtype=float ) mi = sl.medianIndex print( mi, sl.modusIndex, len( sl ) ) if not self.doplot : return plt.plot( xxx, y[ksrt], 'k.' ) plt.plot( xxx, yfit[ksrt], 'r-' ) for k in range( mi-3, mi+4 ) : mdl = sl[k].model print( mdl.fullName() ) ymod = mdl.result( xdata, sl[k].parameters ) plt.plot( xxx, ymod[ksrt], 'g-' ) print( ymod ) print( ksrt ) plt.show() print( ymod[ksrt] )
def testNestedSampler( self ) : print( "========================" ) print( " Test Nested Sampler" ) print( "========================" ) ND = 10 NP = 100 numpy.random.seed( 12345 ) xdata = numpy.random.rand( NP, ND ) * 10 print( fmt( xdata ) ) y = 0.5 * xdata[:,1] print( xdata.shape, y.shape ) y += numpy.where( xdata[:,4] > 3, 5, 0 ) y += numpy.where( xdata[:,6] > 8, 0, 3 ) y += numpy.where( xdata[:,4] > 7, 2, 0 ) noise = numpy.random.randn( NP ) y += 0.02 * noise pm = DecisionTreeModel( ndim=ND, kdim=[0], depth=0 ) print( pm ) lolim = [-10] hilim = [+10] pm.setLimits( lowLimits=lolim, highLimits=hilim ) Tools.printclass( pm ) ns = NestedSampler( xdata, pm, y, ensemble=100, seed=2031967 ) ns.verbose = 2 ns.weed = 10000 ns.distribution.setLimits( [0.01, 100] ) if not self.dofull : ns.ensemble = 10 logE = ns.sample( ) sl = ns.samples yfit = sl.average( xdata ) # ksrt = numpy.argsort( yfit ) xxx = numpy.arange( NP, dtype=float ) mi = sl.medianIndex print( mi, sl.modusIndex, len( sl ) ) if not self.doplot : return mdl = sl[mi].model ksrt = mdl.sortXdata( xdata ) plt.plot( xxx, y[ksrt], 'k.' ) plt.plot( xxx, yfit[ksrt], 'r-' ) for k in range( mi-3, mi+4 ) : mdl = sl[k].model print( mdl.fullName() ) ymod = mdl.result( xdata, sl[k].parameters ) plt.plot( xxx, ymod[ksrt], 'g-' ) print( ymod ) print( ksrt ) print( ymod[ksrt] ) plt.show()
def test6( self ): print( " Test 6 DecisionTreeModel" ) dtm = DecisionTreeModel( ndim=20, kdim=[0,1,2,3,4,5,6], split=[0.1*k for k in range(2,8)], depth=3 ) dtm.parameters = numpy.arange( dtm.npars, dtype=float ) + 1 print( dtm.fullName() ) print( fmt( dtm.parameters, max=None ) ) self.assertTrue( dtm.npbase == dtm.npars ) self.assertTrue( dtm.npars == dtm.countLeaf() ) self.assertTrue( dtm.ncomp == dtm.countBranch() ) print( "GROW at location 2 dim 3 split 0.44" ) dtm.grow( offset=0, location=2, kdim=3, split=0.44 ) print( dtm ) print( fmt( dtm.parameters, max=None ) ) self.assertTrue( dtm.npbase == dtm.npars ) self.assertTrue( dtm.npars == dtm.countLeaf() ) self.assertTrue( dtm.ncomp == dtm.countBranch() ) print( "VARY at location 4, dim=5, split=0.55" ) dtm.vary( location=4, kdim=5, split=0.55 ) print( dtm ) print( fmt( dtm.parameters, max=None ) ) self.assertTrue( dtm.npbase == dtm.npars ) self.assertTrue( dtm.npars == dtm.countLeaf() ) self.assertTrue( dtm.ncomp == dtm.countBranch() ) print( "GROW at location 8" ) dtm.grow( offset=0, location=8 ) print( dtm ) print( fmt( dtm.parameters, max=None ) ) self.assertTrue( dtm.npbase == dtm.npars ) self.assertTrue( dtm.npars == dtm.countLeaf() ) self.assertTrue( dtm.ncomp == dtm.countBranch() ) self.assertTrue( len( dtm.parNames ) == dtm.npars ) print( "SHRINK at location 7" ) dtm.shrink( offset=0, location=7 ) print( dtm ) print( fmt( dtm.parameters, max=None ) ) self.assertTrue( dtm.npbase == dtm.npars ) self.assertTrue( dtm.npars == dtm.countLeaf() ) self.assertTrue( dtm.ncomp == dtm.countBranch() ) print( "SHRINK at location 3" ) dtm.shrink( offset=0, location=3 ) print( dtm ) print( fmt( dtm.parameters, max=None ) ) self.assertTrue( dtm.npbase == dtm.npars ) self.assertTrue( dtm.npars == dtm.countLeaf() ) self.assertTrue( dtm.ncomp == dtm.countBranch() ) print( "SHRINK at location 0" ) dtm.shrink( offset=0, location=0 ) print( dtm.fullName() ) print( fmt( dtm.parameters, max=None ) ) self.assertTrue( dtm.npbase == dtm.npars ) self.assertTrue( dtm.npars == dtm.countLeaf() ) self.assertTrue( dtm.ncomp == dtm.countBranch() ) printclass( dtm )
def test7(self): print("7 Test Dynamic Modifiable Splines: evidence unknown scale") t, y = self.makeData() knots = [0, 100] mxk = 15 mdl = SplinesDynamicModel(knots=knots, dynamic=True, maxKnots=mxk, minDistance=0.01) mdl.setLimits(lowLimits=[-10.0], highLimits=[+10.0]) ep = EvidenceProblem(model=mdl, xdata=t, ydata=y) distr = ModelDistribution(limits=[0.01, 1]) #ns = NestedSampler( t, mdl, y, seed=1235, engines=eng, distribution=distr ) #print( ns.problem ) #print( ns.model ) #print( fmt( ns.xdata ) ) #print( fmt( ns.ydata ) ) ns = NestedSampler(problem=ep, distribution=distr) print(ns.problem) print(ns.model) print(fmt(ns.xdata)) print(fmt(ns.ydata)) ns.verbose = 2 ## Comment next if-statement out for a full run of NestedSampler if not self.dofull: ns.ensemble = 10 ns.minimumIterations = 500 evid = ns.sample(plot=self.doplot) if not self.doplot: return ## Plot the evolutie of knots and sample weights cc = ['k,', 'b,', 'r,', 'g,', 'c,', 'm,'] sl = ns.samples ka = numpy.zeros((mxk, len(sl)), dtype=float) plt.figure(1, figsize=[14, 8]) for k, s in enumerate(sl): n = len(s.model.knots) ka[:n, k] = s.model.knots for j in range(mxk): plt.plot(ka[j, :], cc[j % 6]) wgts = sl.getWeightEvolution() mw = max(wgts) plt.plot(100 * wgts / mw, 'k-') plt.xlabel("Iteration number") plt.ylabel("Knot position cq. sample weight") plt.show() plt.figure(2, figsize=[14, 8]) plt.plot(t, y, 'k.') cc = ['k-', 'b-', 'r-', 'g-', 'c-', 'm-', 'y-'] cp = ['k*', 'b*', 'r*', 'g*', 'c*', 'm*', 'y*'] for k in range(0, 2100, 200): kc = k % 7 s = sl[k] mdl = s.model kn = mdl.knots plt.plot(kn, [-1 + k / 1000] * len(kn), cp[kc]) plt.plot(t, mdl(t), cc[kc]) print(fmt(k)) print(fmt(kn, max=None)) print(fmt(mdl.parameters, max=None), fmt(s.logL)) plt.show()