コード例 #1
0
ファイル: covariance2.py プロジェクト: pombredanne/procgraph
class CovarianceRemember(Block):
    """ Quick hack to remember covariance across executions """

    Block.alias("covariance_rem")

    Block.config("filename", default="cov_remember.pickle")
    Block.input("x")
    Block.output("cov_x")

    def init(self):
        filename = self.config.filename
        if os.path.exists(filename):
            self.info("Loading state from filename %r" % filename)
            self.state = safe_pickle_load(filename)
        else:
            from boot_agents.utils.mean_covariance import MeanCovariance

            self.state = MeanCovariance()

    def update(self):
        x = self.input.x.astype("float32")
        self.state.update(x)

        self.output.cov_x = self.state.get_covariance()
        safe_pickle_dump(self.state, self.config.filename)  # @UndefinedVariable
コード例 #2
0
    def init(self, boot_spec):
        ExpSwitcher.init(self, boot_spec)
        # TODO: check float
        if len(boot_spec.get_observations().shape()) != 1:
            raise UnsupportedSpec('I assume 2D signals.')

        self.yy = Expectation()
        self.ylogy = Expectation()
        self.einvy = Expectation()
        self.ey = Expectation()
        self.elogy = Expectation()

        self.covy = MeanCovariance()
        self.covfy = MeanCovariance()
コード例 #3
0
ファイル: covariance2.py プロジェクト: pombredanne/procgraph
    def init(self):
        filename = self.config.filename
        if os.path.exists(filename):
            self.info("Loading state from filename %r" % filename)
            self.state = safe_pickle_load(filename)
        else:
            from boot_agents.utils.mean_covariance import MeanCovariance

            self.state = MeanCovariance()
コード例 #4
0
class EstStatsTh(ExpSwitcher):
    ''' 

    '''

    def init(self, boot_spec):
        ExpSwitcher.init(self, boot_spec)
        # TODO: check float
        if len(boot_spec.get_observations().shape()) != 1:
            raise UnsupportedSpec('I assume 2D signals.')

        self.yy = Expectation()
        self.ylogy = Expectation()
        self.einvy = Expectation()
        self.ey = Expectation()
        self.elogy = Expectation()

        self.covy = MeanCovariance()
        self.covfy = MeanCovariance()
         
    def merge(self, other):
        self.yy.merge(other.yy)
        self.ylogy.merge(other.ylogy)
        self.einvy.merge(other.einvy)
        self.ey.merge(other.ey)
        self.elogy.merge(other.elogy)
        self.covy.merge(other.covy)
        self.covfy.merge(other.covfy)
        
    def process_observations(self, obs):
        y = obs['observations']
        n = y.size
#         # XXX
#         which = np.array(range(y.size)) < 100
#         y[which] = (y * y)[which]
        
        dt = obs['dt'].item()
        z = y == 0
        y[z] = 0.5
        yy = outer(y, y)
        dt = 1
        logy = np.log(y)
        
        # TMP 
        logy = y * y 
#         logy[:int(n / 4)] = y[:int(n / 4)] 

        ylogy = outer(logy, y)
        
        self.yy.update(yy, dt)
        self.ylogy.update(ylogy, dt)

        invy = 1.0 / y
        self.einvy.update(invy, dt)
        self.ey.update(y, dt)
        self.elogy.update(logy, dt)

        self.covy.update(y, dt)
        self.covfy.update(logy, dt)

    def publish(self, pub):
#         pub.text('warn', 'using y^3')
        yy = self.yy.get_value()
        ylogy = self.ylogy.get_value()
        
        def symmetrize(x):
            return 0.5 * (x + x.T)
        yy = symmetrize(yy)
        ylogy = symmetrize(ylogy)
        
        
        with pub.subsection('yy') as sub:
            sub.array_as_image('val', yy)
            pub_svd_decomp(sub, yy)
            pub_eig_decomp(sub, yy)
            
        with pub.subsection('ylogy') as sub:
            sub.array_as_image('ylogy', ylogy)
            pub_svd_decomp(sub, ylogy)
            pub_eig_decomp(sub, ylogy)
            
        f = pub.figure()
        with f.plot('both') as pylab:
            pylab.plot(yy.flat, ylogy.flat, '.')
            pylab.xlabel('yy')
            pylab.ylabel('ylogy')
            pylab.axis('equal')
        
        D = get_deriv_matrix(yy.shape[0])
        inv = np.linalg.inv(yy)
        x = np.dot(ylogy, inv)
        
        f.array_as_image('inv', inv)
        f.array_as_image('ylogy_times_inverse', x)
        f.array_as_image('D', D)
        f.array_as_image('Dlog', np.dot(D, x))
        
        diag = np.diag(x)
        einvy = self.einvy.get_value()
        elogy = self.elogy.get_value()
        ey = self.ey.get_value()
        f = pub.figure()
        with f.plot('einvy', caption='E{1/y}') as pylab:
            pylab.plot(einvy, 's')
            y_axis_set_min(pylab, 0)
        with f.plot('elogy', caption='E{logy}') as pylab:
            pylab.plot(elogy, 's')
            # y_axis_set_min(pylab, 0)
        with f.plot('ey', caption='E{y}') as pylab:
            pylab.plot(ey, 's')
            y_axis_set_min(pylab, 0)
        with f.plot('diag', caption='diagonal of x') as pylab:
            pylab.plot(diag, 's')