Example #1
0
    def plot_single_coefficient(self,
                                beta,
                                ylim=None,
                                ax=None,
                                highlight_median="green"):
        """
        Plot the dynamic distribution of a single model coefficient.

        Args:
          beta: The coefficient to be plotted.  A matrix.  Rows are Monte Carlo
            draws, and columns are time points.
          ylim: A pair of numbers giving the lower and upper limits of the Y
            axis.  If 'None' then 'ylim' will be inferred from the range of the
            data.
          ax: A plt.Axes object on which to draw.  If None then a new
            plt.Figure and Axes will be created and drawn on function exit.
          highlight_median: The name of a color used to draw the meadian of the
            curves at each time point.  The empty string signals not to add the
            extra highlighting.

        Returns:
          The axes object containing the plot.
        """
        fig = None
        if ax is None:
            fig, ax = plt.subplots(1, 1)
        R.plot_dynamic_distribution(beta,
                                    timestamps=self._unique_timestamps,
                                    ax=ax,
                                    ylim=ylim,
                                    highlight_median=highlight_median)
        if fig is not None:
            fig.show()
        return ax
Example #2
0
    def plot_state(self,
                   burn=None,
                   time=None,
                   show_actuals=True,
                   style=None,
                   scale=None,
                   ylim=None,
                   ax=None,
                   **kwargs):
        if style is None:
            style = "dynamic"
        style = R.unique_match(style, ["dynamic", "boxplot"])

        if scale is None:
            scale = "linear"
        scale = R.unique_match(scale, ["linear", "mean"])

        niter = self._niter
        if burn is None:
            burn = self.suggest_burn()

        if time is None:
            time = self.original_series.index

        state_contribution = np.zeros((niter, len(time)))
        for model in self._state_models:
            state_contribution += model.state_contribution

        R.plot_dynamic_distribution(
            curves=state_contribution,
            timestamps=time,
            ax=ax,
            ylim=ylim,
            **kwargs)
Example #3
0
    def plot_size(self, ax=None, burn: int = None, **kwargs):
        fig = None
        if ax is None:
            fig, ax = plt.subplots(1, 1)

        size = np.sum(self._beta_draws != 0, axis=1)
        R.plot_dynamic_distribution(
            size,
            timestamps=self._unique_timestamps,
            ax=ax,
            xlab="Time",
            ylab="Number Included Predictors",
        )

        if fig is not None:
            fig.show()
        return ax
Example #4
0
    def plot_state_contribution_default(self,
                                        fig,
                                        gridspec,
                                        time,
                                        burn=None,
                                        ylim=None,
                                        **kwargs):
        """
        A default implementation of plot_state_distribution.

        Plots the contribution of this state model to the conditional mean at
        time t on the supplied set of axes using a dynamic distribution plot.

        Args:
          ax:  The axes object on which to draw the plot.
          time:  The time axis.
          burn:  The number of iterations to discard as burn-in.
          ylim:  A pair giving the lower and upper limits on the y axis.
          kwargs:  Extra arguments passed to plot_dynamic_distribution.

        Effects:
          Creates a dynamic distribution plot on the supplied set of axes.

        Returns:
          The axes object.
        """
        if burn > 0:
            curves = self._state_contribution[burn:, :]
        else:
            curves = self._state_contribution

        ax = fig.add_subplot(gridspec)

        R.plot_dynamic_distribution(curves=curves,
                                    timestamps=time,
                                    ax=ax,
                                    ylim=ylim,
                                    **kwargs)

        return ax