def tfenbu():
    fig, ax = plt.subplots(1, 1)
    df = 5
    mean, var, skew, kurt = t.stats(df, moments='mvsk')
    x = np.linspace(t.ppf(0.01, df), t.ppf(0.99, df), 100)
    # ax.plot(x, t.pdf(x, df),'r-', lw=5, alpha=0.6, label='t pdf')
    # plt.show()

    #自由度为10,t值小于-2的概率
    t.cdf(-2, df=10)
    #自由度为25,t分布右尾概率为0.025时的值
    t.ppf(0.975, df=25)
 def __init__(self, dofs):
     self.dofs = dofs
     if self.dofs is not None:
         if self.dofs > 0:
             self.bounds = np.array([-np.inf, np.inf])
             mean, var, skew, kurt = t.stats(df=self.dofs, moments='mvsk')
             self.parent = t(df=self.dofs)
             self.mean = mean
             self.variance = var
             self.skewness = skew
             self.kurtosis = kurt
             self.x_range_for_pdf = np.linspace(-5.0, 5.0,
                                                RECURRENCE_PDF_SAMPLES)
             self.parent = t(self.dofs)
Example #3
0
    def test_tstudent(self):
        from scipy.stats import t
        import matplotlib.pyplot as plt
        fig, ax = plt.subplots(1, 1)

        df = 2.74
        mean, var, skew, kurt = t.stats(df, moments='mvsk')

        x = np.linspace(t.ppf(0.01, df), t.ppf(0.99, df), 100)
        ax.plot(x, t.pdf(x, df), 'r-', lw=5, alpha=0.6, label='t pdf')

        rv = t(df)
        ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')

        vals = t.ppf([0.001, 0.5, 0.999], df)
        np.allclose([0.001, 0.5, 0.999], t.cdf(vals, df))

        r = t.rvs(df, size=1000)

        ax.hist(r, density=True, histtype='stepfilled', alpha=0.2)
        ax.legend(loc='best', frameon=False)
        self.assertEqual(str(ax), "AxesSubplot(0.125,0.11;0.775x0.77)")
print("Pvalue:", pvalue)
print("Tvalue", tvalue)

if pvalue <= alpha:
    # we reject null hypothesis
    print('Null hypothesis is rejected')
else:
    # we reject alternative hypothesis
    print('Null hypothesis cannot be rejected')

deg_freedom = df_s["usd_goal_real"].shape[0] - 1

print(np.var(df_s["usd_goal_real"]))
print(np.var(df_f["usd_goal_real"]))

t_dist = t.stats(deg_freedom)

print(deg_freedom)
print(t_dist)

critical_value = stats.t.ppf(q=1 - alpha, df=deg_freedom)

plt.style.use('ggplot')
x = np.linspace(-10, 10, 1000)
plt.plot(x, stats.t.pdf(x, deg_freedom))
plt.annotate('Critical Value = {0:.2f}'.format(critical_value),
             xy=(critical_value, 0.10),
             xytext=(critical_value, 0.12),
             arrowprops=dict(facecolor='black', shrink=0.5),
             verticalalignment='bottom')
plt.annotate('Current Value = {0:.2f}'.format(tvalue),
Example #5
0
from scipy.stats import t
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1)

# Calculate a few first moments:

df = 2.74
mean, var, skew, kurt = t.stats(df, moments='mvsk')

# Display the probability density function (``pdf``):

x = np.linspace(t.ppf(0.01, df), t.ppf(0.99, df), 100)
ax.plot(x, t.pdf(x, df), 'r-', lw=5, alpha=0.6, label='t pdf')

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

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

rv = t(df)
ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')

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

vals = t.ppf([0.001, 0.5, 0.999], df)
np.allclose([0.001, 0.5, 0.999], t.cdf(vals, df))
# True

# Generate random numbers:
Example #6
0
import pandas as pd
import seaborn as sns
from sklearn.model_selection import train_test_split
from scipy.stats import cauchy, norm, t
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE

# Student's t Distribution with Degree of Freedom = 1 and Normal/Gaussian Distrubution are used for Measuring Similarities
x = np.linspace(-10,10,500)
my_cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", ["red","green","blue","gold","purple","black"])

# Degree of Freedom
df = 1

n_mean, n_var, n_skew, n_kurt = norm.stats(moments='mvsk')
c_mean, c_var, c_skew, c_kurt = t.stats(df,moments='mvsk')

plt.plot(x,norm.pdf(x),'r',label='Normal Distribution')
plt.plot(x,cauchy.pdf(x),'b',label="Student's t Distribution")
plt.grid()
plt.legend()
plt.title('Normal Distribution vs Student t-Distribution')
plt.savefig('Images/SimilarityDistributions.png')
plt.show()


# Loading Dataset
Train = pd.read_csv('Dataset/train.csv')

X_Train = ((Train.loc[:, Train.columns != 'label']).to_numpy())
Y_Train = ((Train['label']).to_numpy())
import numpy as np
from scipy.stats import t, norm
import matplotlib.pyplot as plt
from scipy.special import gamma

ns = [100, 1000, 10000]
alphas = [0.01, 0.1, 1, 10]
v = 3
mu = 1.5
sigma = 1
mean_, var_ = t.stats(v, mu, sigma, moments='mv')


def f(x, v, mu, std):
    return ((gamma((v + 1) / 2) / (gamma(v / 2) * np.sqrt(v * np.pi) * std)) *
            (1 + (1 / v) * ((x - mu) / std)**2)**(-(v + 1) / 2))


def plot_t_dist():
    x = np.linspace(-5, 5, 1000)
    plt.title('MCMC Student-t distribution')
    plt.plot(x, f(x, 3, 1.5, 1), color='#1d55a5')
    plt.axvline(x=mean_,
                color='red',
                label=f'mean={mean_}, std={round(np.sqrt(var_), 4)}')
    plt.axvspan(float(mean_) - np.sqrt(var_),
                float(mean_) + np.sqrt(var_),
                facecolor='#97d8ed')
    plt.grid()
    plt.show()
Example #8
0
def testStudent(N, size):
    values = [lhw.rnstud(N) for i in range(size)]
    mean, var = t.stats(N, moments='mv')
    startWork(values, mean, var, "student")
    pass