Esempio n. 1
0
 def propose(self):
     """
     Propose a move.
     """
     tau = (self.adaptive_scale_factor * self.proposal_sd) ** 2
     self.stochastic.value = \
             pm.rlognormal(np.log(self.stochastic.value), tau)
Esempio n. 2
0
 def propose(self):
     """
     Propose a move.
     """
     tau = (self.adaptive_scale_factor * self.proposal_sd)**2
     self.stochastic.value = \
             pm.rlognormal(np.log(self.stochastic.value), tau)
Esempio n. 3
0
def settlement_size_samples(mu, tau, cutoff, sum_mu, sum_tau, pop_accounted, N):
    """
    Returns N samples from the distribution of unsampled settlement sizes.
    Settlement sizes are drawn from a truncated lognormal distribution 
    conditional on their sum being equal to sum_val.
    
    At the SIR stage, 100 samples are proposed and 10 are retained.
    
    :Parameters:
    - mu : float
      The mean parameter.
    - tau : float
      The precision parameter.
    - cutoff : float
      The truncation value.
    - sum_mu : float
      The mean of the lognormal distribution of total population.
    - sum_tau : float
      The precision parameter of the lognormal distribution of total population.
    - pop_accounted : integer
      The total population accounted for by the GRUMP urban extents.
    - N : integer
      The number of samples to return.    
    """

    N_sum_vals = N/10
    
    # Compute moments and characteristic function for single population size,
    # to be used in all posterior evaluations.
    lnorms = np.exp(geto_truncnorm(mu, tau, log(cutoff), 10000))        
    sum_moments = np.mean(lnorms), np.var(lnorms)
    oFT = robust_CF(mu, tau, cutoff)
    
    # Generate values for total population in region not accounted for by
    # GRUMP urban extents.
    sum_vals = pm.rlognormal(sum_mu, sum_tau, size=N_sum_vals)-pop_accounted
    where_not_OK = np.where(sum_vals < 0)
    while len(where_not_OK[0]) > 0:
        sum_vals[where_not_OK] = pm.rlognormal(sum_mu, sum_tau, size=len(where_not_OK[0]))-pop_accounted
        where_not_OK = np.where(sum_vals < 0)        
    
    # Create 10 samples using SIR for each sum.
    samps = []
    for sum_val in sum_vals:
        
        tries = 0
        while tries < 10:
            try:
                # Find posterior of N given this sum, and draw a single sample from it.
                Nmesh, p = robust_posterior(mu, tau, cutoff, sum_val, prior_fun=None, sum_moments=sum_moments, oFT=oFT)
                N = Nmesh[int(pm.rcategorical(p))]

                # Draw 10 samples for the sizes of the constituent populations given their number and
                # the total population size.
                w,i,s = SIR_simplex_sample(mu, tau, cutoff, sum_val, N, N_proposals=1000, N_samps=10)
                break
            except RuntimeError:
                print 'Failed at N=%f, Nmesh=%s, p=%s. Trying again'%(N,Nmesh,p)
                tries += 1
                if tries==10:
                    import sys
                    a,b,c = sys.exc_info()
                    raise a,b,c
                
        samps.extend(list(s.T))

    # Return, you're done!
    return samps