Beispiel #1
0
def evaluate_models_on_testing(x, y, models):
    """
    For each regression model, compute the RMSE for this model and plot the
    test data along with the model’s estimation.

    For the plots, you should plot data points (x,y) as blue dots and your best
    fit curve (aka model) as a red solid line. You should also label the axes
    of this figure appropriately and have a title reporting the following
    information:
        degree of your regression model,
        RMSE of your model evaluated on the given data points.

    Args:
        x: an 1-d pylab array with length N, representing the x-coordinates of
            the N sample points
        y: an 1-d pylab array with length N, representing the y-coordinates of
            the N sample points
        models: a list containing the regression models you want to apply to
            your data. Each model is a pylab array storing the coefficients of
            a polynomial.

    Returns:
        None
    """
    for i in models:
        p = pylab.poly1d(i)
        pylab.figure()
        pylab.plot(x, y, 'o', x, p(x), '-')
        pylab.xlabel('Years')
        pylab.ylabel('Temperature')
        pylab.title(str(len(i)-1) + '-degree fit\nR=' +
                    str(round(rmse(y, p(x)), 2)))
Beispiel #2
0
 def plot_regression(self):
     # sample plot of points
     x = self.NaN_filtered['tmax']
     y = self.NaN_filtered['tmin']
     fit = polyfit(x, y, 1)
     fit_fn = poly1d(fit)
     # takes in x and returns an estimate for y
     plt.plot(x, y, 'yo', x, fit_fn(x), '--k')
     plt.title("Temperature pattern regression plot")
     plt.xlabel("tmax")
     plt.ylabel("tmin")
     plt.show()
 def plot_regression(self):
     # sample plot of points
     x = self.NaN_filtered['tmax']
     y = self.NaN_filtered['tmin']
     fit = polyfit(x,y,1)
     fit_fn = poly1d(fit)
     # takes in x and returns an estimate for y
     plt.plot(x,y, 'yo', x, fit_fn(x), '--k')
     plt.title("Temperature pattern regression plot")
     plt.xlabel("tmax")
     plt.ylabel("tmin")
     plt.show()        
Beispiel #4
0
def evaluate_models_on_training(x, y, models):
    """
    For each regression model, compute the R-squared value for this model with the
    standard error over slope of a linear regression line (only if the model is
    linear), and plot the data along with the best fit curve.

    For the plots, you should plot data points (x,y) as blue dots and your best
    fit curve (aka model) as a red solid line. You should also label the axes
    of this figure appropriately and have a title reporting the following
    information:
        degree of your regression model,
        R-square of your model evaluated on the given data points,
        and SE/slope (if degree of this model is 1 -- see se_over_slope).

    Args:
        x: an 1-d pylab array with length N, representing the x-coordinates of
            the N sample points
        y: an 1-d pylab array with length N, representing the y-coordinates of
            the N sample points
        models: a list containing the regression models you want to apply to
            your data. Each model is a pylab array storing the coefficients of
            a polynomial.

    Returns:
        None
    """
    for i in models:
        p = pylab.poly1d(i)
        pylab.figure()
        pylab.plot(x, y, 'o', x, p(x), '-')
        pylab.xlabel('Years')
        pylab.ylabel('Degrees Celsius')
        if len(i) <= 2:
            pylab.title(str(len(i)-1) + '-degree fit\nR=' + str(round(r_squared(y, p(x)), 2)) +
                        ', SE=' + str(round(se_over_slope(x, y, p(x), i), 2)))
        else:
            pylab.title(str(len(i)-1) + '-degree fit\nR=' +
                        str(round(r_squared(y, p(x)), 2)))
 def plot_regression(self):
     # sample plot of points
     x = self.filtered.tmax
     y = self.filtered.tmin
     fit = polyfit(x,y,1)
     fit_fn = poly1d(fit)
     # takes in x and returns an estimate for y
     # fig 1
     plt.plot(x,y, 'yo', x, fit_fn(x), '--k')
     plt.title("Temperature pattern regression plot")
     plt.xlabel("tmax")
     plt.ylabel("tmin")
     # fig 2
     plt.figure()
     andrews_curves(self.filtered[[2,3]], 'tmin')
     plt.title("Andrews curve plot for tmin")
     plt.show()
     # fig 3
     plt.figure()
     self.df[[2,3]].boxplot()
     # fig 4
     self.filtered.plot()
     plt.show()
Beispiel #6
0
 def plot_regression(self):
     # sample plot of points
     x = self.filtered.tmax
     y = self.filtered.tmin
     fit = polyfit(x, y, 1)
     fit_fn = poly1d(fit)
     # takes in x and returns an estimate for y
     # fig 1
     plt.plot(x, y, 'yo', x, fit_fn(x), '--k')
     plt.title("Temperature pattern regression plot")
     plt.xlabel("tmax")
     plt.ylabel("tmin")
     # fig 2
     plt.figure()
     andrews_curves(self.filtered[[2, 3]], 'tmin')
     plt.title("Andrews curve plot for tmin")
     plt.show()
     # fig 3
     plt.figure()
     self.df[[2, 3]].boxplot()
     # fig 4
     self.filtered.plot()
     plt.show()
Beispiel #7
0
    def plotStatistics(self):
        self.axScatter.clear()
        self.axHistogram.clear()
        isNone = 0
        for thisSymbol, thisColorLine in zip(self.mainWidget.symbolWidget,
                                             sp.colorLine):
            if thisSymbol.activeSymbol is not None:
                s = thisSymbol.activeSymbol
                m = self.mainWidget.defineMarket.activeMarket
                rSymbol, rMarket, HVaR5, HVaR1 = self.getCommonRange(s, m)
                self.axScatter.plot(rMarket,
                                    rSymbol,
                                    '.',
                                    color=thisColorLine,
                                    markersize=3,
                                    alpha=0.5,
                                    zorder=10)
                if self.plotRPerc is True:
                    rMin = -self.rMax
                    rMax = self.rMax
                    rSym = '%'
                    rFormat = '%.2f'
                else:
                    rMin = -100.0 * self.rMax
                    rMax = 100.0 * self.rMax
                    rSym = 'bp'
                    rFormat = '%3d'
                self.axScatter.set_xlabel('Market/index return [' + rSym + ']')
                self.axScatter.set_ylabel('Symbol return [' + rSym + ']')
                self.axHistogram.set_xlabel('Symbol return [' + rSym + ']')
                self.axScatter.set_xlim([rMin, rMax])
                self.axScatter.set_ylim([rMin, rMax])

                nData = len(rSymbol)
                iID = sp.colorLine.index(thisColorLine)
                self.mainWidget.statSymbol[5*iID].setText('Day avg: ' + rFormat % np.mean(rSymbol) + rSym + \
                                                        '\tAnn avg: ' + rFormat % (np.mean(rSymbol)*260.0) + rSym)
                self.mainWidget.statSymbol[5*iID+1].setText('Day std: ' + rFormat % np.std(rSymbol) + rSym + \
                                                          '\tAnn std: ' + rFormat % (np.std(rSymbol)*np.sqrt(260.0)) + rSym)
                self.mainWidget.statSymbol[5*iID+2].setText('Max: ' + rFormat % max(rSymbol) + rSym + \
                                                          '\tMin: ' + rFormat % min(rSymbol) + rSym)
                self.mainWidget.statSymbol[5*iID+4].setText('Sharpe: ' + rFormat % (np.mean(rSymbol)*np.sqrt(260.0)/np.std(rSymbol)) + \
                                                          '\tHVaR1%: ' + rFormat % HVaR1[0] + rSym)
                beta, alpha, r_value, p_value, std_err = ss.linregress(
                    rMarket, rSymbol)
                rEstimate = plb.poly1d([beta, alpha])
                rStd = np.sqrt(np.mean((rSymbol - rEstimate(rMarket))**2))
                betaEquation = np.cov(rMarket, rSymbol)[0, 1] / np.var(rMarket)
                self.mainWidget.statSymbol[5*sp.colorLine.index(thisColorLine) + 3].setText('beta: ' + '%.2f' % betaEquation + \
                                                                                       '\talpha: ' + rFormat % alpha + rSym)
                if self.plotLeastSquaresLine is True:
                    self.axScatter.plot([rMin, rMax],
                                        rEstimate([rMin, rMax]),
                                        thisColorLine,
                                        linewidth=2.0,
                                        zorder=10)
                if self.plotStdLine is True:
                    self.axScatter.fill_between(
                        [rMin, rMax],
                        np.subtract(rEstimate([rMin, rMax]), rStd),
                        np.add(rEstimate([rMin, rMax]), rStd),
                        color=thisColorLine,
                        alpha=0.2,
                        zorder=10)

                if self.plotCounts is True:
                    weights = np.ones_like(rSymbol)
                    self.axHistogram.set_ylabel('Counts')
                else:
                    weights = np.divide(np.ones_like(rSymbol), len(rSymbol))
                    self.axHistogram.set_ylabel('Frequency')
                if self.plotLine is False:
                    n, bins, patches = self.axHistogram.hist(
                        rSymbol,
                        self.nBins,
                        color=thisColorLine,
                        edgecolor=thisColorLine,
                        weights=weights,
                        log=self.plotLog,
                        range=(-rMax, rMax),
                        linewidth=2.0,
                        alpha=0.3,
                        zorder=10)
                else:
                    n, bins, patches = self.axHistogram.hist(
                        rSymbol,
                        self.nBins,
                        color=thisColorLine,
                        edgecolor=thisColorLine,
                        weights=weights,
                        log=self.plotLog,
                        range=(-rMax, rMax),
                        linewidth=2.0,
                        alpha=0.1,
                        zorder=10)
                    middleBins = []
                    for iBin in range(0, len(bins) - 1):
                        middleBins.append(0.5 * (bins[iBin] + bins[iBin + 1]))
                    self.axHistogram.plot(middleBins,
                                          n,
                                          marker='.',
                                          linestyle='-',
                                          color=thisColorLine,
                                          markersize=5,
                                          linewidth=2.0,
                                          zorder=10)

                self.axHistogram.set_xlim([rMin, rMax])
                if self.plotLog is True:
                    if self.plotCounts is True:
                        self.axHistogram.set_ylim([0.9, max(1.05 * n)])
                    else:
                        self.axHistogram.set_ylim([1.0e-5, 1.0])
                if self.plotVaR is True:
                    if self.plotRPerc is True: j = pc
                    else: j = bp
                    for thisBin, thisPatches in zip(bins, patches):
                        if thisBin < HVaR5[0] and thisBin > HVaR1[0]:
                            thisPatches.set_facecolor(sp.colorNegative)
                            thisPatches.set_alpha(0.5)
                        elif thisBin < HVaR1[0]:
                            thisPatches.set_facecolor(sp.colorVeryNegative)
                            thisPatches.set_alpha(0.5)

            else:
                isNone += 1

        self.decorateAxes()
        if isNone == len(self.mainWidget.symbolWidget):
            self.resetGraph()
        self.draw()