示例#1
0
def trunc_logser_rvs(p, upper_bound, size):
    """Random variates of the upper truncated log-series

    Currently this function only supports random variate generation for p < 1.
    This will cover most circumstances, but it is possible to have p >= 1 for
    the truncated version of the distribution.

    """
    assert p < 1, 'trunc_logser_rvs currently only supports random number generation for p < 1'
    size = int(size)
    rvs = logser.rvs(p, size=size)
    for i in range(0, size):
        while(rvs[i] > upper_bound):
            rvs[i] = logser.rvs(p, size=1)
    return rvs
def _frank(M, N, alpha):
    if(N<2):
        raise ValueError('Dimensionality Argument [N] must be an integer >= 2')
    elif(N==2):        
        u1 = uniform.rvs(size=M)
        p = uniform.rvs(size=M)
        if abs(alpha) > math.log(sys.float_info.max):
            u2 = (u1 < 0).astype(int) + np.sign(alpha)*u1  # u1 or 1-u1
        elif abs(alpha) > math.sqrt(np.spacing(1)):
            u2 = -1*np.log((np.exp(-alpha*u1)*(1-p)/p + np.exp(-alpha))/(1 + np.exp(-alpha*u1)*(1-p)/p))/alpha
        else:
            u2 = p
        
        U = np.column_stack((u1,u2))
    else:
        # Algorithm 1 described in both the SAS Copula Procedure, as well as the
        # paper: "High Dimensional Archimedean Copula Generation Algorithm"
        if(alpha<=0):
            raise ValueError('For N>=3, alpha >0 in Frank Copula')
            
        U = np.empty((M,N))
        for ii in range(0,M):
            p = -1.0*np.expm1(-1*alpha)
            if(p==1):
                # boundary case protection
                p = 1 - np.spacing(1)
            v = logser.rvs(p, size=1)
            
            # sample N independent uniform random variables
            x_i = uniform.rvs(size=N)
            t = -1*np.log(x_i)/v
            U[ii,:] = -1.0*np.log1p( np.exp(-t)*np.expm1(-1.0*alpha))/alpha
            
    return U
示例#3
0
 def rvs_s(lmb=10, binom_n=23, binom_p=0.2, compound='binom'):
     N = np.random.poisson(lmb)
     rv = 0
     for _ in range(N):
         if compound == 'binom':
             vms = np.random.binomial(binom_n, binom_p)
         elif compound == 'binom_pl1':
             vms = np.random.binomial(binom_n, binom_p) + 1
         else:
             vms = logser.rvs(.8)
         rv += vms
     return rv
示例#4
0
def SimuFrank(n, m, theta):
    """
    # Frank copula
    Requires:
        n = number of variables to generate
        m = sample size
        theta = Frank copula parameter
    """

    v = [np.random.uniform(0, 1, m) for i in range(0, n)]
    p = 1 - np.exp(-theta)
    X = logser.rvs(p, loc=0, size=m, random_state=None)

    phi_t = lambda t: -np.log(1 - np.exp(-t) * (1 - np.exp(-theta))) / theta
    u = [phi_t(-np.log(v[i]) / X) for i in range(0, n)]
    return u
示例#5
0
def _frank(M, N, alpha):
    if (N < 2):
        raise ValueError('Dimensionality Argument [N] must be an integer >= 2')
    elif (N == 2):
        u1 = uniform.rvs(size=M)
        p = uniform.rvs(size=M)
        if abs(alpha) > math.log(sys.float_info.max):
            u2 = (u1 < 0).astype(int) + np.sign(alpha) * u1  # u1 or 1-u1
        elif abs(alpha) > math.sqrt(np.spacing(1)):
            u2 = -1 * np.log(
                (np.exp(-alpha * u1) * (1 - p) / p + np.exp(-alpha)) /
                (1 + np.exp(-alpha * u1) * (1 - p) / p)) / alpha
        else:
            u2 = p

        U = np.column_stack((u1, u2))
    else:
        # Algorithm 1 described in both the SAS Copula Procedure, as well as the
        # paper: "High Dimensional Archimedean Copula Generation Algorithm"
        if (alpha <= 0):
            raise ValueError('For N>=3, alpha >0 in Frank Copula')

        U = np.empty((M, N))
        #v_vec = np.empty(M)
        for ii in range(0, M):
            p = -1.0 * np.expm1(-1 * alpha)
            if (p == 1):
                # boundary case protection
                p = 1 - np.spacing(1)
            v = logser.rvs(p, size=1)
            #v_vec[ii] = v
            # sample N independent uniform random variables
            x_i = uniform.rvs(size=N)
            t = -1 * np.log(x_i) / v
            U[ii, :] = -1.0 * np.log1p(
                np.exp(-t) * np.expm1(-1.0 * alpha)) / alpha

        #sio.savemat('logser_v.mat', {'v':v_vec})

    return U
示例#6
0
 def _sample_scipy(self, size):
     p = float(self.p)
     from scipy.stats import logser
     return logser.rvs(p=p, size=size)
x = np.arange(logser.ppf(0.01, p), logser.ppf(0.99, p))
ax.plot(x, logser.pmf(x, p), 'bo', ms=8, label='logser pmf')
ax.vlines(x, 0, logser.pmf(x, p), colors='b', lw=5, alpha=0.5)

# Alternatively, the distribution object can be called (as a function)
# to fix the shape and location. This returns a "frozen" RV object holding
# the given parameters fixed.

# Freeze the distribution and display the frozen ``pmf``:

rv = logser(p)
ax.vlines(x,
          0,
          rv.pmf(x),
          colors='k',
          linestyles='-',
          lw=1,
          label='frozen pmf')
ax.legend(loc='best', frameon=False)
plt.show()

# Check accuracy of ``cdf`` and ``ppf``:

prob = logser.cdf(x, p)
np.allclose(x, logser.ppf(prob, p))
# True

# Generate random numbers:

r = logser.rvs(p, size=1000)
def rvs_comp_poisson(t, theta=5, m=100):
    p = t / (theta + t)
    lamb = -m * np.log(1 - p)
    log_rvs = lambda: logser.rvs(p)
    return CompoundPoisson.rvs_s_1(log_rvs, lamb)