Beispiel #1
0
def kde_demo1():
    """KDEDEMO1 Demonstrate the smoothing parameter impact on KDE.

    KDEDEMO1 shows the true density (dotted) compared to KDE based on 7
    observations (solid) and their individual kernels (dashed) for 3
    different values of the smoothing parameter, hs.

    Examples
    --------
    >>> kde_demo1()
    """
    x = np.linspace(-4, 4, 101)
    x0 = x / 2.0
    data = np.random.normal(loc=0, scale=1.0, size=7)
    kernel = Kernel('gauss')
    hs = kernel.hns(data)
    h_vec = [hs / 2, hs, 2 * hs]

    for ix, h in enumerate(h_vec):
        plt.figure(ix)
        kde = KDE(data, hs=h, kernel=kernel)
        f2 = kde(x, output='plot', title='h_s = {0:2.2f}'.format(float(h)),
                 ylab='Density')
        f2.plot('k-')

        plt.plot(x, st.norm.pdf(x, 0, 1), 'k:')
        n = len(data)
        plt.plot(data, np.zeros(data.shape), 'bx')
        y = kernel(x0) / (n * h * kernel.norm_factor(d=1, n=n))
        for i in range(n):
            plt.plot(data[i] + x0 * h, y, 'b--')
            plt.plot([data[i], data[i]], [0, np.max(y)], 'b')

        plt.axis([min(x), max(x), 0, 0.5])
Beispiel #2
0
    def plotresq(self, symb1="r-", symb2="b."):
        """PLOTRESQ displays a residual quantile plot.

        The purpose of the plot is to graphically assess whether
        the data could come from the fitted distribution. If so the
        plot will be linear. Other distribution types will introduce
        curvature in the plot.
        """
        n = len(self.data)
        eprob = (arange(1, n + 1) - 0.5) / n
        y = self.ppf(eprob)
        y1 = self.data[[0, -1]]
        plotbackend.plot(self.data, y, symb2, y1, y1, symb1)
        plotbackend.xlabel("Empirical")
        plotbackend.ylabel("Model (%s)" % self.dist.name)
        plotbackend.title("Residual Quantile Plot")
        plotbackend.axis("tight")
        plotbackend.axis("equal")
Beispiel #3
0
    def plotresq(self, symb1='r-', symb2='b.'):
        '''PLOTRESQ displays a residual quantile plot.

        The purpose of the plot is to graphically assess whether
        the data could come from the fitted distribution. If so the
        plot will be linear. Other distribution types will introduce
        curvature in the plot.
        '''
        n = len(self.data)
        eprob = (arange(1, n + 1) - 0.5) / n
        y = self.ppf(eprob)
        y1 = self.data[[0, -1]]
        plotbackend.plot(self.data, y, symb2, y1, y1, symb1)
        plotbackend.xlabel('Empirical')
        plotbackend.ylabel('Model (%s)' % self.dist.name)
        plotbackend.title('Residual Quantile Plot')
        plotbackend.axis('tight')
        plotbackend.axis('equal')
Beispiel #4
0
    def plotepdf(self, symb1="r-", symb2="b-"):
        """Plot Empirical and fitted Probability Density Function

        The purpose of the plot is to graphically assess whether
        the data could come from the fitted distribution.
        If so the histogram should resemble the model density.
        Other distribution types will introduce deviations in the plot.
        """
        x, pdf = self._get_empirical_pdf()
        ymax = pdf.max()
        # plotbackend.hist(self.data,normed=True,fill=False)
        plotbackend.plot(self.data, self.pdf(self.data), symb1, x, pdf, symb2)
        ax = list(plotbackend.axis())
        ax[3] = min(ymax * 1.3, ax[3])
        plotbackend.axis(ax)
        plotbackend.xlabel("x")
        plotbackend.ylabel("f(x) (%s)" % self.dist.name)
        plotbackend.title("Density plot")
Beispiel #5
0
    def plotresprb(self, symb1="r-", symb2="b."):
        """ PLOTRESPRB displays a residual probability plot.

        The purpose of the plot is to graphically assess whether
        the data could come from the fitted distribution. If so the
        plot will be linear. Other distribution types will introduce curvature
        in the plot.
        """
        n = len(self.data)
        # ecdf = (0.5:n-0.5)/n;
        ecdf = arange(1, n + 1) / (n + 1)
        mcdf = self.cdf(self.data)
        p1 = [0, 1]
        plotbackend.plot(ecdf, mcdf, symb2, p1, p1, symb1)
        plotbackend.xlabel("Empirical")
        plotbackend.ylabel("Model (%s)" % self.dist.name)
        plotbackend.title("Residual Probability Plot")
        plotbackend.axis("equal")
        plotbackend.axis([0, 1, 0, 1])
Beispiel #6
0
    def plotepdf(self, symb1='r-', symb2='b-'):
        '''Plot Empirical and fitted Probability Density Function

        The purpose of the plot is to graphically assess whether
        the data could come from the fitted distribution.
        If so the histogram should resemble the model density.
        Other distribution types will introduce deviations in the plot.
        '''
        x, pdf = self._get_empirical_pdf()
        ymax = pdf.max()
        # plotbackend.hist(self.data,normed=True,fill=False)
        plotbackend.plot(self.data, self.pdf(self.data), symb1,
                         x, pdf, symb2)
        ax = list(plotbackend.axis())
        ax[3] = min(ymax * 1.3, ax[3])
        plotbackend.axis(ax)
        plotbackend.xlabel('x')
        plotbackend.ylabel('f(x) (%s)' % self.dist.name)
        plotbackend.title('Density plot')
Beispiel #7
0
    def plotresprb(self, symb1='r-', symb2='b.'):
        ''' PLOTRESPRB displays a residual probability plot.

        The purpose of the plot is to graphically assess whether
        the data could come from the fitted distribution. If so the
        plot will be linear. Other distribution types will introduce curvature
        in the plot.
        '''
        n = len(self.data)
        # ecdf = (0.5:n-0.5)/n;
        ecdf = arange(1, n + 1) / (n + 1)
        mcdf = self.cdf(self.data)
        p1 = [0, 1]
        plotbackend.plot(ecdf, mcdf, symb2,
                         p1, p1, symb1)
        plotbackend.xlabel('Empirical')
        plotbackend.ylabel('Model (%s)' % self.dist.name)
        plotbackend.title('Residual Probability Plot')
        plotbackend.axis('equal')
        plotbackend.axis([0, 1, 0, 1])
Beispiel #8
0
#! Histogram of crestperiod compared to the kernel density estimate
#!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import wafo.kdetools as wk
plt.clf()
print(Tc.mean())
print(Tc.max())

t = np.linspace(0.01,8,200);
ftc = wk.TKDE(Tc, L2=0, inc=128)

plt.plot(t,ftc.eval_grid(t), t, ftc.eval_grid_fast(t),'-.')
wm.plot_histgrm(Tc, normed=True)
plt.title('Kernel Density Estimates')
plt.xlabel('Tc [s]')
plt.axis([0, 8, 0, 0.5])
plt.show()

#! Extreme waves - model check: the highest and steepest wave
#!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
plt.clf()
S, H = ts.wave_height_steepness(kind=0)
indS = S.argmax()
indH = H.argmax()
ts.plot_sp_wave([indH, indS],'k.')
plt.show()

#! Does the highest wave contradict a transformed Gaussian model?
#!----------------------------------------------------------------

# TODO: Fix this
Beispiel #9
0
print('fm = %g, alpha = %g, ' % (fm, alfa))

#! Visually examine data
#!------------------------
#! We finish this section with some remarks about the quality
#! of the measured data. Especially sea surface measurements can be
#! of poor quality. We shall now check the  quality of the dataset {\tt xx}.
#! It is always good practice to visually examine the data
#! before the analysis to get an impression of the quality,
#! non-linearities and narrow-bandedness of the data.
#! First we shall plot the data and zoom in on a specific region.
#! A part of sea data is visualized with the following commands
plt.clf()
ts.plot_wave('k-', tp, '*', nfig=1, nsub=1)

plt.axis([0, 2, -2, 2])
plt.show()

#! Finding possible spurious points
#!------------------------------------
#! However, if the amount of data is too large for visual examinations one
#! could use the following criteria to find possible spurious points. One
#! must be careful using the criteria for extremevalue analysis, because
#! it might remove extreme waves that are OK and not spurious.

import wafo.misc as wm
dt = ts.sampling_period()
# dt = np.diff(xx[:2,0])
dcrit = 5 * dt
ddcrit = 9.81 / 2 * dt * dt
zcrit = 0
Beispiel #10
0
#! Histogram of crestperiod compared to the kernel density estimate
#!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import wafo.kdetools as wk
plt.clf()
print(Tc.mean())
print(Tc.max())

t = np.linspace(0.01, 8, 200)
ftc = wk.TKDE(Tc, L2=0, inc=128)

plt.plot(t, ftc.eval_grid(t), t, ftc.eval_grid_fast(t), '-.')
wm.plot_histgrm(Tc, normed=True)
plt.title('Kernel Density Estimates')
plt.xlabel('Tc [s]')
plt.axis([0, 8, 0, 0.5])
plt.show()

#! Extreme waves - model check: the highest and steepest wave
#!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
plt.clf()
S, H = ts.wave_height_steepness(kind=0)
indS = S.argmax()
indH = H.argmax()
ts.plot_sp_wave([indH, indS], 'k.')
plt.show()

#! Does the highest wave contradict a transformed Gaussian model?
#!----------------------------------------------------------------

# TODO: Fix this
Beispiel #11
0
plt.show()


#! Estimation of spectrum
#!~~~~~~~~~~~~~~~~~~~~~~~
#! A common situation is that one wants to estimate the spectrum for wave
#! measurements. The following code simulate 20 minutes signal sampled at 4Hz
#! and compare the spectral estimate with the original Torsethaugen spectum.
plt.clf()
Fs = 4
xs = S1.sim(ns=np.fix(20 * 60 * Fs), dt=1. / Fs)
ts = wo.mat2timeseries(xs)
Sest = ts.tospecdata(L=400)
S1.plot()
Sest.plot('--')
plt.axis([0, 3, 0, 5])
plt.show()

#! Section 1.4.2 Probability distributions of wave characteristics.
#!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#! Probability distribution of wave trough period:
#! WAFO gives the possibility of computing the exact probability
#! distributions for a number of characteristics given a spectral density.
#! In the following example we study the trough period extracted from the
#! time series and compared with the theoretical density computed with exact
#! spectrum, S1, and the estimated spectrum, Sest.
plt.clf()
import wafo.misc as wm
dtyex = S1.to_t_pdf(pdef='Tt', paramt=(0, 10, 51), nit=3)
dtyest = Sest.to_t_pdf(pdef='Tt', paramt=(0, 10, 51), nit=3)
Beispiel #12
0
plt.show()


#! Estimation of spectrum
#!~~~~~~~~~~~~~~~~~~~~~~~
#! A common situation is that one wants to estimate the spectrum for wave
#! measurements. The following code simulate 20 minutes signal sampled at 4Hz
#! and compare the spectral estimate with the original Torsethaugen spectum.
plt.clf()
Fs = 4
xs = S1.sim(ns=np.fix(20 * 60 * Fs), dt=1. / Fs)
ts = wo.mat2timeseries(xs)
Sest = ts.tospecdata(L=400)
S1.plot()
Sest.plot('--')
plt.axis([0, 3, 0, 5])
plt.show()

#! Section 1.4.2 Probability distributions of wave characteristics.
#!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#! Probability distribution of wave trough period:
#! WAFO gives the possibility of computing the exact probability
#! distributions for a number of characteristics given a spectral density.
#! In the following example we study the trough period extracted from the
#! time series and compared with the theoretical density computed with exact
#! spectrum, S1, and the estimated spectrum, Sest.
plt.clf()
import wafo.misc as wm
dtyex = S1.to_t_pdf(pdef='Tt', paramt=(0, 10, 51), nit=3)
dtyest = Sest.to_t_pdf(pdef='Tt', paramt=(0, 10, 51), nit=3)
Beispiel #13
0
print('fm = %g, alpha = %g, ' % (fm, alfa))

#! Visually examine data
#!------------------------
#! We finish this section with some remarks about the quality
#! of the measured data. Especially sea surface measurements can be
#! of poor quality. We shall now check the  quality of the dataset {\tt xx}.
#! It is always good practice to visually examine the data
#! before the analysis to get an impression of the quality,
#! non-linearities and narrow-bandedness of the data.
#! First we shall plot the data and zoom in on a specific region.
#! A part of sea data is visualized with the following commands
plt.clf()
ts.plot_wave('k-', tp, '*', nfig=1, nsub=1)

plt.axis([0, 2, -2, 2])
plt.show()

#! Finding possible spurious points
#!------------------------------------
#! However, if the amount of data is too large for visual examinations one
#! could use the following criteria to find possible spurious points. One
#! must be careful using the criteria for extremevalue analysis, because
#! it might remove extreme waves that are OK and not spurious.

import wafo.misc as wm
dt = ts.sampling_period()
# dt = np.diff(xx[:2,0])
dcrit = 5 * dt
ddcrit = 9.81 / 2 * dt * dt
zcrit = 0