def sample_normal_in_one_level(self,level,Avees,theta,mu,scale=None):
        """
        Modifies Avees and theta. 
        theta refers to the values in the cpa space.        
        """
       
        cpa_space = self.ms.L_cpa_space[level]   
        if mu is None:
            mu = cpa_space.get_zeros_theta()  

#            ipshell('oops')
#            raise NotImplementedError
        else:
            if len(mu) != cpa_space.d:
                raise ValueError(mu.shape,cpa_space.d)
            
        Sigma =  self.L_cpa_space_covs[level].cpa_cov  
        # sample in the subspace                
        if scale is None:
            theta[:] = draw_from_mvnormal(mean=mu,cov=Sigma)                   
        else:  
#            ipshell('debug')                        
#            raise ValueError("Is this code still current?")
   
#            theta[:] = draw_from_mvnormal(mean=mu,cov=scale**2 * self.L_cpa_space_cov[level])
            theta[:]=draw_from_mvnormal(mean=mu,cov=scale**2 * Sigma )
        
        # move to the joint Lie algebra
        cpa_space.theta2Avees(theta=theta,Avees=Avees)             
Exemplo n.º 2
0
    def __call__(self,
                 theta_current,
                 theta_proposed,
                 prob_larger=0.1 * 0,
                 nullify_cells=None):
        """
        Modifies theta_proposed
        """
        if nullify_cells:
            raise ValueError("That was a stupid idea. Don't do it.")
        theta = theta_proposed
        level = self.level
        ms = self.ms
        msp = self.msp
        cpa_space = ms.L_cpa_space[level]

        scale = self.scale
        if prob_larger == 0.0:
            pass
        else:
            u = np.random.random()
            if u > 1 - prob_larger:
                scale = self.scale * 10
            else:
                pass

        if nullify_cells is None:
            if 0:
                msp.sample_normal_in_one_level(level,
                                               cpa_space.Avees,
                                               theta,
                                               mu=theta_current,
                                               scale=scale)
            else:

                if self.use_local == False:
                    covs = msp.L_cpa_space_covs[level]
                    cpa_cov = covs.cpa_cov
                    cpa_cov_diagonal = np.diag(cpa_cov.diagonal())
                    #                ipshell('hi')
                    #                1/0
                    theta[:] = draw_from_mvnormal(mean=theta_current,
                                                  cov=scale**2 *
                                                  cpa_cov_diagonal)
                else:
                    if cpa_space.dim_domain != 1 and cpa_space.tess != 'I':
                        raise NotImplementedError(cpa_space.tess)

                    cpa_space.theta2Avees(theta_current)
                    Avees = cpa_space.Avees
                    dim_range = cpa_space.dim_range

                    velTess = cpa_space.zeros_velTess()
                    cpa_space.Avees2velTess(Avees=Avees, velTess=velTess)

                    #                    vals +=1000*np.random.standard_normal(vals.shape)

                    # TODO: switch to a more intelligent proposal

                    V = cpa_space.local_stuff.vert_tess[:, :cpa_space.
                                                        dim_domain]
                    scale_local_proposal = self.scale_local_proposal

                    for i in range(1):
                        idx = np.random.random_integers(0, len(velTess) - 1)
                        #                        1/0
                        #                        velTess[idx]+=1000*np.random.standard_normal(size=dim_range)

                        value = np.random.standard_normal(size=dim_range)
                        if cpa_space.dim_domain > 1:
                            s = scale_local_proposal * cpa_space.XMAXS.min()

                            velTess[idx] += s * value
                        else:

                            velTess[idx] += .01 * value

#                        velTess[idx]+=1*1.0*cpa_space.XMAXS.min()*np.random.standard_normal(size=dim_range)

#                        velTess[idx]+=1.0*cpa_space.XMAXS.min()*np.random.standard_normal(size=dim_range)

                        if any(cpa_space.zero_v_across_bdry):

                            # in principle, the constraint is only about
                            # normals to the bdry.
                            # however, if the variance is too large,
                            # the tangential comp can cause problems,
                            # in the sense it will drive everything
                            # to the corner. So use a hack: force zero.
                            # the projection later may still give values,
                            # but these are faily benign
                            # that are nonzero. Like I said, a hack.
                            for coo in range(cpa_space.dim_domain):
                                if V[idx, coo] in (0, cpa_space.XMAXS[coo]):
                                    velTess[idx] = 0

    #                                ipshell('hi')
    #                                1/0

                    cpa_space.project_velTess(velTess, velTess)
                    #                    ipshell('hi')
                    #                    1/0

                    cpa_space.velTess2Avees(velTess=velTess, Avees=Avees)
                    cpa_space.Avees2theta(theta=theta)

        if nullify_cells is not None:
            raise NotImplementedError
            covs = msp.L_cpa_space_covs[level]
            cpa_cov = covs.cpa_cov
            pa_cov = covs.pa_cov
            w = np.ones(cpa_space.d_no_constraints)
            w.reshape(cpa_space.nC, -1)[nullify_cells] = 0
            print w
            pa_cov2 = np.diag(w).dot(pa_cov).dot(np.diag(w))
            cpa_cov2 = cpa_space.B.T.dot(pa_cov2).dot(cpa_space.B)

            theta[:] = draw_from_mvnormal(mean=theta_current * 0,
                                          cov=scale**2 * cpa_cov2)
    def sample_normal_in_one_level_velTess(self,level,Avees=None,
                                           velTess=None,mu=None,scale=None):
        """
        Modifies Avees and velTess. 
        Unless Avees is None, in which case only 
        vellTess and cpa_space.Avees are modified.
            
        """
        
        if velTess is None:
            raise ValueError
        
        cpa_space = self.ms.L_cpa_space[level]   
        if mu is None:
            mu = cpa_space.zeros_velTess().ravel()  
        else:
            if len(mu) != cpa_space.d:
                raise ValueError(mu.shape,cpa_space.d)
        
        
        Sigma =  self.L_cpa_space_covs[level].velTess_cov_byB  
#        Sigma = self.L_cpa_space_covs[level].velTess_cov
        if Sigma.shape[0]!=len(mu):
            raise ValueError(Sigma.shape,mu.shape)
                  
        if scale is None:            
            velTess.ravel()[:] = draw_from_mvnormal(mean=mu,cov=Sigma)                   
        else:  
#            ipshell('debug')                        
#            raise ValueError("Is this code still current?")
   
#            theta[:] = draw_from_mvnormal(mean=mu,cov=scale**2 * self.L_cpa_space_cov[level])
            velTess.ravel()[:]=draw_from_mvnormal(mean=mu,cov=scale**2 * Sigma )
        
        if cpa_space.dim_domain==1:
            """
            Terrible hack... I need to project it instead
            But the differece is mild anyway.
            """
             
            if scale  is None:
                S = Sigma
            else:
                S = Sigma * scale**2
            if any(cpa_space.zero_v_across_bdry):
#                velTess[0,:]=velTess[-1,:]=0
                Nv = len(velTess)
                idx_y=[0,Nv-1]   
                velTess[idx_y,:]=0
                
                if 0:
                    idx_x=np.delete(np.arange(Nv),idx_y)
                    Sxgiveny = cond_gaussian.compute_cond_cov(Sigma,idx_x,idx_y)
                    # the mean and the value we condition on are zero
                    # so don't bother to compute cond mean. It is just zeros.
                    velTess[idx_x,0]=draw_from_mvnormal(mu[idx_x],Sxgiveny)
#                1/0
#                ipshell('h');1/0
                
        if Avees is not None:
            cpa_space.velTess2Avees(velTess=velTess,Avees=Avees)
Exemplo n.º 4
0
    def __call__(self,theta_current,theta_proposed,prob_larger=0.1*0,
                 nullify_cells=None):
        """
        Modifies theta_proposed
        """
        if nullify_cells:
            raise ValueError("That was a stupid idea. Don't do it.")
        theta = theta_proposed
        level=self.level
        ms = self.ms
        msp = self.msp
        cpa_space = ms.L_cpa_space[level]
        
        scale=self.scale
        if prob_larger==0.0:
            pass
        else:
            u = np.random.random()                                       
            if u>1-prob_larger:
                scale = self.scale * 10                
            else:
                pass
         
        if nullify_cells is None:    
            if 0:
                msp.sample_normal_in_one_level(level,cpa_space.Avees,theta,
                                          mu=theta_current,scale=scale)
            else:
               
                if self.use_local == False:
                    covs=msp.L_cpa_space_covs[level]
                    cpa_cov = covs.cpa_cov   
                    cpa_cov_diagonal = np.diag(cpa_cov.diagonal())
    #                ipshell('hi')
    #                1/0
                    theta[:]=draw_from_mvnormal(mean=theta_current,cov=scale**2 * cpa_cov_diagonal)
                else:
                    if cpa_space.dim_domain!=1 and cpa_space.tess !='I':
                        raise NotImplementedError(cpa_space.tess)
                     
                    cpa_space.theta2Avees(theta_current)
                    Avees=cpa_space.Avees    
                    dim_range = cpa_space.dim_range
                     
                    
                    velTess = cpa_space.zeros_velTess()
                    cpa_space.Avees2velTess(Avees=Avees,velTess=velTess)
                    
#                    vals +=1000*np.random.standard_normal(vals.shape)
                    
                    # TODO: switch to a more intelligent proposal
                    
                    V = cpa_space.local_stuff.vert_tess[:,:cpa_space.dim_domain]
                    scale_local_proposal=self.scale_local_proposal
                    
                    for i in range(1):
                        idx = np.random.random_integers(0,len(velTess)-1)
#                        1/0
#                        velTess[idx]+=1000*np.random.standard_normal(size=dim_range)
                        
                        
                        value = np.random.standard_normal(size=dim_range)
                        if cpa_space.dim_domain>1:
                            s=scale_local_proposal*cpa_space.XMAXS.min()
                            
                           
                            
                            velTess[idx]+=s*value
                        else:
                            
                            velTess[idx]+=.01*value
                        
                        
#                        velTess[idx]+=1*1.0*cpa_space.XMAXS.min()*np.random.standard_normal(size=dim_range)

#                        velTess[idx]+=1.0*cpa_space.XMAXS.min()*np.random.standard_normal(size=dim_range)
                        
                        if any(cpa_space.zero_v_across_bdry):
                             
                            # in principle, the constraint is only about
                            # normals to the bdry. 
                            # however, if the variance is too large,
                            # the tangential comp can cause problems,
                            # in the sense it will drive everything
                            # to the corner. So use a hack: force zero.
                            # the projection later may still give values,
                            # but these are faily benign
                            # that are nonzero. Like I said, a hack.
                            for coo in range(cpa_space.dim_domain):                            
                                if V[idx,coo] in (0,cpa_space.XMAXS[coo]):
                                    velTess[idx]=0     
    #                                ipshell('hi')
    #                                1/0
                            
                        
                    
                    cpa_space.project_velTess(velTess,velTess)
#                    ipshell('hi')
#                    1/0
                    
                    cpa_space.velTess2Avees(velTess=velTess,Avees=Avees)
                    cpa_space.Avees2theta(theta=theta)

                    
                           
        if nullify_cells is not None:
            raise NotImplementedError 
            covs=msp.L_cpa_space_covs[level]
            cpa_cov = covs.cpa_cov
            pa_cov = covs.pa_cov
            w = np.ones(cpa_space.d_no_constraints)
            w.reshape(cpa_space.nC,-1)[nullify_cells]=0
            print w
            pa_cov2=np.diag(w).dot(pa_cov).dot(np.diag(w))
            cpa_cov2=cpa_space.B.T.dot(pa_cov2).dot(cpa_space.B)
            
            theta[:]=draw_from_mvnormal(mean=theta_current*0,cov=scale**2 * cpa_cov2 )