Ejemplo n.º 1
0
    def get_sample_means(
        self,
        sample_size: int,
        num_samples: int = 1,
        t: float = -1,
    ) -> np.ndarray:
        """
        Calculate the sample means.

        Each mean is from a sample of ``sample_size`` agents.

        The number of means is the same as ``num_samples``.

        Means are taken either from ``opinions`` argument or from ``self.result.y`` at time point ``t``.

        see https://en.wikipedia.org/wiki/Central_limit_theorem

        :param sample_size: Pick this many agents' opinions (i.e. a sample).
        :param num_samples: Number of sample to perform.
        :param t: Time at which to conduct the sampling (-1 for last time point).
        :return: Array of means (size equal to ``num_samples``).
        """

        t_idx = np.argmin(np.abs(t -
                                 self.result.t)) if isinstance(t, float) else t
        opinions = self.result.y[:, t_idx]
        return sample_means(opinions, sample_size, num_samples, rng=self.rn)
Ejemplo n.º 2
0
def _subsample_clt_sample(sn, y: np.ndarray, n: int, num_samples: int):
    """
    method to re-assign self._sample_means
    :math:`(X_1 - \\bar{X_n})`

    where :math:`X` is a random sample and :math:`\\bar{X}_{n}` is the sample mean for :math:`n` random samples.

    :param sn: SocialNetwork object to store sample means (and to use it's random number generator)
    :type sn: opdynamics.socialnetworks.SocialNetwork
    :param y: opinions
    :param n: sample size
    :param num_samples: number of samples (N)
    """
    sn._sample_means = sample_means(
        y, 1, num_samples=num_samples, rng=sn.rn) - sample_means(
            y, n, num_samples=num_samples, rng=sn.rn)
Ejemplo n.º 3
0
def _basic_clt_sample(sn, y: np.ndarray, n: int, num_samples: int):
    """
    method to re-assign self._sample_means
    :math:`\\sqrt {n}\\left(\\bar{X}_{n}-\\mu \\right) \\rightarrow \\mathcal{N}\\left(0,\\sigma ^{2}\\right)`

    where :math:`X` is a random sample and :math:`\\bar{X}_{n}` is the sample mean for :math:`n` random samples.

    :param sn: SocialNetwork object to store sample means (and to use it's random number generator)
    :type sn: opdynamics.socialnetworks.SocialNetwork
    :param y: opinions
    :param n: sample size
    :param num_samples: number of samples (N)
    """
    sn._sample_means = np.sqrt(n) * (
        sample_means(y, n, num_samples=num_samples, rng=sn.rn) - np.mean(y))