Ejemplo n.º 1
0
 def tearDown(self):
     '''
     plot the log likelihood the sampler used with the sampled points
     and the true log likelihood
     '''
     
     # allocating memory
     x = np.arange(-10, 10, 0.05)
     n = len(x)
     f = np.zeros( n )
     
     # calculate the curves for the given input
     for j in range(0,n):    
         
         # do kriging, get avg value and std dev
         v = kg.kriging(x[j] ,self.CFG) 
         f[j] = v[0] # set the interpolant
         
     
     # do all the plotting here
     curve1  = plt.plot(x, f, label = "kriged value")
     curve2  = plt.plot( self.CFG.X, self.CFG.F, 'bo', label = "sampled points ")
     curve3  = plt.plot(x, truth.gaussian1D(x), label = "true log-likelihood")
     
     plt.setp( curve1, 'linewidth', 3.0, 'color', 'k', 'alpha', .5 )
     
     
     plt.legend(loc=1,prop={'size':7})    
     plt.title("Kriging with bounds using " + self.CFG.algType.getDescription() )
     plt.savefig("graphics/Test_Gaussian: kriged LL")
     plt.close()
Ejemplo n.º 2
0
    def testPrior(self):

        # allocating memory
        x = np.arange(-self.CFG.M, self.CFG.M, 0.05)
        n = len(x)
        f = np.zeros(n)
        true = np.zeros(n)
        prior = np.ones(n)

        # calculate the curves for the given input
        for j in range(0, n):

            # do kriging, get avg value and std dev
            v = kg.kriging(x[j], self.CFG)
            f[j] = (v[0])  # set the interpolant
            prior[j] = self.CFG.prior(x[j])  # set the limiting curve
            true[j] = self.CFG.LL(x[j])

        #move to normal, non-exponential, scale
        fe = np.exp(f)
        priore = np.exp(prior)
        truee = np.exp(true)
        Fe = np.exp(np.asarray(self.CFG.F))
        X = np.asarray(self.CFG.X)

        # create a figure with two subplts
        fig, (ax0, ax1) = plt.subplots(nrows=2, sharex=True)

        # first plot
        curve1 = ax0.plot(x, f, label="kriged LL")
        curve2 = ax0.plot(x, true, label="true LL")
        #curve3  = ax0.plot(x, prior, label = "prior log-likelihood")
        curve4 = ax0.plot(self.CFG.X,
                          self.CFG.F,
                          'bo',
                          label="sampled points ")

        ax0.setp(curve1, 'linewidth', 3.0, 'color', 'k', 'alpha', .5)
        ax0.setp(curve2, 'linewidth', 1.5, 'color', 'r', 'alpha', .5)
        ax0.setp(curve4, 'linewidth', 1.5, 'color', 'b', 'alpha', .5)

        ax0.legend(loc=1, prop={'size': 7})
        ax0.title("Kriged log likelihood")

        # scond plot
        curve5 = ax1.plot(x, fe, label="exp(kriged LL)")
        curve6 = ax1.plot(x, true, label="unnormalized density")
        #curve7  = ax1.plot(x, prior, label = "prior log-likelihood")
        curve8 = ax1.plot(X, Fe, 'bo', label="sampled points ")

        ax1.setp(curve5, 'linewidth', 3.0, 'color', 'k', 'alpha', .5)
        ax1.setp(curve6, 'linewidth', 1.5, 'color', 'r', 'alpha', .5)
        ax1.setp(curve8, 'linewidth', 1.5, 'color', 'b', 'alpha', .5)

        # save
        os.system("mkdir graphics")
        fig.savefig("graphics/Test_Density: Kriged LL and resulting density")

        plt.close()
Ejemplo n.º 3
0
    def testPrior(self):
        
        # allocating memory
        x = np.arange(-self.CFG.M, self.CFG.M, 0.05)
        n = len(x)
        f = np.zeros( n )
        true = np.zeros( n )
        prior = np.ones( n )

        # calculate the curves for the given input
        for j in range(0,n):    
            
            # do kriging, get avg value and std dev
            v = kg.kriging(x[j] , self.CFG) 
            f[j] =  (v[0]) # set the interpolant
            prior[j] = self.CFG.prior(x[j])   # set the limiting curve
            true[j]  = self.CFG.LL(   x[j])  
        
        #move to normal, non-exponential, scale
        fe = np.exp(f)
        priore = np.exp(prior)
        truee = np.exp(true)
        Fe = np.exp(np.asarray( self.CFG.F ))
        X =  np.asarray( self.CFG.X )
        
        # create a figure with two subplts
        fig, (ax0, ax1) = plt.subplots(nrows=2, sharex=True)

        # first plot
        curve1  = ax0.plot(x, f, label = "kriged LL")
        curve2  = ax0.plot(x, true, label = "true LL")
        #curve3  = ax0.plot(x, prior, label = "prior log-likelihood")
        curve4 =  ax0.plot(  self.CFG.X ,    self.CFG.F  , 'bo', label = "sampled points ")
        
        ax0.setp( curve1, 'linewidth', 3.0, 'color', 'k', 'alpha', .5 )
        ax0.setp( curve2, 'linewidth', 1.5, 'color', 'r', 'alpha', .5 )
        ax0.setp( curve4, 'linewidth', 1.5, 'color', 'b', 'alpha', .5 )
        
        ax0.legend(loc=1,prop={'size':7})    
        ax0.title("Kriged log likelihood")
        
        # scond plot
        curve5  = ax1.plot(x, fe, label = "exp(kriged LL)")
        curve6  = ax1.plot(x, true, label = "unnormalized density")
        #curve7  = ax1.plot(x, prior, label = "prior log-likelihood")
        curve8 =  ax1.plot(  X ,   Fe  , 'bo', label = "sampled points ")
        
        ax1.setp( curve5, 'linewidth', 3.0, 'color', 'k', 'alpha', .5 )
        ax1.setp( curve6, 'linewidth', 1.5, 'color', 'r', 'alpha', .5 )
        ax1.setp( curve8, 'linewidth', 1.5, 'color', 'b', 'alpha', .5 )
        
        # save
        os.system("mkdir graphics")
        fig.savefig("graphics/Test_Density: Kriged LL and resulting density")
        
        
        
        plt.close()
Ejemplo n.º 4
0
 def testSimple(self):
     
     # the center of mass of the x1,...,x4 is (0,0)
     s = np.array( [0, 0] )
     
     # kriging for this center ...
     b , c = kg.kriging(s, self.CFG)
     
     # ... should be the average of the values (f1,...,f4) at the points
     self.assertTrue(np.allclose( np.array( [0] ) , b ))
Ejemplo n.º 5
0
    def testSimple(self):

        # the center of mass of the x1,...,x4 is (0,0)
        s = np.array([0, 0])

        # kriging for this center ...
        b, c = kg.kriging(s, self.CFG)

        # ... should be the average of the values (f1,...,f4) at the points
        self.assertTrue(np.allclose(np.array([0]), b))
Ejemplo n.º 6
0
    def testMovie1D(self):

        # the true log-likelihood function
        f = self.CFG.LL

        # the size of the box outside of which the probability is zero
        M = self.CFG.M

        # the bounds on the plot axes
        # CHANGE THESE IF STUFF HAPPEN OUTSIDE THE FRAME
        xMin = -M
        xMax = M
        yMax = 100
        yMin = -300

        # all the x values for which we plot
        x = np.arange(xMin, xMax, 0.05)

        # we create each frame many times, so the movie is slower and easier to watch
        delay = 5

        # The number of evaluations of the true likelihood
        # CHANGE THIS IF YOU WANT A
        nf = 12

        # allocate memory for the arrays to be plotted
        kriged = np.zeros(x.shape)
        limit = np.zeros(x.shape)
        true = np.zeros(x.shape)

        # create frames for the ffmpeg programs
        for frame in range(nf + 1):

            # the current a value of the kriged interpolant "at infinity"

            limAtInfty, tmp = kg.setGetLimit(self.CFG)

            # create the kriged curve and the limit curve
            for j in range(0, len(x)):
                kriged[j] = kg.kriging(x[j], self.CFG)[0]
                limit[j] = limAtInfty
                true[j] = f(x[j])  # the real log likelihood

            # each frame is saved delay times, so we can watch the movie at reasonable speed
            #for k in range(delay):
            fig = plt.figure(frame * delay)

            # here we create the plot. nothing too fascinating here.
            curve1 = plt.plot(x, kriged, label="kriged log-likelihood")
            curve2 = plt.plot(x, true, label="true log-likelihood")
            curve3 = plt.plot(self.CFG.X,
                              self.CFG.F,
                              'bo',
                              label="sampled points ")
            curve4 = plt.plot(x, limit, 'g', label="kriged value at infinity")

            plt.setp(curve1, 'linewidth', 3.0, 'color', 'k', 'alpha', .5)
            plt.setp(curve2, 'linewidth', 1.5, 'color', 'r', 'alpha', .5)

            plt.axis([xMin, xMax, yMin, yMax])
            PlotTitle = 'Kriged Log-Likelihood Changes in Time. r = ' + str(
                self.CFG.r) + " Algorithm: " + self.CFG.algType.getDescription(
                )
            plt.title(PlotTitle)
            textString = 'using  ' + str(frame) + ' sampled points'
            plt.text(1.0, 1.0, textString)
            plt.legend(loc=1, prop={'size': 7})

            for k in range(delay):
                FrameFileName = "Data/Movie1DFrames/Frame" + str(
                    frame * delay + k) + ".png"
                plt.savefig(FrameFileName)
                if (frame * delay + k) % 10 == 0:
                    print("saved file " + FrameFileName + ".  " +
                          str(frame * delay + k) + " / " + str(nf * delay))

            plt.close(frame * delay)

            # IMPORTANT - we sample from the kriged log-likelihood. this is crucial!!!!
            self.sampler.sample()
Ejemplo n.º 7
0
    def testNoise(self):
        '''
        Do the same thing Test_plots does, only with noisy observations
        '''
        
        # allocating memory
        x = np.arange(-10, 10, 0.05)
        n = len(x)
        f = np.zeros( n )
        upper = np.zeros( n )
        lower = np.zeros( n )
        limit = np.ones( n )

        # locations where we know the function value
        X = []        
        x1 =  pt.PointWithError( [ 1.1 ] , 0.12 )
        x2 =  pt.PointWithError( [ 1.0 ] , 0.52 )
        x3 =  pt.PointWithError( [ -1.1] , 0.06 )
        x4 =  pt.PointWithError( [ -3.0] , 0.1 )
        X.append(x1)
        X.append(x2)
        X.append(x3)
        X.append(x4)

        # create the container object and populate it...
        CFG = cfg.Config()
        for v in X: 
            CFG.addPair(v , truth.trueLL(v) + v.getError()*np.random.randn()  ) #... with (point, value) pair...
        CFG.setType(type.RASMUSSEN_WILLIAMS) #... with the algorithm we use...
        #CFG.setType(type.AUGMENTED_COVARIANCE)
        #CFG.setType(type.COVARIANCE)
        
        r = 1.3
        CFG.setR(r) # ...with the location scale hyper parameter r...
        CFG.setMatrices() # ... and with the matrices the kriging procedure uses
        
        # the value of the kriged function "at infinity"
        limAtInfty, tmp = kg.setGetLimit(CFG)

        # calculate the curves for the given input
        for j in range(0,n):    
            
            # do kriging, get avg value and std dev
            v = kg.kriging(x[j] ,CFG) 
            f[j] = v[0] # set the interpolant
            upper[j] = v[0] + 1.96*v[1] # set the upper bound
            lower[j] = v[0] - 1.96*v[1] # set lower bound
            limit[j] = limAtInfty # set the limiting curve
        
        # do all the plotting here
        curve1  = plt.plot(x, f, label = "kriged value")
        curve2  = plt.plot(x, upper, label = "1.96 standard deviations")
        curve3  = plt.plot(x, lower)
        curve4  = plt.plot(x, limit, label = "kriged value at infinity")
        curve5 =  plt.plot( CFG.X, CFG.F, 'bo', label = "sampled points ")
        
        plt.setp( curve1, 'linewidth', 3.0, 'color', 'k', 'alpha', .5 )
        plt.setp( curve2, 'linewidth', 1.5, 'color', 'r', 'alpha', .5 )
        plt.setp( curve3, 'linewidth', 1.5, 'color', 'r', 'alpha', .5 )
        plt.setp( curve4, 'linewidth', 1.5, 'color', 'b', 'alpha', .5 )
        
        plt.legend(loc=1,prop={'size':7})    
        plt.title("Kriging with noise using " + CFG.algType.getDescription() )
        os.system("mkdir graphics")     
        plt.savefig("graphics/Test_Noise: Kriged noisy LL")
        plt.close()
Ejemplo n.º 8
0
 def testUniform(self):
     #self.assertTrue(    np.allclose( np.array([1.5]) , kg.kriging(np.array([5.0]),self.a )[0] )      )
     self.assertTrue(
         np.allclose(np.array([1.5]),
                     kg.kriging(np.array([20.0]), self.CFG)[0]))
Ejemplo n.º 9
0
   def testMovie1D(self):
       
       # the true log-likelihood function
       f = self.CFG.LL 
       
       # the size of the box outside of which the probability is zero
       M = self.CFG.M 
       
       # the bounds on the plot axes
       # CHANGE THESE IF STUFF HAPPEN OUTSIDE THE FRAME
       xMin = -M
       xMax = M
       yMax = 100
       yMin = -300
       
       # all the x values for which we plot
       x = np.arange(xMin, xMax, 0.05)
       
       # we create each frame many times, so the movie is slower and easier to watch
       delay = 5
       
       # The number of evaluations of the true likelihood
       # CHANGE THIS IF YOU WANT A 
       nf    = 12       
       
       # allocate memory for the arrays to be plotted
       kriged = np.zeros( x.shape )
       limit = np.zeros( x.shape )
       true = np.zeros( x.shape )
       
       
       # create frames for the ffmpeg programs
       for frame in range (nf+1):
           
           # the current a value of the kriged interpolant "at infinity"
           
           limAtInfty , tmp= kg.setGetLimit(self.CFG)
           
           # create the kriged curve and the limit curve
           for j in range(0,len(x)):
               kriged[j] = kg.kriging(x[j] ,self.CFG)[0]
               limit[j] = limAtInfty
               true[j] = f(x[j]) # the real log likelihood
           
           # each frame is saved delay times, so we can watch the movie at reasonable speed    
           #for k in range(delay):
           fig = plt.figure( frame*delay )
           
           # here we create the plot. nothing too fascinating here.
           curve1  = plt.plot(x, kriged , label = "kriged log-likelihood")
           curve2 =  plt.plot(x, true, label = "true log-likelihood")
           curve3 =  plt.plot( self.CFG.X, self.CFG.F, 'bo', label = "sampled points ")
           curve4  = plt.plot(x, limit, 'g', label = "kriged value at infinity")
   
           plt.setp( curve1, 'linewidth', 3.0, 'color', 'k', 'alpha', .5 )
           plt.setp( curve2, 'linewidth', 1.5, 'color', 'r', 'alpha', .5 )
   
           
           plt.axis([xMin, xMax, yMin, yMax])
           PlotTitle = 'Kriged Log-Likelihood Changes in Time. r = ' + str(self.CFG.r) + " Algorithm: " + self.CFG.algType.getDescription()
           plt.title( PlotTitle )
           textString = 'using  ' + str(frame ) + ' sampled points' 
           plt.text(1.0, 1.0, textString)
           plt.legend(loc=1,prop={'size':7})  
           
           for k in range(delay):  
               FrameFileName = "Data/Movie1DFrames/Frame" + str(frame*delay + k) + ".png"
               plt.savefig(FrameFileName)
               if (frame*delay + k) % 10 == 0:
                   print( "saved file " + FrameFileName + ".  " + str(frame*delay + k) +  " / " + str(nf*delay) )
                   
           plt.close( frame*delay ) 
 
           # IMPORTANT - we sample from the kriged log-likelihood. this is crucial!!!!
           self.sampler.sample()
Ejemplo n.º 10
0
    def testMovie2D(self):
        '''
        create a 2D movie, based on the data we put in the container object 
        in the setUp method this method does all the graphics involved
        since this is a 2D running for lots of points might take a while
        '''
        
        # The number of evaluations of the true likelihood
        # CHANGE THIS FOR A LONGER MOVIE!!!
        nf    =  125   
        
        # the bounds on the plot axes
        # CHANGE THIS IF STUFF HAPPEN OUTSIDE THE MOVIE FRAME
        xMin = -self.M
        xMax = self.M
        zMax = 50
        zMin = -500
        
        # create the two meshgrids the plotter needs
        a  = np.arange(xMin, xMax, 0.4)
        b  = np.arange(xMin, xMax, 0.4)
        X, Y = np.meshgrid(a, b)
        
        # we create each frame many times, so the movie is slower and easier to watch
        delay = 4
        
        # allocate memory for the arrays to be plotted
        kriged = np.zeros( X.shape )
        
        # allocate a two dimensional point, for which we calculate kriged value
        p = np.zeros(2)
        
        # create frames for the ffmpeg programs
        for frame in range (nf+1):

            # create the kriged curve 
            for j in range(len(a)):
                for i in range(len(b)):
                    p[0] = X[j,i]
                    p[1] = Y[j,i]    
                    kriged[j,i] = kg.kriging( p ,self.CFG )[0]
                                
            # create surface plot
            fig1 = plt.figure( frame*2 )
           
            ax1 = fig1.add_subplot(111 , projection='3d')
            
            ax1.plot_wireframe(X, Y, kriged, rstride=10, cstride=10)
            ax1.set_xlim(xMin, xMax)
            ax1.set_ylim(xMin, xMax)
            ax1.set_zlim(zMin, zMax)
            
            xs = np.ravel( np.transpose( np.array( self.CFG.X ) )[0] )
            ys = np.ravel( np.transpose( np.array( self.CFG.X ) )[1] )
            zs = np.ravel( np.transpose( np.array( self.CFG.F ) )    )
            ax1.scatter(xs, ys, zs)
    
            PlotTitle1 = 'Kriged LL surface. ' + str(frame) + ' samples. r = ' + str(self.CFG.r) + " Algorithm: " + self.CFG.algType.getDescription()
            plt.title( PlotTitle1 )
            #textString = 'using  ' + str(frame ) + ' sampled points' 
            #plt.text( textString)
            plt.legend(loc=1,prop={'size':7}) 
            
            
            # create contour
            fig2 = plt.figure( frame*2 + 1 )
            ax2 = fig2.add_subplot(111) #, projection='2d')
            cs = ax2.contour(X, Y, kriged, levels = np.arange(zMin , zMax , 25)  ) 
            ax2.clabel(cs, fmt = '%.0f', inline = True) 
            ax2.scatter(xs, ys)
            PlotTitle2 = 'Kriged LL contours. ' + str(frame) + ' samples. r = ' + str(self.CFG.r) + " Algorithm: " + self.CFG.algType.getDescription()
            plt.title( PlotTitle2 )
            # save the plot several times
            for k in range(delay):   
                FrameFileName1 = "Data/Movie2DSurfaceFrames/Frame" + str(frame*delay + k) + ".png"
                FrameFileName2 = "Data/Movie2DContourFrames/Frame" + str(frame*delay + k) + ".png"

                fig1.savefig(FrameFileName1)
                fig2.savefig(FrameFileName2)

                if (frame*delay + k) % 10 == 0:
                    print( "saved " + FrameFileName1 + " and " +FrameFileName2 + ".  " + str(frame*delay + k) +  " / " + str((nf+1)*delay) )
            
            plt.close( frame*2     )
            plt.close( frame*2 + 1 )

            # IMPORTANT - we sample from the kriged log-likelihood. this is crucial!!!!
            self.sampler.sample() 
Ejemplo n.º 11
0
 def testUniform(self):
     #self.assertTrue(    np.allclose( np.array([1.5]) , kg.kriging(np.array([5.0]),self.a )[0] )      )
     self.assertTrue(    np.allclose( np.array([1.5]) , kg.kriging(np.array([20.0]),self.CFG )[0] )      )
Ejemplo n.º 12
0
    def setUp(self):
        '''
        here we set the test up
        this means putting all required information 
        inside the container object
        '''
        # create target directory
        os.system("mkdir graphics")

        # set seed for reproducibility
        np.random.seed(89)
        
        # create an instance of the container
        self.CFG = cfg.Config()
        
        # set the true log-likelihood to be a gaussian
        self.CFG.setLL(truth.gaussian1D)
        f = self.CFG.LL # call it f for short

        # use one initial point        
        p = np.array( [0.0] )
        self.CFG.addPair(p, f(p))
        
        # parameters of the run:
                
        M = 10.0 # outside the box of size M the probability is zero
        self.CFG.setM(M)
        
        r = 1.3 # the typical length scale of the kriging. a hyper parameter
        self.CFG.setR(r)

        # take and incorporate to data an initial sample:        
        self.CFG.setAddSamplesToDataSet( True ) #... tell the container it does so...
        
        # create the sampler
        self.sampler = smp.Sampler ( self.CFG )
        
        k =  45 # ...decide how many initial points we take to resolve the log-likelihood
        for j in range(0,k): 
            print( "Initial samples " + str(j+1) + " of " + str(k))
            self.sampler.sample() # ... sample, incorporate into data set, repeat k times.
        
        
        # plot kriged LL
                
        # allocating memory
        x = np.arange(-10, 10, 0.05)
        n = len(x)
        f = np.zeros( n )
        
        # calculate the curves for the given input
        for j in range(0,n):    
            
            # do kriging, get avg value and std dev
            v = kg.kriging(x[j] ,self.CFG) 
            f[j] = v[0] # set the interpolant
            
        
        # do all the plotting here
        curve1  = plt.plot(x, f, label = "kriged value")
        curve2  = plt.plot( self.CFG.X, self.CFG.F, 'bo', label = "sampled points ")
        curve3  = plt.plot(x, truth.gaussian1D(x), label = "true log-likelihood")
        
        plt.setp( curve1, 'linewidth', 3.0, 'color', 'k', 'alpha', .5 )
        
        
        plt.legend(loc=1,prop={'size':7})    
        plt.title("Kriging with bounds using " + self.CFG.algType.getDescription() )
        plt.savefig("graphics/Test_Gaussian: kriged LL")
        plt.close()