Example #1
0
def plot_truncnorm_cdf():

    import matplotlib.pyplot as plt
    H = 100.  # mm
    B = 300.  # mm
    fig, axs = plt.subplots(1,
                            2,
                            dpi=90,
                            facecolor='w',
                            edgecolor='k',
                            figsize=(B / 25.4, H / 25.4))

    x = np.linspace(0, 5, 200)

    t = phuzzy.TruncNorm(alpha0=[1, 5], number_of_alpha_levels=25)
    p = t.pdf(x)
    P = t.cdf(x)
    axs[0].plot(x, p, label="pdf ana", lw=1)
    axs[1].plot(x, P, label="cdf ana", lw=1)

    f = phuzzy.FuzzyNumber(alpha0=[1, 2],
                           alpha1=[1.1, 1.4],
                           number_of_alpha_levels=5)
    f.df = t.df.copy()
    print(f.df)
    pf = f.pdf(x)
    Pf = f.cdf(x)

    mix_mpl(f)
    f.plot(show=False, ppf=[0, 0.01, .5, .99, 1])
    axs[0].plot(x, pf, label="pdf num", lw=3, c="r", alpha=.5)
    axs[1].plot(x, Pf, label="cdf num", lw=3, c="r", alpha=.5)
    axs[0].legend(loc="best")
    axs[1].legend(loc="best")
    plt.show()
def test_fuzzy():
    n = phuzzy.FuzzyNumber()
    print(n)
    print(n.__class__.__name__)
    assert hasattr(n, "name")
    assert hasattr(n, "df")
    assert hasattr(n, "number_of_alpha_levels")

    t = phuzzy.FuzzyNumber.from_data(data=[1, 3, 5], number_of_alpha_levels=3)
    print(t.df)
    assert len(t.df) == 3
    print(t.__class__.__name__)
    s = t.to_str()
    print(s)
    pdf = t.pdf([2])
    print(pdf)
    cdf = t.cdf([2])
    print(cdf)

    n = phuzzy.FuzzyNumber()
    with pytest.raises(NotImplementedError) as exp:
        s = n.to_str()
        print(s)

    with pytest.raises(NotImplementedError) as exp:
        s = phuzzy.FuzzyNumber.from_str("!")
        print(s)

    t = phuzzy.Triangle(alpha0=[1, 3], alpha1=[2], number_of_alpha_levels=15)
    p = phuzzy.Trapezoid(alpha0=[1, 4],
                         alpha1=[2, 3],
                         number_of_alpha_levels=5)
    n = t + p

    pdf = n.pdf([2])
    print(pdf)
    cdf = n.cdf([2])
    print(cdf)
def test_import_export():
    y = phuzzy.Trapezoid(alpha0=[1, 4],
                         alpha1=[2, 3],
                         number_of_alpha_levels=7)
    e = y.export_csv()
    print(e)

    if is_py2:
        e = e.decode()
    fh = StringIO(e)
    fh.seek(0)

    z = phuzzy.FuzzyNumber(alpha0=[1, 4],
                           alpha1=[2, 3],
                           number_of_alpha_levels=2)
    z.import_csv(fh)
    assert len(z.df) == 7
def test_discretize():
    x = phuzzy.FuzzyNumber(alpha0=[1, 4], number_of_alpha_levels=5)
    with pytest.raises(NotImplementedError) as exp:
        x.discretize(alpha0=[1, 4], alpha1=[2, 3], alpha_levels=10)