Пример #1
0
def plot_hist(ax, sextable, mcolour='blue', fcolour='red', ucolour='none'):
    cdict = {0: mcolour, 1: fcolour, None: ucolour}

    Mx, Lx, Ma, La, Rl, Rx = [
        np.array(map(operator.itemgetter(i), sextable), dtype=float)
        for i in range(1, 7)
    ]
    hard_sex, soft_sex, m_dist, f_dist, m_mu, f_mu, m_bound, f_bound, logit_pm = assign_sex(
        sextable, sextable[0][5])
    males = [Rx[i] for i, j in enumerate(hard_sex) if j == 0]
    females = [Rx[i] for i, j in enumerate(hard_sex) if j == 1]

    mMx = [Mx[i] for i, j in enumerate(hard_sex) if j == 0]
    mMa = [Ma[i] for i, j in enumerate(hard_sex) if j == 0]
    fMx = [Mx[i] for i, j in enumerate(hard_sex) if j == 1]
    fMa = [Ma[i] for i, j in enumerate(hard_sex) if j == 1]

    hardcolours = [cdict[i] for i in hard_sex]
    softcolours = [cdict[i] for i in soft_sex]
    msoftcolours = [cdict[i] for i in soft_sex if i != 1]
    fsoftcolours = [cdict[i] for i in soft_sex if i != 0]

    _, bins, _ = ax.hist(Rx, bins=len(Rx) / 4, fill=False)
    marea = len(males) * (bins[1] - bins[0])
    farea = len(females) * (bins[1] - bins[0])

    x = np.linspace(0, 2 * Rl[0], 1000)
    ax.plot(x, marea * m_dist.pdf(x), '-', color=mcolour)
    #ax.scatter(males, marea*m_dist.pdf(males), facecolor=msoftcolours, edgecolor=mcolour)
    ax.plot(x, farea * f_dist.pdf(x), '-', color=fcolour)
    #ax.scatter(females, farea*f_dist.pdf(females), facecolor=fsoftcolours, edgecolor=fcolour)

    ylim = ax.get_ylim()

    ax.vlines(m_mu, 0, ylim[1], color=mcolour, linestyle=':')
    ax.vlines(f_mu, 0, ylim[1], color=fcolour, linestyle=':')
    ax.vlines(m_bound, 0, ylim[1], color=mcolour, linestyle='--')
    ax.vlines(f_bound, 0, ylim[1], color=fcolour, linestyle='--')

    ax.set_ylim((0, ylim[1]))

    m_patch = mpatches.Patch(color=mcolour)
    f_patch = mpatches.Patch(color=fcolour)
    ax.legend([m_patch, f_patch], [
        'male ($n={}({}), \\bar{{x}}={:.3f}, var={:.2e}, Beta.var={:.2e}$)'.
        format(len(males), len([m for m in males if m < m_bound]), m_mu,
               np.var(males),
               beta.var(np.median(mMa),
                        np.median(mMa) + np.median(mMx))),
        'female ($n={}({}), \\bar{{x}}={:.3f}, var={:.2e}, Beta.var={:.2e}$)'.
        format(len(females), len([f for f in females if f > f_bound]), f_mu,
               np.var(females),
               beta.var(np.median(fMa),
                        np.median(fMa) + np.median(fMx)))
    ])
    ax.set_xlabel('Proportion of mapped reads: X/(X+Autosome)')
    ax.set_ylabel('Frequency (counts)')
    ax.set_title('K-means clustering for sex assignment')
Пример #2
0
        def r(theta_hat):  # expected reward with sampled model state "theta"
            if reward_type == 'least_accurate':
                return -theta_hat  # E(y|theta_hat)

            elif reward_type == 'most_accurate':
                return theta_hat

            elif reward_type == 'most_biased':
                raise ValueError

            elif reward_type == 'least_biased':
                raise ValueError

            elif reward_type == 'groupwise_accuracy':
                var_plus_1 = beta.var(self._params[:, 0] + 1, self._params[:,
                                                                           1])
                var_plus_0 = beta.var(self._params[:, 0],
                                      self._params[:, 1] + 1)
                E_var = var_plus_1 * theta_hat + var_plus_0 * (1 - theta_hat)
                return (self.variance - E_var) * self._weight

            elif reward_type == 'difference':
                if group0 is None or group1 is None:
                    raise ValueError

                def rope(alpha0, alpha1, beta0, beta1):
                    num_samples = 10000
                    theta_0 = np.random.beta(alpha0, beta0, size=(num_samples))
                    theta_1 = np.random.beta(alpha1, beta1, size=(num_samples))
                    delta = theta_0 - theta_1
                    return max((delta < -0.05).mean(),
                               (np.abs(delta) <= 0.05).mean(),
                               (delta > 0.05).mean())

                alpha0, beta0 = self._params[group0]
                alpha1, beta1 = self._params[group1]
                rope_plus_1 = np.array([
                    rope(alpha0 + 1, alpha1, beta0, beta1),
                    rope(alpha0, alpha1 + 1, beta0, beta1)
                ])
                rope_plus_0 = np.array([
                    rope(alpha0, alpha1, beta0 + 1, beta1),
                    rope(alpha0, alpha1, beta0, beta1 + 1)
                ])
                theta_hat = theta_hat[[group0, group1]]
                E_rope = rope_plus_1 * theta_hat + rope_plus_0 * (1 -
                                                                  theta_hat)
                r = np.ones(self._k) * (
                    -1)  # set reward of other groups to be -1
                r[[group0, group1]] = E_rope
                return r
Пример #3
0
 def variance(self) -> float:
     """
     Eval variance of posterior of ECE.
     :return:
     """
     variance_bin = beta.var(self._alpha, self._beta)
     if self._weight is not None:  # pool weights
         weight = self._weight
     else:  # online weights
         tmp = np.sum(self._counts, axis=1)
         weight = tmp / sum(tmp)
     return np.dot(weight * weight, variance_bin)
def shots_to_obs_moments(bitarray: np.ndarray, qubits: List[int], observable: PauliTerm,
                         use_beta_dist_unbiased_prior: bool = False) -> Tuple[float, float]:
    """
    Calculate the mean and variance of the given observable based on the bitarray of results.

    :param bitarray: results from running `qc.run`, a 2D num_shots by num_qubits array.
    :param qubits: list of qubits in order corresponding to the bitarray results.
    :param observable: the observable whose moments are calculated from the shot data
    :param use_beta_dist_unbiased_prior: if true then the mean and variance are estimated from a
        beta distribution that incorporates an unbiased Bayes prior. This precludes var = 0.
    :return: tuple specifying (mean, variance)
    """
    coeff = complex(observable.coefficient)
    if not np.isclose(coeff.imag, 0):
        raise ValueError(f"The coefficient of an observable should not be complex.")
    coeff = coeff.real

    obs_qubits = [q for q, _ in observable]
    # Identify classical register indices to select
    idxs = [idx for idx, q in enumerate(qubits) if q in obs_qubits]

    if len(idxs) == 0:  # identity term
        return coeff, 0

    assert bitarray.shape[1] == len(qubits), 'qubits should label each column of the bitarray'

    # Pick columns corresponding to qubits with a non-identity out_operation
    obs_strings = bitarray[:, idxs]
    # Transform bits to eigenvalues; ie (+1, -1)
    my_obs_strings = 1 - 2 * obs_strings
    # Multiply row-wise to get operator values.
    obs_vals = np.prod(my_obs_strings, axis=1)

    if use_beta_dist_unbiased_prior:
        # For binary classified data with N counts of + and M counts of -, these can be estimated
        # using the mean and variance of the beta distribution beta(N+1, M+1) where the +1 is used
        # to incorporate an unbiased Bayes prior.
        plus_array = obs_vals == 1
        n_minus, n_plus = np.bincount(plus_array, minlength=2)
        bernoulli_mean = beta.mean(n_plus + 1, n_minus + 1)
        bernoulli_var = beta.var(n_plus + 1, n_minus + 1)
        obs_mean, obs_var = transform_bit_moments_to_pauli(bernoulli_mean, bernoulli_var)
        obs_mean *= coeff
        obs_var *= coeff**2
    else:
        obs_vals = coeff * obs_vals
        obs_mean = np.mean(obs_vals).item()
        obs_var = np.var(obs_vals).item() / len(bitarray)

    return obs_mean, obs_var
def survival_statistics(bitstrings):
    """
    Calculate the mean and variance of the estimated probability of the ground state given shot
    data on one or more bits.

    For binary classified data with N counts of 1 and M counts of 0, these
    can be estimated using the mean and variance of the beta distribution beta(N+1, M+1) where the
    +1 is used to incorporate an unbiased Bayes prior.

    :param ndarray bitstrings: A 2D numpy array of repetitions x bit-arrays.
    :return: (survival mean, sqrt(survival variance))
    """
    survived = np.sum(bitstrings, axis=1) == 0

    # count obmurrences of 000...0 and anything besides 000...0
    n_died, n_survived = bincount(survived, minlength=2)

    # mean and variance given by beta distribution with a uniform prior
    survival_mean = beta.mean(n_survived + 1, n_died + 1)
    survival_var = beta.var(n_survived + 1, n_died + 1)
    return survival_mean, np.sqrt(survival_var)
Пример #6
0
 def get_var(self):
     return beta.var(self.a, self.b)
 def variance(self):
     return beta.var(self.alpha, self.beta)
Пример #8
0
 def variance(self) -> np.ndarray:
     """
     Variance of posterior classwise accuracy.
     :return: An (k, ) array of variance of posteriors of classwise accuracies.
     """
     return beta.var(self._params[:, 0], self._params[:, 1])
Пример #9
0
 def var(self, n, p):
     var = beta.var(self, n, p)
     return var
Пример #10
0
from scipy.stats import beta

a = 7.2
b = 2.3
m, v = beta.stats(a, b, moments="mv")
mean = beta.mean(a=a, b=b)
var = beta.var(a=a, b=b)
print("The mean is " + str(m))
print("The variance is " + str(v))

prob = 1 - beta.cdf(a=a, b=b, x=.90)
print("The probability of having a variance over 90% is " + str(prob))
Пример #11
0
 def variance(self) -> np.ndarray:
     return beta.var(self._params[:, 0], self._params[:, 1])
Пример #12
0
 def variance(self):
     return beta.var(self._params[:, 0], self._params[:, 1])