Beispiel #1
0
def plot_wikipedia_pdfs_vs_normal():
    """
    https://en.wikipedia.org/wiki/Student%27s_t-distribution#/media/
    File:T_distribution_1df_enhanced.svg
    https://en.wikipedia.org/wiki/Student%27s_t-distribution#/media/
    File:T_distribution_2df_enhanced.svg
    https://en.wikipedia.org/wiki/Student%27s_t-distribution#/media/
    File:T_distribution_3df_enhanced.svg
    https://en.wikipedia.org/wiki/Student%27s_t-distribution#/media/
    File:T_distribution_5df_enhanced.svg
    https://en.wikipedia.org/wiki/Student%27s_t-distribution#/media/
    File:T_distribution_10df_enhanced.svg
    https://en.wikipedia.org/wiki/Student%27s_t-distribution#/media/
    File:T_distribution_30df_enhanced.svg
    """
    _, axes = plt.subplots(nrows=2, ncols=3, figsize=(16, 9))
    normal = Normal(mu=0, sigma=1)
    max_degrees = (1, 2, 3, 5, 10, 30)
    for d in range(len(max_degrees)):
        ax = axes.flat[d]
        normal.plot(x=x_wikipedia_2, color='blue', ax=ax)
        for d2 in range(d):
            StudentsT(nu=max_degrees[d2]).plot(x=x_wikipedia_2, color='green',
                                               alpha=0.5 ** (d - d2), ax=ax)
        StudentsT(nu=max_degrees[d]).plot(x=x_wikipedia_2, color='red', ax=ax)
        ax.legend(loc='upper right')
    plt.show()
Beispiel #2
0
    def test_fit(self):

        for mu, sigma_sq in zip((0, 0, 0, -2), (0.2, 1.0, 5.0, 0.5)):
            normal_orig = Normal(mu=mu, sigma_sq=sigma_sq)
            normal_fit = Normal.fit(normal_orig.rvs(100_000))
            self.assertAlmostEqual(normal_orig.mu, normal_fit.mu, 1)
            self.assertAlmostEqual(normal_orig.sigma_sq, normal_fit.sigma_sq,
                                   1)
Beispiel #3
0
    def from_ratio_frame(data: DataFrame, name: str = ''):
        """
        Create a new NormalSeries using the distributions of data in each
        column of a DataFrame.

        :param data: Data with ratio values.
        :param name: Name for the Series.
        """
        normals = {}
        for col in data.columns:
            normals[col] = Normal(mu=data[col].mean(), sigma=data[col].std())
        normals = Series(data=normals, name=name)
        return NormalSeries(normals)
Beispiel #4
0
 def probably_greater_than(self, other: 'Ordinal') -> float:
     """
     Find the approximate probability that self > other, assuming an
     underlying Normal data generating distribution..
     """
     m_self = self._data_vals.mean()
     s_self = self._data_vals.std()
     m_other = other._data_vals.mean()
     s_other = other._data_vals.std()
     m_diff = m_self - m_other
     s_diff = (s_self ** 2 + s_other ** 2) ** 0.5
     diff = Normal(mu=m_diff, sigma=s_diff)
     return diff > 0
Beispiel #5
0
def plot_normal_students_t_laplace():
    """
    Machine Learning: A Probabilistic Perspective. Figure 2.7
    """
    _, axes = plt.subplots(ncols=2, figsize=(16, 9))
    # define distributions
    normal = Normal(mu=0, sigma=1)
    students_t = StudentsT(nu=1)
    laplace = Laplace(mu=0, b=1 / sqrt(2))
    # plot pdfs
    ax = axes[0]
    normal.plot(x=x, ls=':', color='black', ax=ax)
    students_t.plot(x=x, ls='--', color=ML_APP_DARK_BLUE, ax=ax)
    laplace.plot(x=x, ls='-', color='red', ax=ax)
    ax.set_ylim(0, 0.8)
    ax.legend(loc='upper right')
    # plot log-pdfs
    ax = axes[1]
    normal.log_pdf().plot(x=x, ls=':', color='black', ax=ax)
    students_t.log_pdf().plot(x=x, ls='--', color=ML_APP_DARK_BLUE, ax=ax)
    laplace.log_pdf().plot(x=x, ls='-', color='red', ax=ax)
    ax.set_ylim(-9, 0)
    ax.legend(loc='upper right')
    plt.show()
Beispiel #6
0
    def setUp(self) -> None:

        self.n1 = Normal(0, 1)
        self.n2 = Normal(1, 2)