Esempio n. 1
0
    def xxxtest3(self, plot=False):
        """     # 	test iterative slope fit  """
        print("Testing AnnealingAmoeba with limits")

        if plot:
            x = numpy.linspace(-10, 10, 201, dtype=float)
            y = numpy.linspace(-10, 10, 201, dtype=float)
            self.plot(self.func2,
                      x=x,
                      y=y,
                      contour=[5, 10, 15, 20, 30, 40, 60, 80, 100, 200])

        xx = numpy.asarray([3, -2], dtype=float)
        amb = AnnealingAmoeba(self.func2, xx, reltol=1e-10, verbose=5)
        xopt = amb.minimize()

        print(amb.fopt, xopt)
        if plot:
            plt.plot([xopt[1]], [xopt[0]], 'r+')

        amb = AnnealingAmoeba(self.func2,
                              xx,
                              temp=10,
                              seed=123456,
                              verbose=100)
        amb.minimize()

        xopt = amb.xopt
        print(amb.fopt, xopt)
        if plot:
            plt.plot([xopt[1]], [xopt[0]], 'g+')
            plt.show()

        assertAAE(amb.xopt, [-1.836, -2.0767], 3)
        assertAAE(amb.fopt, 2.0221211, 5)
Esempio n. 2
0
    def test2_1(self):
        print("====test2_1============================")
        nn = 100
        x = numpy.arange(nn, dtype=float) / 50
        ym = 0.2 + 0.5 * x
        nf = 0.1
        numpy.random.seed(2345)
        noise = numpy.random.randn(nn)

        y = ym + nf * noise
        limits = [-1, 2]

        pm = PolynomialModel(1)
        bf = Fitter(x, pm)

        pars = bf.fit(y)
        logz0 = bf.getLogZ(limits=limits)
        logl0 = bf.logLikelihood
        print("pars  ", fmt(pars))
        print("stdv  ", fmt(bf.stdevs))
        print("logZ  ", fmt(logz0), "   logL  ", fmt(logl0))

        errdis = GaussErrorDistribution()
        problem = ClassicProblem(pm, xdata=x, ydata=y)

        logz1, logl1 = plotErrdis2d(errdis,
                                    problem,
                                    limits=limits,
                                    max=0,
                                    plot=self.doplot)
        if self.doplot:
            plt.plot(pars[0], pars[1], 'k.')

        print("logZ  ", fmt(logz1), "   logL  ", fmt(logl1))

        model = PolynomialModel(1)
        model.setLimits(lowLimits=limits[0], highLimits=limits[1])
        ns = NestedSampler(x, model, y, verbose=0)

        logE = ns.sample()

        par2 = ns.parameters
        logz2 = ns.logZ
        dlz2 = ns.logZprecision
        print("pars  ", fmt(par2))
        print("stdv  ", fmt(ns.stdevs))
        print("logZ  ", fmt(logz2), " +- ", fmt(dlz2))

        self.assertTrue(abs(logz2 - logz0) < 1.0)
        #        print( logz0 - logz1, logz0 - logz2 )

        samples = ns.samples
        parevo = samples.getParameterEvolution()
        llevo = samples.getLogLikelihoodEvolution()
        lwevo = samples.getLogWeightEvolution()

        assertAAE(numpy.sum(numpy.exp(lwevo)), 1.0)

        if self.doplot:
            plt.show()
Esempio n. 3
0
    def test2(self):
        """     # 	test iterative slope fit  """
        print("Testing AnnealingAmoeba with limits")

        xx = numpy.asarray([3, -2], dtype=float)
        amb = AnnealingAmoeba(self.func1,
                              xx,
                              limits=[[-1, 0], [3, 3]],
                              reltol=1e-10,
                              maxiter=100,
                              verbose=5)
        Tools.printclass(amb)
        self.assertRaises(ConvergenceError, amb.minimize)

        xx = numpy.asarray([3, -2], dtype=float)
        amb = AnnealingAmoeba(self.func1,
                              xx,
                              limits=[[-1, 0], [3, 3]],
                              reltol=1e-10,
                              verbose=5)

        amb.minimize()
        Tools.printclass(amb)
        assertAAE(amb.xopt, [-1.0, 0.0])
        assertAAE(amb.fopt, self.func1([-1.0, 0.0]))
Esempio n. 4
0
    def testLogFactorial(self, plot=False):

        print("====LogFactorial ===================")
        self.assertTrue(logFactorial(0) == 0)
        self.assertTrue(logFactorial(1) == 0)
        self.assertTrue(logFactorial(3) == 1.7917594692280550)
        self.assertTrue(logFactorial(10) == 15.1044125730755159)
        print(logFactorial(99), logFactorial(100), logFactorial(101))
        self.assertTrue(logFactorial(99) == 359.1342053695754544)
        lf = 359.1342053695754544 + math.log(100)
        assertAAE(logFactorial(100), lf)

        kk = [3, 5, 7]
        print(logFactorial(kk))
        self.assertTrue(isinstance(kk, list))

        data = numpy.arange(100, dtype=int) + 40

        logd = numpy.log(data)

        lf1 = numpy.cumsum(logd) + logFactorial(39)
        lf2 = logFactorial(data)

        k = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
        print(lf1[k])
        print(lf2[k])
        assertAAE(lf1, lf2)
Esempio n. 5
0
    def testKernel2dModelDefault(self):
        print("*******************************************************")
        print("*  Kernel2dModel Circular Gauss                       *")
        print("*******************************************************")

        m = Kernel2dModel()
        self.assertTrue(m.npchain, 4)

        x1 = numpy.arange(101, dtype=float) / 25.0 - 2.02

        x = x1.copy()
        x = numpy.append(x, x1 + 0.01, 0).reshape(101, 2)
        print(fmt(x))

        p = m.parameters
        print(m)
        print(x[40, :])
        m.testPartial([x[40, :]], p)

        for k in range(4):
            print("%d  %-12s %-12s" %
                  (k, m.getParameterName(k), m.getParameterUnit(k)))

        p = numpy.asarray([1.0, 1.01, 3.0, 1.2], dtype=float)
        part = m.partial(x, p)
        nump = m.numPartial(x, p)
        assertAAE(part, nump, 4)
        mc = m.copy()
        mc.parameters = p
        m.parameters = p
        assertAAE(m.result(x), mc.result(x))
        self.assertTrue(isinstance(mc.kernel, Gauss))
    def bstest7(self, kn, order=3, plot=False):
        print("==== test 7 ========= order = %d ===========" % (order))

        x = numpy.linspace(0, 10, 101, dtype=float)

        n = 0
        cc = ['k-', 'b-', 'r-', 'g-', 'c-', 'm-']

        sm = BasicSplinesModel(knots=kn, order=order, border=1)
        spar = numpy.ones(sm.npars, dtype=float)
        bpar = numpy.linspace(0.0, 1.0, sm.npars, dtype=float)

        ysm = sm.result(x, spar)
        pts = sm.partial(x, spar)

        ybm = sm.result(x, bpar)
        ptb = sm.partial(x, bpar)

        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 + 1.1, 'k-')
            plt.plot(x, ysm, 'k-')
            for i in range(sm.npars):
                plt.plot(x, ptb[:, i] + 1.1, cc[i % 6])
                plt.plot(x, pts[:, i], cc[i % 6])

            plt.show()

        assertAAE(ysm, 1.0)
Esempio n. 7
0
    def testKernel2dModelCosine(self):
        print("*******************************************************")
        print("*  Kernel2dModel Elliptic Cosine                      *")
        print("*******************************************************")

        m = Kernel2dModel(kernel=Cosine(), shape=2)
        self.assertTrue(m.npchain, 5)

        x1 = numpy.arange(101, dtype=float) / 25.0 - 2.02

        x = x1.copy()
        x = numpy.append(x, x1 + 0.01, 0).reshape(101, 2)

        p = m.parameters
        print(m)
        m.testPartial([[0.1, 0.2]], p)

        for k in range(m.npbase):
            print("%d  %-12s %-12s" %
                  (k, m.getParameterName(k), m.getParameterUnit(k)))

        p = numpy.asarray([1.0, 1.01, 3.0, 1.2, 2.0], dtype=float)
        part = m.partial(x, p)
        nump = m.numPartial(x, p)
        assertAAE(part, nump, 4)
        mc = m.copy()
        mc.parameters = p
        m.parameters = p
        assertAAE(m.result(x), mc.result(x))
        self.assertTrue(isinstance(mc.kernel, Cosine))
Esempio n. 8
0
def plotErrdis2d( errdis, problem, limits=[-10,10], nslim=None, max=None,
                plot=None ) :

    np = 201
    map = numpy.ndarray( (np,np), dtype=float )

    par = problem.model.parameters
    pars = numpy.append( par, errdis.hypar )
    r0lo = limits[0]
    r0hi = limits[1]
    r0 = numpy.linspace( r0lo, r0hi, np, dtype=float )
    if nslim is None :
        r1lo, r1hi = ( r0lo, r0hi )
        r1 = numpy.linspace( r1lo, r1hi, np, dtype=float )
        prior1 = numpy.zeros_like( r1 ) + 1 / ( r1hi - r1lo )
    else :
        r1lo, r1hi = ( nslim[0], nslim[1] )
        r1 = numpy.linspace( r1lo, r1hi, np, dtype=float )
        cp = math.log( r1hi / r1lo )                # integral of Jeffreys
        prior1 = 1 / ( cp * r1 )


    pixsz = ( r0hi - r0lo ) * ( r1hi - r1lo ) / ( np * np )
    prior0 = numpy.zeros_like( r0 ) + 1 / ( r0hi - r0lo )
    prior = numpy.outer( prior1, prior0 ) * pixsz
    zp = numpy.sum( prior )
#    print( numpy.sum( prior0 ), numpy.sum( prior1 ), zp )
    assertAAE( zp, 1.0, 2 )

#    print( prior.shape, prior[0,0], ( 1.0/ ( r0hi - r0lo ) ) )

    for k1,p1 in enumerate( r1 ) :
        pars[1] = p1
        for k0,p0 in enumerate( r0 ) :
            pars[0] = p0
            map[k1,k0] = errdis.logLikelihood( problem, pars )

    logl = numpy.max( map )
#    print( prior.shape, prior[0,0], 2*math.log( 1.0/ ( r0hi - r0lo ) ) )
#    print( pixsz )

    map += numpy.log( prior )

    if max is None :
        max = numpy.max( map )

    map = numpy.exp( map - max )

    if plot :
        plt.figure( str( errdis ) )

        # More colormaps in matplotlib: docs->Matplotlib examples->color Examples

        plt.imshow( map, aspect=None, cmap='viridis', origin='lower',
                extent=(r0lo,r0hi,r1lo,r1hi) )
        plt.xlabel( "parameter 0" )
        plt.ylabel( "parameter 1" )

    return ( ( math.log( numpy.sum( map ) ) + max ), logl )
Esempio n. 9
0
    def xxxdtest(self, x, m, p):

        f = m.result(x, p)
        print("Result   ", f.shape)
        print(fma(f))

        if self.doplot:
            for k in range(x.shape[1]):
                plt.plot(x[:, k], 'k-')
            for n in range(f.shape[1]):
                plt.plot(f[:, n], 'r-')
            plt.show()

        dfdx = m.derivative(x, p)
        print("DfDx     ", dfdx.shape)

        for k in range(x.shape[1]):
            xp = x.copy()
            xm = x.copy()

            xp[:, k] += 0.00001
            xm[:, k] -= 0.00001

            yp = m.result(xp, p)
            ym = m.result(xm, p)

            numx = (yp - ym) / 0.00002
            numx = numx.transpose()

            print(k)
            print("dx0   ", fma(dfdx[:, :, k], indent=7))
            print("nm0   ", fma(numx, indent=7))
            assertAAE(dfdx[:, :, k], numx)

        part = m.basePartial(x, p)
        for pk in part:
            print("Partial  ", pk.shape, id(pk))
            print(fmt(pk, tail=6))

        for k in range(m.npars):
            print("parameter ", k, "  ", m.parNames[k])
            pp = p.copy()
            pp[k] += 0.0001
            pm = p.copy()
            pm[k] -= 0.0001
            yp = m.result(x, pp)
            ym = m.result(x, pm)

            num = (yp - ym) / 0.0002

            mm = k // m.ndim
            if mm >= m.ndout:
                mm = k - m.in2out

            print("In ", k, "  Out ", mm)
            print("part  ", fmt(part[mm][:, k], tail=3))
            print("nm0   ", fmt(num[:, mm], tail=3))
            assertAAE(part[mm][:, k], num[:, mm], 4)
    def testLaplaceErrorDistribution(self):
        print("\n   Test Laplace Error Distribution\n")
        poly = PolynomialModel(1)
        ced = LaplaceErrorDistribution(self.x, self.data)
        self.assertTrue(ced.acceptWeight())

        param = numpy.asarray([1, 10, 1], dtype=float)
        logL = ced.logLikelihood(poly, param)
        print("logL  =  %8.3f" % (logL))
        scale = 0.1
        param[2] = scale
        s2 = scale * scale
        logL = ced.logLikelihood(poly, param)

        altL = -len(self.data) * math.log(2.0 * scale)
        res = ced.getResiduals(poly, param[:2])
        altL -= numpy.sum(numpy.abs(res)) / scale
        print("logL  =  %8.3f  alt %8.3f" % (logL, altL))
        assertAAE(logL, altL)

        scale = 1.0
        param[2] = scale
        fi = [0, 1, 2]
        dL = ced.partialLogL(poly, param, fi)
        nL = ced.numPartialLogL(poly, param, fi)
        print("params  = ", param, scale)
        print("partial = ", dL)
        print("numpart = ", nL)
        assertAAE(dL, nL, 2)

        scale = 0.5
        param[2] = scale
        ced.weights = self.wgt
        dL = ced.partialLogL(poly, param, fi)
        nL = ced.numPartialLogL(poly, param, fi)
        print("params  = ", param, scale)
        print("partial = ", dL)
        print("numpart = ", nL)
        assertAAE(dL, nL, 2)

        logL = ced.logLikelihood(poly, param)
        cced = ced.copy()
        logLc = cced.logLikelihood(poly, param)
        print(cced)
        print("logL  =  %8.3f  copy %8.3f" % (logL, logLc))
        dLc = cced.partialLogL(poly, param, fi)
        print("params  = ", param, scale)
        print("partial = ", dL)
        assertAAE(logL, logLc, 6)
        assertAAE(dL, dLc, 6)

        for i in range(11):
            param = numpy.asarray([i - 5, 5, 1], dtype=float)
            print(param, ":  ", end="")
            for k in range(9):
                print(" %8.3f" % ced.logLikelihood(poly, param), end="")
                param[1] += 1
            print("")
    def testMonteCarlo3(self, doplot=False):
        print("====== MonteCarlo 3 ===================")

        N = 101
        x = numpy.arange(N, dtype=float) * 0.1
        ran = numpy.random
        ran.seed(1235)
        noise = ran.standard_normal(N)

        ym = x * x + 0.03 * x + 0.05
        y1 = ym + 10 * noise

        pm = PolynomialModel(2)

        ftr = Fitter(x, pm)

        pars1 = ftr.fit(y1)
        stdv1 = ftr.getStandardDeviations()
        print("parameters : ", pars1)
        print("std devs   : ", stdv1)
        print("chisquared : ", ftr.chisq)

        lmce = ftr.monteCarloError()
        chisq = ftr.chisq

        mce = MonteCarlo(x, pm, ftr.covariance)
        mce1 = mce.getError()
        assertAAE(lmce, mce1)

        yfit = pm.result(x)
        s2 = numpy.sum(numpy.square((yfit - ym) / lmce))
        print(s2, math.sqrt(s2 / N))

        integral = numpy.sum(yfit)
        s1 = 0
        s2 = 0
        k = 0
        for k in range(1, 100001):
            rv = mce.randomVariant(x)
            s1 += numpy.sum(rv)
            s2 += numpy.sum(numpy.square(rv - yfit))
            if k % 10000 == 0:
                print("%6d  %10.3f %10.3f %10.3f" %
                      (k, integral, s1 / k, math.sqrt(s2 / k)))

        ### TBC  dont know why the factor 1000 is there. ########
        print(abs(integral - s1 / k), math.sqrt(s2 / (k * 1000)))
        self.assertTrue(abs(integral - s1 / k) < math.sqrt(s2 / (k * 1000)))

        if doplot:
            pyplot.plot(x, y1, 'b.')

            pyplot.plot(x, ym, 'k-')
            pyplot.plot(x, yfit, 'g-')
            pyplot.plot(x, yfit + lmce, 'r-')
            pyplot.plot(x, yfit - lmce, 'r-')
            pyplot.show()
    def testCauchyErrorDistribution(self):
        print("\n   Test Cauchy Error Distribution\n")
        poly = PolynomialModel(1)

        problem = ClassicProblem(model=poly, xdata=self.x, ydata=self.data)

        ced = CauchyErrorDistribution()
        self.assertFalse(ced.acceptWeight())

        param = numpy.asarray([1, 10, 1], dtype=float)
        logL = ced.logLikelihood(problem, param)
        print("logL  =  %8.3f" % (logL))
        scale = 0.1
        s2 = scale * scale
        param[2] = scale
        logL = ced.logLikelihood(problem, param)
        altL = ced.logLikelihood_alt(problem, param)

        lLdata = ced.logLdata(problem, param)
        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  alt %8.3f" % (logL, altL ) )
        #        assertAAE( logL, altL )

        scale = 1.0
        param[2] = scale
        fi = [0, 1, -1]
        dL = ced.partialLogL(problem, param, fi)
        nL = ced.numPartialLogL(problem, param, fi)
        print("params  = ", param, scale)
        print("partial = ", dL)
        print("numpart = ", nL)
        assertAAE(dL, nL, 2)

        scale = 0.5
        param[2] = scale
        dL = ced.partialLogL(problem, param, fi)
        nL = ced.numPartialLogL(problem, param, fi)
        print("params  = ", param, scale)
        print("partial = ", dL)
        print("numpart = ", nL)
        assertAAE(dL, nL, 2)

        aG = ced.partialLogL_alt(problem, param, fi)
        print("partalt = ", aG)
        assertAAE(dL, aG, 5)

        for i in range(11):
            param = numpy.asarray([i - 5, 5, 1], dtype=float)
            print(param, ":  ", end="")
            for k in range(9):
                print(" %8.3f" % ced.logLikelihood(problem, param), end="")
                param[1] += 1
            print("")
Esempio n. 13
0
    def test1(self):
        """     # 	test iterative slope fit  """
        print("Testing AnnealingAmoeba")

        xx = numpy.asarray([3, -2], dtype=float)
        amb = AnnealingAmoeba(self.func1, xx, verbose=5)

        Tools.printclass(amb)
        amb.minimize()
        Tools.printclass(amb)
        assertAAE(amb.xopt, [-1.2, -1.44], 2)
        assertAAE(amb.fopt, self.func1([-1.2, -1.44]), 3)
Esempio n. 14
0
    def testAmoebaFitter(self):

        c0 = 3.2
        c1 = -0.1
        c2 = 0.3
        c3 = 1.1
        c4 = 2.1
        y = (self.x - c1) / c2
        y = c0 * numpy.exp(-y * y) + self.noise

        print("++++++++++++++++++++++++++++++++++++++++++++++++++")
        print("Testing Nonlinear Fitters: Amoeba")
        print("++++++++++++++++++++++++++++++++++++++++++++++++++")
        modl1 = GaussModel()

        numpy.set_printoptions(precision=3, suppress=True)
        amfit = AmoebaFitter(self.x, modl1)

        tr = numpy.asarray([c0, c1, c2, c3, c4])
        print("Truth ", tr)

        par1 = amfit.fit(y)

        print("x     ", self.x)
        print("y     ", y)
        print("par   ", par1)

        modl2 = GaussModel()
        modl2.addModel(PolynomialModel(1))
        z = y + c4 * self.x + c3

        modl2.parameters = numpy.append(par1, [0, 0])
        lmfit = AmoebaFitter(self.x, modl2)

        par2 = lmfit.fit(z)
        print("z      ", z)
        print("par2   ", par2)

        print("chisq1 = ", amfit.chisq, "  chisq2 = ", lmfit.chisq)

        assertAAE(par1, par2[:3], 1)
        assertAAE(par2, tr, 0)

        if self.doplot:
            xx = numpy.linspace(-1, +1, 1001)
            plt.plot(self.x, y, 'k+')
            plt.plot(xx, modl1.result(xx), 'k-')
            plt.plot(self.x, z, 'r+')
            plt.plot(xx, modl2.result(xx), 'r-')
            plt.show()
Esempio n. 15
0
    def testImageAssistant3dF(self, plot=False):

        print("====ImageAssistant 3d F ===================")
        ymap = numpy.arange(24, dtype=float).reshape(2, 3, 4)
        print(ymap)
        ia = ImageAssistant(order='F')
        xdata = ia.getIndices(ymap)
        print(xdata)
        ydata = ia.getydata(ymap)
        print(ydata)
        ydata += 1
        ymap1 = ia.resizeData(ydata)
        print(ymap1)
        assertAAE(ymap1, ymap + 1)
    def testImageAssistantC(self):

        print("====ImageAssistant C ===================")
        ymap = numpy.arange(6, dtype=float).reshape(2, 3)
        print(ymap)
        ia = ImageAssistant()
        xdata = ia.getIndices(ymap)
        print(xdata)
        print(xdata.shape)
        for k in range(6):
            assertAAE(xdata[k, :], [k // 3, k % 3])
            self.assertTrue(ymap[xdata[k, 0], xdata[k, 1]] == k)
        ydata = ia.getydata(ymap)
        print(ydata)
        ydata += 1
        ymap1 = ia.resizeData(ydata)
        print(ymap1)
        assertAAE(ymap1, ymap + 1)

        xy0 = ia.getPositions(ymap, center=False)
        print(xy0)
        self.assertTrue(xy0[0, 0] == 0 and xy0[5, 1] == 2)

        xy1 = ia.getPositions(ymap)
        print(xy1)
        assertAAE(xy1, xy0 + 0.5)

        xy2 = ia.getPositions(ymap, deproject=numpy.log)
        print(xy2)
        assertAAE(xy2, numpy.log(xy1))
    def testImageAssistant3dC(self):

        print("====ImageAssistant 3d C ===================")
        ymap = numpy.arange(24, dtype=float).reshape(2, 3, 4)
        print(ymap)
        ia = ImageAssistant(order='C')
        xdata = ia.getIndices(ymap)
        print(xdata)
        ydata = ia.getydata(ymap)
        print(ydata)
        ydata += 1
        ymap1 = ia.resizeData(ydata, shape=ymap.shape)
        print(ymap1)
        assertAAE(ymap1, ymap + 1)
Esempio n. 18
0
    def testNormalize(self):
        """
        test normalized parameter, alternative for keepfixed.

        1.

        2.

        """
        plot = self.doplot

        print("\n   Fitter Test 2 Normalize  \n")
        model = PolynomialModel(2)

        sm = PowerModel(1)

        numpy.random.seed(2345)
        x = numpy.linspace(0.0, 10.0, 101)
        y = 1.0 + 0.5 * x - 0.2 * x * x + numpy.random.randn(101) * 0.1

        sm += model  ## degenerate model par[0] == par[2]

        print(sm)

        fitter = Fitter(x, sm)

        self.assertTrue(sm.npchain == 4)

        print(fmt(fitter.hessian))

        ppp = fitter.fit(y, plot=self.doplot)
        print(ppp)
        #        self.assertRaises( numpy.linalg.linalg.LinAlgError, fitter.fit, y )

        fitter.normalize([0.0, 0.0, 1.0, 0.0], 1.0)  ## fix par[2] at 1.0

        print(fmt(fitter.hessian))
        print(fmt(fitter.covariance))

        par = fitter.fit(y, plot=self.doplot)

        print("param = ", sm.parameters)
        assertAAE(par[2], 1.0)
        assertAAE(par, [-0.5, 1.0, 1.0, -0.2], 1)

        chisq = fitter.chisq
        print("chisq = %f" % (chisq))

        std = fitter.standardDeviations
        print("stdev = ", std)
Esempio n. 19
0
    def testVector(self):
        print("====testVector======================")
        x = np.arange(10, dtype=float)
        y = np.random.rand(10)
        w = np.ones(10, dtype=float)
        pm = PolynomialModel(1)
        bf = BaseFitter(x, pm)
        v1 = bf.getVector(y)
        print(v1)
        v2 = bf.getVector(y, index=[0])
        print(v2)
        v3 = bf.getVector(y, index=[1])
        print(v3)

        assertAAE(v1, np.append(v2, v3))
Esempio n. 20
0
    def testcr1(self):

        print("===== cyclic results 1 ================")

        mdl = PolynomialModel(1)
        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(mdl)

        r0 = numpy.linspace(-0.7, 1.5, 12)

        r1 = problem.cyclicCorrection(r0)

        assertAAE(r0, r1)
Esempio n. 21
0
    def testcr5(self):

        print("===== cyclic results 5 ================")

        mdl = PhaseModel(cyclic={0: 1.0, 2: 1.2})
        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)

        r0 = numpy.linspace(-0.8, 1.5, 12).reshape(3, 4).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))
        self.assertTrue(all(numpy.abs(r1[:, 2]) < 0.6))
    def testImageAssistantF(self):

        print("====ImageAssistant F ===================")
        ymap = numpy.arange(6, dtype=float).reshape(2, 3)
        print(ymap)
        ia = ImageAssistant(order='F')
        xdata = ia.getIndices(ymap)
        print(xdata)
        for k in range(6):
            assertAAE(xdata[k, :], [k % 3, k // 3])
            self.assertTrue(ymap[xdata[k, 1], xdata[k, 0]] == k)
        ydata = ia.getydata(ymap)
        print(ydata)
        ydata += 1
        ymap1 = ia.resizeData(ydata, shape=ymap.shape)
        print(ymap1)
        assertAAE(ymap1, ymap + 1)
    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("")
Esempio n. 24
0
    def testImageAssistantC(self, plot=False):

        print("====ImageAssistant C ===================")
        ymap = numpy.arange(6, dtype=float).reshape(2, 3)
        print(ymap)
        ia = ImageAssistant()
        xdata = ia.getIndices(ymap)
        print(xdata)
        print(xdata.shape)
        for k in range(6):
            assertAAE(xdata[k, :], [k // 3, k % 3])
            self.assertTrue(ymap[xdata[k, 0], xdata[k, 1]] == k)
        ydata = ia.getydata(ymap)
        print(ydata)
        ydata += 1
        ymap1 = ia.resizeData(ydata)
        print(ymap1)
        assertAAE(ymap1, ymap + 1)
    def testGaussErrorDistribution(self):

        print("=======   Test Gauss Error Distribution  ==================")

        poly = PolynomialModel(1)
        param = numpy.asarray([1, 10, 1], dtype=float)
        ged = GaussErrorDistribution(self.x, self.data)
        self.assertTrue(ged.acceptWeight())

        #   data = { -11, -9, -5, -5, -1, 1, 1, 5, 5, 8, 11 }
        #   f( x ) = { -10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10 }
        #   f - d=     1   1  -1   1  -1 -1  1 -1  1  0  -1

        chisq = ged.getChisq(ged.getResiduals(poly, param[:2]), param[2])
        print("chisq = %8.3f" % (chisq))
        logL = ged.logLikelihood(poly, param)
        altL = -0.5 * (11 * math.log(2 * math.pi) + chisq)
        print("logL  = %8.3f  %8.3f" % (logL, altL))
        assertAAE(logL, altL)

        scale = 0.1
        param[2] = scale
        logL = ged.logLikelihood(poly, param)
        altL = -11 * (0.5 * math.log(2 * math.pi) +
                      math.log(scale)) - 0.5 * chisq / (scale * scale)
        print("logL  = %8.3f  %8.3f" % (logL, altL))
        assertAAE(logL, altL)

        scale = 1.0
        param[2] = scale
        fitIndex = numpy.arange(3)
        dL = ged.partialLogL(poly, param, fitIndex)
        nL = ged.numPartialLogL(poly, param, fitIndex)
        print("partial = ", dL)
        print("numpart = ", nL)
        assertAAE(dL, nL, 5)

        scale = 0.5
        param[2] = scale
        ged.weights = self.wgt
        dL = ged.partialLogL(poly, param, fitIndex)
        nL = ged.numPartialLogL(poly, param, fitIndex)
        print("partial = ", dL)
        print("numpart = ", nL)
        assertAAE(dL, nL, 5)

        scale = 1.0
        for i in range(11):
            param = numpy.asarray([i - 5, 5, 1], dtype=float)
            print(param, "  :  ", end="")
            for k in range(9):
                print(" %8.3f" % ged.logLikelihood(poly, param), end="")
                param[1] += 1
            print("")
    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)
Esempio n. 27
0
    def testHessian(self):
        print("====testHessian======================")
        x = np.arange(10, dtype=float)
        pm = PolynomialModel(1)
        bf = BaseFitter(x, pm)
        y = np.ones(10, dtype=float)
        w = np.ones(10, dtype=float)
        p = np.asarray([1.0, 1.0])

        h0 = bf.hessian
        print(h0)
        h1 = bf.getHessian()
        print(h1)
        h2 = bf.getHessian(params=p)
        print(h2)
        h3 = bf.getHessian(weights=w, params=p)
        print(h3)
        assertAAE(h0, h1)
        assertAAE(h1, h2)
        assertAAE(h1, h3)
        index = 0
        h4 = bf.getHessian(weights=w, params=p, index=index)
        print(h4)
        index = 1
        h4 = bf.getHessian(weights=w, params=p, index=index)
        print(h4)
        index = [0, 1]
        w /= 2
        h4 = bf.getHessian(weights=w, params=p, index=index)
        print(h4)
        print(bf.sumwgt)

        ih = bf.getInverseHessian()
        print(ih)

        dia = np.inner(h1, ih)
        print(dia)
        assertAAE(dia, np.eye(2))
    def testPoissonErrorDistribution(self):
        print("====== Test Poisson Error Distribution ======================")
        poly = PolynomialModel(1)
        param = numpy.asarray([12, 10], dtype=float)
        data = numpy.asarray(self.data + 12, dtype=int)
        print("Data : ", data)
        ped = PoissonErrorDistribution(self.x, data)
        self.assertFalse(ped.acceptWeight())

        logL = ped.logLikelihood(poly, param)
        print("logL  = %8.3f" % (logL))
        scale = 0.1
        logL = ped.logLikelihood(poly, param)
        mok = poly.result(self.x, param)
        altL = numpy.sum(data * numpy.log(mok) - mok - logFactorial(data))

        print("logL  = %8.3f  %8.3f" % (logL, altL))
        assertAAE(logL, altL)

        logL = ped.logLikelihood(poly, [-5, 0])
        print("logL  = %8.3f" % (logL))
        self.assertTrue(math.isinf(logL))

        scale = 1.0
        fitIndex = [0, 1]
        dL = ped.partialLogL(poly, param, fitIndex)
        nL = ped.numPartialLogL(poly, param, fitIndex)
        print("partial = ", dL)
        print("numpart = ", nL)
        assertAAE(dL, nL, 5)

        scale = 0.5
        dL = ped.partialLogL(poly, param, fitIndex)
        nL = ped.numPartialLogL(poly, param, fitIndex)
        print("partial = ", dL)
        print("numpart = ", nL)
        assertAAE(dL, nL, 5)

        scale = 1.0
        for i in range(10):
            param = numpy.asarray([8 + i, 4], dtype=float)
            print(param, "  :  ", end="")
            for k in range(9):
                print(" %8.3f" % ped.logLikelihood(poly, param), end="")
                param[1] += 1
            print("")
    def testCauchyErrorDistribution(self):
        print("\n   Test Cauchy Error Distribution\n")
        poly = PolynomialModel(1)
        ced = CauchyErrorDistribution(self.x, self.data)
        self.assertFalse(ced.acceptWeight())

        param = numpy.asarray([1, 10, 1], dtype=float)
        logL = ced.logLikelihood(poly, param)
        print("logL  =  %8.3f" % (logL))
        scale = 0.1
        s2 = scale * scale
        param[2] = scale
        logL = ced.logLikelihood(poly, param)
        altL = math.log(scale / math.pi) * len(self.data)
        res = ced.getResiduals(poly, param[:2])
        altL -= numpy.sum(numpy.log(s2 + res * res))
        print("logL  =  %8.3f  alt %8.3f" % (logL, altL))
        assertAAE(logL, altL)

        scale = 1.0
        param[2] = scale
        fi = [0, 1, 2]
        dL = ced.partialLogL(poly, param, fi)
        nL = ced.numPartialLogL(poly, param, fi)
        print("params  = ", param, scale)
        print("partial = ", dL)
        print("numpart = ", nL)
        assertAAE(dL, nL, 2)

        scale = 0.5
        param[2] = scale
        dL = ced.partialLogL(poly, param, fi)
        nL = ced.numPartialLogL(poly, param, fi)
        print("params  = ", param, scale)
        print("partial = ", dL)
        print("numpart = ", nL)
        assertAAE(dL, nL, 2)

        for i in range(11):
            param = numpy.asarray([i - 5, 5, 1], dtype=float)
            print(param, ":  ", end="")
            for k in range(9):
                print(" %8.3f" % ced.logLikelihood(poly, param), end="")
                param[1] += 1
            print("")
Esempio n. 30
0
    def test4(self):

        print("****** STELLAR ORBIT test 4 ***************")
        m = StellarOrbitModel(spherical=False)

        p = math.pi
        rho = numpy.array([0, 1, 1, 1, 1, 3, 2.4])
        phi = numpy.array([0, 0, p, p / 2, p / 4, p / 6, 4.3])
        rp0 = numpy.append(rho, phi).reshape(2, -1).transpose()

        xy0 = m.toRect(rp0)

        print("x0   ", fma(xy0[:, 0]))
        print("y0   ", fma(xy0[:, 1]))

        rp1 = m.toSpher(xy0)

        print("r1   ", fma(rp1[:, 0]))
        print("t1   ", fma(rp1[:, 1]))

        xy1 = m.toRect(rp1)

        print("x1   ", fma(xy1[:, 0]))
        print("y1   ", fma(xy1[:, 1]))

        rp2 = m.toSpher(xy1)

        print("r2   ", fma(rp2[:, 0]))
        print("t2   ", fma(rp2[:, 1]))

        xy2 = m.toRect(rp2)

        print("x2   ", fma(xy2[:, 0]))
        print("y2   ", fma(xy2[:, 1]))

        rp3 = m.toSpher(xy2)

        print("r3   ", fma(rp3[:, 0]))
        print("t3   ", fma(rp3[:, 1]))

        #        assertAAE( xy0, xy1 )
        assertAAE(xy1, xy2)
        assertAAE(rp1, rp2)
        assertAAE(rp2, rp3)