コード例 #1
0
ファイル: Test_Reproducible.py プロジェクト: yairdaon/Krig
    def testReproducibility(self):
        '''
        tests that using the same seed, we can reproduce results.
        note: this is what we did in the setUp method above EXCEPT
        the last two lines.
        '''
        
        np.random.seed(127)
        r = 1.0
        M = 10.0
        f = truth.trueLL
        #    Start the algorithm using these points
        StartPoints = []
        StartPoints.append( np.array( [ -1.5 ] ) )
        StartPoints.append( np.array( [  1.5 ] ) )

        #     Initializations of the algorithm
        CFG = cfg.Config()
        for point in StartPoints:
            CFG.addPair(point, f(point))
        CFG.setR(r)
        CFG.setM(M)
        CFG.setMatrices()
        
        sampler = smp.Sampler(CFG)
        sampler.sample()
        sampler.sample()
        
        # now we put the sample in y
        self.y = sampler.sample()   
        
        # and compare
        self.assertEqual(self.x, self.y)  
コード例 #2
0
ファイル: Test_Reproducible.py プロジェクト: yairdaon/Krig
 def testReproducibilityFails(self):
         '''
         tests that using different seeds, we cannot expect to
         reproduce results. 
         note: this is what we did in the testReproducibility method 
         above EXCEPT for the first line and the last.
         '''
         
         # Seed is commented, so we cannot expect reproducibility
         #np.random.seed(127)
         r = 1.0
         M = 10.0
         f = truth.trueLL
         #    Start the algorithm using these points
         StartPoints = []
         StartPoints.append( np.array( [ -1.5 ] ) )
         StartPoints.append( np.array( [  1.5 ] ) )
 
         #     Initializations of the algorithm
         CFG = cfg.Config()
         for point in StartPoints:
             CFG.addPair(point, f(point))
         CFG.setR(r)
         CFG.setM(M)
         CFG.setMatrices()
         
         sampler = smp.Sampler(CFG)
         sampler.sample()
         sampler.sample()
         
         # now we put the sample in y
         self.y = sampler.sample()   
         
         # and compare. they should be different!
         self.assertTrue( not np.array_equal(self.x, self.y) )        
コード例 #3
0
    def setUp(self):

        r = 1.0
        M = 25.0
        # create locations where values of log
        # likelihood are known
        X = []
        x1 = np.array([0.5])
        x2 = np.array([1.0])
        x3 = np.array([1.5])
        x4 = np.array([1.25])
        X.append(x1)
        X.append(x2)
        X.append(x3)
        X.append(x4)

        CFG = cfg.Config()

        # set some parameters
        CFG.setR(r)
        CFG.setM(M)

        # set all these known values to be the same
        for i in range(len(X)):
            CFG.addPair(X[i], np.array([1.5]))

        CFG.setMatrices()
        self.CFG = CFG
コード例 #4
0
ファイル: Test_Reproducible.py プロジェクト: yairdaon/Krig
    def setUp(self):
        
        np.random.seed(127)
        
        # parameters:
        r = 1.0 # length scale hyper parameter
        M = 10.0 # size of box with non-vanishing probability
        f = truth.trueLL # the real log-likelihood
        
        # start the algorithm using these points
        StartPoints = []
        StartPoints.append( np.array( [ -1.5 ] ) )
        StartPoints.append( np.array( [  1.5 ] ) )

        # creating the container object...
        CFG = cfg.Config()
        for point in StartPoints:
            CFG.addPair( point, f(point)) # ...populating it with (point,value) pairs...
        CFG.setR(r) # ...setting the hyper parametr r...
        CFG.setM(M) # ...setting the box size M...
        CFG.setMatrices() #... set the matrices we use for kriging...
        
        sampler = smp.Sampler(CFG)
        sampler.sample() # ... and sample two points using the given seed
        sampler.sample() # note: the sampler adds these points to the container a on its own
        
        # now we sample
        self.x = sampler.sample()
コード例 #5
0
    def setUp(self):
        '''
        set the test up. define points and their
        log likelihood
        '''

        x1 = np.matrix([0, 1])
        x2 = np.matrix([1, 0])
        x3 = np.matrix([0, -1])
        x4 = np.matrix([-1, 0])
        f1 = np.array([0])
        f2 = np.array([2])
        f3 = np.array([0])
        f4 = np.array([-2])

        # create the container object ...
        CFG = cfg.Config()

        # ...add the points to it ...
        CFG.addPair(x1, f1)
        CFG.addPair(x2, f2)
        CFG.addPair(x3, f3)
        CFG.addPair(x4, f4)

        # ...set the characteristic distance....
        CFG.setR(1.0)

        # ...and create the matrices used for kriging
        CFG.setMatrices()

        # keep the container in the right scope of the whole test
        self.CFG = CFG
コード例 #6
0
ファイル: Test_Sampler.py プロジェクト: yairdaon/Krig
    def setUp(self):
        '''
        set up the test. this means creating the container object 
        that holds all the data, settings, variables and flags 
        required for a successful run
        '''

        # for reproducibility purposes
        np.random.seed(5012)

        # the size of the box outside of which we have zero probability
        M = 2.0

        # the length scale of the covariance function. this is a hyper parameter.
        r = 1.0

        # create and populate the container object:
        CFG = cfg.Config()  # call the constructor...
        CFG.addPair(np.array([1.0, 1.0, 1.0]),
                    np.array([2.45]))  #...add data...
        CFG.setR(r)  # ...set the length scale hyper parameter...
        CFG.setM(M)  # ...set the box size...
        CFG.setMatrices()  # ...calcualte the matrices neede for kriging...
        CFG.LL = truth.norm2D
        self.CFG = CFG  # ... and keep the container in the scope of the test.

        self.sampler = smp.Sampler(CFG)
コード例 #7
0
ファイル: Test_Prior.py プロジェクト: yairdaon/Krig
    def setUp(self):

        # for reproducibility purposes
        np.random.seed(1243)

        # create the container object
        CFG = cfg.Config()

        # set plot bounds
        #self.M = 1

        # set prior loglikelihood to exponential
        #         prior = lambda x: -np.linalg.norm(x)
        #         CFG.setPrior(prior)

        # set true LL
        likelihood = truth.bigPoly1D
        CFG.setLL(likelihood)

        # use RW's algorithm
        CFG.setType(type.RASMUSSEN_WILLIAMS)

        # quick setup
        CFG.quickSetup(1)

        # create sampler...
        self.sampler = smp.Sampler(CFG)
        k = 7  # ...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.

        self.CFG = CFG
コード例 #8
0
    def setUp(self):
        '''
        this is where most of the setup is done for the movie
        '''
        
        # for reproducibility
        np.random.seed(1792) 
        
        # tell the OS to prepare for the movie and the frames
        os.system("mkdir Data")     
        os.system("mkdir Data/Movie2DSurfaceFrames")
        os.system("mkdir Data/Movie2DContourFrames")

        os.system("mkdir graphics")
        os.system("rm -f Data/Movie2DSurfaceFrames/*.png") 
        os.system("rm -f Data/Movie2DContourFrames/*.png")     
    
        
        
        #     Initializations of the container object
        
        CFG = cfg.Config()
        
        # The length scale parameter in the Gaussian process covariance function.
        r = 10
        CFG.setR(r) 
            
        # the size of the box outside of which the probability is zero
        self.M = 15
        CFG.setM(self.M)

        # we know the true log-likelihood in these points
        StartPoints = []
        StartPoints.append( np.array( [ 0 , 0 ] ) )
        StartPoints.append( np.array( [0.5,1.0] ) )
        
        # the true log-likelihood function
        # CHANGE THIS IF YOU WANT YOUR OWN LOG-LIKELIHOOD!!!        
        CFG.setLL( truth.norm2D )
        self.f = CFG.LL # the true log-likelihood function 
        
        for point in StartPoints:
            CFG.addPair( point, CFG.LL(point) )
        
        # we use algorithm 2.1 from Rasmussen & Williams book
        #CFG.setType( type.RASMUSSEN_WILLIAMS  )
        CFG.setType( type.AUGMENTED_COVARIANCE)
        
        # keep the container in scope so we can use it later
        self.sampler = smp.Sampler( CFG )
        self.CFG = CFG
コード例 #9
0
    def setUp(self):
        '''
        this is where most of the setup is done for the movie
        '''

        # tell the OS to prepare for the movie and the frames
        os.system("mkdir Data")
        os.system("mkdir Data/Movie1DFrames")
        os.system("mkdir graphics")
        os.system("rm -f Data/Movie1DFrames/*.png")

        # for reproducibility
        np.random.seed(1792)

        #     Initializations of the container object
        CFG = cfg.Config()

        # the true log-likelihood function
        CFG.LL = truth.bigPoly1D

        # make it remember the state of the PRNG
        CFG.state = np.random.get_state()

        # The length scale parameter in the Gaussian process covariance function.
        r = 1.3
        CFG.setR(r)

        # the size of the box outside of which the probability is zero
        M = 2.5
        CFG.setM(M)

        # we know the true log-likelihood in these points
        StartPoints = []
        StartPoints.append(np.array([0]))  #-0.5)*(M/2.0) )
        StartPoints.append(np.array([0.5]))  #-0.5)*(M/2.0) )
        ##StartPoints.append( np.array( [ -M/2.0 ]))
        ##StartPoints.append( np.array( [  M/2.0 ]))

        for point in StartPoints:
            CFG.addPair(point, CFG.LL(point))

        # we use algorithm 2.1 from Rasmussen & Williams book
        #CFG.setType( type.RASMUSSEN_WILLIAMS )
        #CFG.setType(type.AUGMENTED_COVARIANCE )
        CFG.setType(type.COVARIANCE)

        # keep the container in scope so we can use it later
        self.CFG = CFG
        self.sampler = smp.Sampler(self.CFG)
コード例 #10
0
ファイル: Test_Noise.py プロジェクト: yairdaon/Krig
    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()
コード例 #11
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()