Ejemplo n.º 1
0
 def test_dist(self):
     # Test that we can specify distributions both by name and as objects.
     svals1, ppcc1 = stats.ppcc_plot(self.x, -10, 10, dist='tukeylambda')
     svals2, ppcc2 = stats.ppcc_plot(self.x, -10, 10, dist=stats.tukeylambda)
     assert_allclose(svals1, svals2, rtol=1e-20)
     assert_allclose(ppcc1, ppcc2, rtol=1e-20)
     # Test that 'tukeylambda' is the default dist
     svals3, ppcc3 = stats.ppcc_plot(self.x, -10, 10)
     assert_allclose(svals1, svals3, rtol=1e-20)
     assert_allclose(ppcc1, ppcc3, rtol=1e-20)
Ejemplo n.º 2
0
    def test_plot_kwarg(self):
        # Check with the matplotlib.pyplot module
        fig = plt.figure()
        fig.add_subplot(111)
        stats.ppcc_plot(self.x, -20, 20, plot=plt)
        plt.close()

        # Check that a Matplotlib Axes object is accepted
        fig.add_subplot(111)
        ax = fig.add_subplot(111)
        stats.ppcc_plot(self.x, -20, 20, plot=ax)
        plt.close()
Ejemplo n.º 3
0
    def test_plot_kwarg(self):
        # Check with the matplotlib.pyplot module
        fig = plt.figure()
        fig.add_subplot(111)
        stats.ppcc_plot(self.x, -20, 20, plot=plt)
        plt.close()

        # Check that a Matplotlib Axes object is accepted
        fig.add_subplot(111)
        ax = fig.add_subplot(111)
        stats.ppcc_plot(self.x, -20, 20, plot=ax)
        plt.close()
Ejemplo n.º 4
0
 def test_empty(self):
     # For consistency with probplot return for one empty array,
     # ppcc contains all zeros and svals is the same as for normal array
     # input.
     svals, ppcc = stats.ppcc_plot([], 0, 1)
     assert_allclose(svals, np.linspace(0, 1, num=80))
     assert_allclose(ppcc, np.zeros(80, dtype=float))
Ejemplo n.º 5
0
 def test_empty(self):
     # For consistency with probplot return for one empty array,
     # ppcc contains all zeros and svals is the same as for normal array
     # input.
     svals, ppcc = stats.ppcc_plot([], 0, 1)
     assert_allclose(svals, np.linspace(0, 1, num=80))
     assert_allclose(ppcc, np.zeros(80, dtype=float))
Ejemplo n.º 6
0
 def test_basic(self):
     N = 5
     svals, ppcc = stats.ppcc_plot(self.x, -10, 10, N=N)
     ppcc_expected = [
         0.21139644, 0.21384059, 0.98766719, 0.97980182, 0.93519298
     ]
     assert_allclose(svals, np.linspace(-10, 10, num=N))
     assert_allclose(ppcc, ppcc_expected)
Ejemplo n.º 7
0
def ppcc_plot(series, lambda_min=-5, lambda_max=5, dist="tukeylambda",
              N=100, ax=None, save=False, show=True):

    if ax is None:
        fig, ax = plt.subplots()

    svals, ppcc = stats.ppcc_plot(series, lambda_min, lambda_max,
                                  dist=dist, plot=ax, N=N)

    show_and_save_plot(show=show, save=save, filename="ppcc.png")

    max_corr_value = ppcc.max()
    max_corr_lambda = svals[ppcc.argmax()]
    return svals, ppcc, max_corr_value, max_corr_lambda
Ejemplo n.º 8
0
 def test_basic(self):
     N = 5
     svals, ppcc = stats.ppcc_plot(self.x, -10, 10, N=N)
     ppcc_expected = [0.21139644, 0.21384059, 0.98766719, 0.97980182, 0.93519298]
     assert_allclose(svals, np.linspace(-10, 10, num=N))
     assert_allclose(ppcc, ppcc_expected)
Ejemplo n.º 9
0
# First we generate some random data from a Tukey-Lambda distribution,
# with shape parameter -0.7:

from scipy import stats
import matplotlib.pyplot as plt

np.random.seed(1234567)
x = stats.tukeylambda.rvs(-0.7, loc=2, scale=0.5, size=10000) + 1e4

# Now we explore this data with a PPCC plot as well as the related
# probability plot and Box-Cox normplot.  A red line is drawn where we
# expect the PPCC value to be maximal (at the shape parameter -0.7 used
# above):

fig = plt.figure(figsize=(12, 4))
ax1 = fig.add_subplot(131)
ax2 = fig.add_subplot(132)
ax3 = fig.add_subplot(133)
res = stats.probplot(x, plot=ax1)
res = stats.boxcox_normplot(x, -5, 5, plot=ax2)
res = stats.ppcc_plot(x, -5, 5, plot=ax3)
ax3.vlines(-0.7, 0, 1, colors='r', label='Expected shape value')
plt.show()
    def redraw(self):
        def bootstrap(data, num_samples, statistic, alpha):
            n = len(data)
            idx = random.randint(0, n, (num_samples, n))
            samples = data[idx]
            stat = np.sort(statistic(samples, 1))
            return (stat[int((alpha / 2.0) * num_samples)], stat[int(
                (1 - alpha / 2.0) * num_samples)], samples)

        data = DS.Raw.iloc[DS.Ir, DS.Ic]
        data = data.assign(Lr=DS.Lr[DS.Ir])
        data = data.assign(Cr=DS.Cr[DS.Ir])
        data = data[[self.YcomboBox.currentText(), 'Lr', 'Cr']]
        if data.shape[1] != 3:
            QtWidgets.QMessageBox.critical(self,'Error',"More columns have the same name",\
                                           QtWidgets.QMessageBox.Ok)
            return ()
        Nnan = data[self.YcomboBox.currentText()].isnull().all()
        data = data.dropna()
        Lr = data['Lr'].values
        Cr = data['Cr'].values
        data = data.drop('Lr', axis=1)
        data = data.drop('Cr', axis=1)
        Y = data.values.ravel()
        if Y.dtype == 'float' and Y.dtype == 'int':
            QtWidgets.QMessageBox.critical(self,'Error',"Some values are not numbers!",\
                                           QtWidgets.QMessageBox.Ok)
            return ()
        color = 'blue'
        fig = Figure()
        ax = fig.add_subplot(111)
        if self.CcheckBox.isChecked():
            color = DS.Cc[self.YcomboBox.currentIndex() - 1]
        if self.BoxradioButton.isChecked():
            medianprops = dict(marker='D',
                               markeredgecolor='black',
                               markerfacecolor=color)
            ax.boxplot(Y, vert=1, medianprops=medianprops)
            if self.YcheckBox.isChecked():
                if self.YlineEdit.text():
                    ax.set_ylabel(self.YlineEdit.text())
            if self.XcheckBox.isChecked():
                if self.XlineEdit.text():
                    ax.set_xlabel(self.YlineEdit.text())
            if Nnan:
                ax.annotate('NaN present',
                            xy=(0.05, 0.95),
                            xycoords='axes fraction')
        elif self.NormalityradioButton.isChecked():
            stats.probplot(Y, plot=ax)
            if self.XcheckBox.isChecked():
                if self.XlineEdit.text():
                    ax.set_xlabel('Normal N(0,1) Statistic Medians')
            if self.YcheckBox.isChecked():
                if self.YlineEdit.text():
                    ax.set_ylabel('Ordered Responce')
            if Nnan:
                ax.annotate('NaN present',
                            xy=(0.05, 0.95),
                            xycoords='axes fraction')
        elif self.PPCCradioButton.isChecked():
            stats.ppcc_plot(Y, -6, 6, plot=ax)
            if self.XcheckBox.isChecked():
                if self.XlineEdit.text():
                    ax.set_xlabel('Shape Values')
            if self.YcheckBox.isChecked():
                if self.YlineEdit.text():
                    ax.set_ylabel('Prob.Plot.Corr.Coef.')
            if Nnan:
                ax.annotate('NaN present',
                            xy=(0.05, 0.95),
                            xycoords='axes fraction')
        elif self.BoxCoxradioButton.isChecked():
            fig = Figure()
            if (Y <= 0).all():
                QtWidgets.QMessageBox.critical(
                    self, 'Error', "Data must be strictly positive!",
                    QtWidgets.QMessageBox.Ok)
                return ()
            bins = np.linspace(np.amin(Y), np.amax(Y), 21)
            ax1 = fig.add_subplot(2,
                                  2,
                                  1,
                                  title="Original " +
                                  self.YcomboBox.currentText())
            ax1.hist(Y,
                     bins=bins,
                     histtype='bar',
                     color=color,
                     alpha=0.5,
                     orientation="vertical",
                     label="x")
            if Nnan:
                ax1.annotate('NaN present',
                             xy=(0.05, 0.95),
                             xycoords='axes fraction')
            trans_y, lambda_ = stats.boxcox(Y)
            ax2 = fig.add_subplot(2,
                                  2,
                                  2,
                                  title='Transformed Data (lambda=' +
                                  str(round(lambda_, 2)) + ')')
            bins = np.linspace(np.amin(trans_y), np.amax(trans_y), 21)
            ax2.hist(trans_y,
                     bins=bins,
                     histtype='bar',
                     color=color,
                     alpha=0.5,
                     orientation="vertical",
                     label="x Transformed")
            ax3 = fig.add_subplot(2,
                                  2,
                                  3,
                                  title="Original " +
                                  self.YcomboBox.currentText())
            stats.probplot(Y, dist='norm', plot=ax3)
            ax4 = fig.add_subplot(2, 2, 4)
            stats.probplot(trans_y, dist='norm', plot=ax4)
            ax4.set_title('Transformed Data (lambda=' +
                          str(round(lambda_, 2)) + ')')
        elif self.logisticradioButton.isChecked():
            stats.probplot(Y, dist=stats.logistic, plot=ax)
            ax.set_xlabel('Quantiles')
            ax.set_ylabel("Ordered " + self.YcomboBox.currentText())
            ax.set_title('Logistic', fontsize=12)
            if Nnan:
                ax.annotate('NaN present',
                            xy=(0.05, 0.95),
                            xycoords='axes fraction')
        elif self.laplaceradioButton.isChecked():
            stats.probplot(Y, dist=stats.laplace, plot=ax)
            ax.set_xlabel('Quantiles')
            ax.set_ylabel('Ordered ' + self.YcomboBox.currentText())
            ax.set_title('Laplace', fontsize=12)
            if Nnan:
                ax.annotate('NaN present',
                            xy=(0.05, 0.95),
                            xycoords='axes fraction')
        elif self.logammaradioButton.isChecked():
            shape = float(self.logammadoubleSpinBox.value())
            stats.probplot(Y, dist=stats.loggamma, sparams=shape, plot=ax)
            ax.set_xlabel('Quantiles')
            ax.set_ylabel("Ordered " + self.YcomboBox.currentText())
            ax.set_title('Log Gamma with shape ' + str(shape), fontsize=12)
            if Nnan:
                ax.annotate('NaN present',
                            xy=(0.05, 0.95),
                            xycoords='axes fraction')
        elif self.lognormalradioButton.isChecked():
            shape = float(self.lognormdoubleSpinBox.value()) / 10.
            stats.probplot(Y, dist=stats.lognorm, sparams=(shape), plot=ax)
            ax.set_xlabel('Quantiles')
            ax.set_ylabel("Ordered " + self.YcomboBox.currentText())
            ax.set_title('Log norm with shape ' + str(shape), fontsize=12)
            if Nnan:
                ax.annotate('NaN present',
                            xy=(0.05, 0.95),
                            xycoords='axes fraction')
        elif self.BootmeanradioButton.isChecked():
            fig = Figure()
            ax1 = fig.add_subplot(1,
                                  2,
                                  1,
                                  title="Historgram of " +
                                  self.YcomboBox.currentText())
            low, high, samples = bootstrap(Y, self.spinBox.value(), np.mean,
                                           0.05)
            points = np.mean(samples, 1)
            ax1.hist(points, 50, histtype='step')
            if Nnan:
                ax1.annotate('NaN present',
                             xy=(0.05, 0.95),
                             xycoords='axes fraction')
            ax2 = fig.add_subplot(1, 2, 2, title='Bootstrap 95% CI for mean')
            ax2.scatter(0.1 * (random.random(len(points)) - 0.5), points)
            ax2.plot([0.19, 0.21], [low, low], 'r', linewidth=2)
            ax2.plot([0.19, 0.21], [high, high], 'r', linewidth=2)
            ax2.plot([0.2, 0.2], [low, high], 'r', linewidth=2)
            ax2.set_xlim([-0.2, 0.3])
        elif self.BootsdradioButton.isChecked():
            fig = Figure()
            ax1 = fig.add_subplot(1,
                                  2,
                                  1,
                                  title="Historgram of " +
                                  self.YcomboBox.currentText())
            low, high, samples = bootstrap(Y, self.spinBox.value(), np.std,
                                           0.05)
            points = np.std(samples, 1)
            ax1.hist(points, 50, histtype='step')
            if Nnan:
                ax1.annotate('NaN present',
                             xy=(0.05, 0.95),
                             xycoords='axes fraction')
            ax2 = fig.add_subplot(
                1, 2, 2, title='Bootstrap 95% CI for standard deviation')
            ax2.scatter(0.1 * (random.random(len(points)) - 0.5), points)
            ax2.plot([0.19, 0.21], [low, low], 'r', linewidth=2)
            ax2.plot([0.19, 0.21], [high, high], 'r', linewidth=2)
            ax2.plot([0.2, 0.2], [low, high], 'r', linewidth=2)
            ax2.set_xlim([-0.2, 0.3])
        elif self.histogramradioButton.isChecked():
            iqr = np.percentile(Y, [75, 25])
            iqr = iqr[0] - iqr[1]
            n = Y.shape[0]
            dy = abs(float(Y.max()) - float(Y.min()))
            nbins = np.floor(dy / (2 * iqr) * n**(1 / 3)) + 1
            nbins = 2 * nbins
            bins = np.linspace(float(Y.min()), float(Y.max()), nbins)
            ax.hist(Y,
                    bins=bins,
                    histtype='bar',
                    color=color,
                    alpha=0.5,
                    orientation='vertical',
                    label="X")
            if Nnan:
                ax.annotate('NaN present',
                            xy=(0.05, 0.95),
                            xycoords='axes fraction')
        elif self.trendradioButton.isChecked():
            nr = len(Y)
            ind = np.array(range(1, nr + 1))
            color_point = 'red'
            color_line = 'blue'
            if self.CcheckBox.isChecked():
                color_line = DS.Cc[self.YcomboBox.currentIndex() - 1]
                color_point = Cr
            ax.scatter(ind, Y, marker='o', color=color_point)
            if self.LcheckBox.isChecked():
                ax.plot(ind, Y, color=color_line)
            if (nr > 30):
                itick = np.linspace(0, nr - 1, 20).astype(int)
                ltick = Lr[itick]
            else:
                itick = ind
                ltick = Lr
            ax.set_xticks(itick)
            ax.set_xticklabels(ltick, rotation='vertical')
            ax.set_xlabel('Object')
            ax.set_ylabel(self.YcomboBox.currentText())
            if Nnan:
                ax.annotate('NaN present',
                            xy=(0.05, 0.95),
                            xycoords='axes fraction')
        if self.TcheckBox.isChecked():
            if self.TlineEdit.text():
                ax.set_title(self.TlineEdit.text())
        else:
            ax.set_title('')
        if self.XcheckBox.isChecked():
            if self.XlineEdit.text():
                ax.set_xlabel(self.XlineEdit.text())
        else:
            ax.set_xlabel('')
        if self.YcheckBox.isChecked():
            if self.YlineEdit.text():
                ax.set_ylabel(self.YlineEdit.text())
        else:
            ax.set_ylabel('')
        if self.XGcheckBox.isChecked():
            ax.xaxis.grid(True)
        else:
            ax.xaxis.grid(False)
        if self.YGcheckBox.isChecked():
            ax.yaxis.grid(True)
        else:
            ax.yaxis.grid(False)
        if not self.XMcheckBox.isChecked():
            ax.tick_params(axis='x',
                           which='both',
                           bottom='off',
                           top='off',
                           labelbottom='off')
        if not self.YMcheckBox.isChecked():
            ax.tick_params(axis='y',
                           which='both',
                           left='off',
                           right='off',
                           labelleft='off')
        self.rmmpl()
        self.addmpl(fig)