Estimate changepoints in a time series by using R.
189850
def estimate_cp_pval(ts, method="mean"):
    

    """ 
        ts: time series
        method: look for a single changepoint in 'mean' , 'var', 'mean and var'
        Returns: returns index of the changepoint, and pvalue. Here pvalue = 1
                 means statistically significant
    """
    robjects.r("library(changepoint)")
    method_map = {
        "mean": "cpt.mean({}, class=FALSE)",
        "var": "cpt.var({}, class=FALSE)",
        "meanvar": "cpt.meanvar({}, class=FALSE)",
    }
    mt = robjects.FloatVector(ts)
    robjects.globalenv["mt"] = mt
    cmd = method_map[method].format("mt")
    robjects.globalenv["mycpt"] = robjects.r(cmd)
    ecp_pval = robjects.r("mycpt")
    ecp = ecp_pval[0]
    pval = ecp_pval[1]
    return ecp, pval
299864
def time_segments_average(X, interval, time_column):
    """"""
    warnings.warn(_TIME_SEGMENTS_AVERAGE_DEPRECATION_WARNING, DeprecationWarning)

    if isinstance(X, np.ndarray):
        X = pd.DataFrame(X)

    X = X.sort_values(time_column).set_index(time_column)

    start_ts = X.index.values[0]
    max_ts = X.index.values[-1]

    values = list()
    index = list()
    while start_ts <= max_ts:
        end_ts = start_ts + interval
        subset = X.loc[start_ts:end_ts - 1]
        means = subset.mean(skipna=True).values
        values.append(means)
        index.append(start_ts)
        start_ts = end_ts

    return np.asarray(values), np.asarray(index)
123200
def smoothed_moving_average(data, period):
    
    catch_errors.check_for_period_error(data, period)
    series = pd.Series(data)
    return series.ewm(alpha = 1.0/period).mean().values.flatten()
301512
def _reshape_timeseries(self, ts):
        """"""
        if np.count_nonzero(np.diff(self._sublengths)) == 0:
            # then all submodels have the same dimension, so can reshape array
            # in place without copying data:
            subdim = self.dimension // self._n
            shp = list(ts.shape)
            shp[1] = self._n
            shp.insert(2, subdim)
            ts = ts.reshape(tuple(shp)).swapaxes(1, 2)
            # label variables only if all sub-models agree on the labels:
            all_var_labels = [m.labels for m in self.submodels]
            var_labels = all_var_labels[0]
            if all(v == var_labels for v in all_var_labels[1:]):
                ts.labels[1] = var_labels
            ts.labels[2] = self._node_labels()
            return ts
        else:
            # will pad with zeros for submodels with less variables
            subdim = max(self._sublengths)
            shp = list(ts.shape)
            shp[1] = subdim
            shp.insert(2, self._n)
            ar = np.zeros(shp)
            labels = ts.labels
            labels.insert(2, self._node_labels())
            for k in range(self._n):
                sl = slice(self._si[k], self._si[k+1])
                ar[:,:,k,...] = ts[:,sl,...]
            return Timeseries(ar, ts.tspan, labels)
1370515
def rhochange(self):
        """"""

        if self.opt['HighMemSolve']:
            self.c = sl.solvedbi_sm_c(self.gDf, np.conj(self.gDf), self.rho,
                                      self.cri.axisM)
998647
def hilbert(ts):
    """"""
    output = signal.hilbert(signal.detrend(ts, axis=0), axis=0)
    return Timeseries(output, ts.tspan, labels=ts.labels)
1372395
def loadCurve(data, groups, thresholds, absvals, fs, xlabels):
        """"""
        xlims = (xlabels[0], xlabels[-1])
        pw = ProgressWidget(groups, xlims)
        spike_counts = []
        # skip control
        for itrace in range(data.shape[0]):
            count = 0
            for ichan in range(data.shape[2]):
                flat_reps = data[itrace,:,ichan,:].flatten()
                count += len(spikestats.spike_times(flat_reps, thresholds[ichan], fs, absvals[ichan]))
            spike_counts.append(count/(data.shape[1]*data.shape[2])) #mean spikes per rep

        i = 0
        for g in groups:
            for x in xlabels:
                pw.setPoint(x, g, spike_counts[i])
                i +=1

        return pw
358227
def pstdev(data):
    """"""
    #: http://stackoverflow.com/a/27758326
    n = len(data)
    if n < 2:
        raise ValueError('variance requires at least two data points')
    ss = _ss(data)
    pvar = ss/n  # the population variance
    return pvar**0.5
63479
def event_estimation(self, event, logliks, logsumexps, renamed=''):
        
        plotter = _ModelLoglikPlotter()
        plotter.plot(event, logliks, logsumexps, self.modality_to_color,
                     renamed=renamed)
        return plotter
258084
def turningpoint(self):
        """"""
        coefs = self.coefs
        old_dc = coefs[1]-coefs[0]
        for idx in range(self.order-2):
            new_dc = coefs[idx+2]-coefs[idx+1]
            if (old_dc < 0.) and (new_dc > old_dc):
                return idx, coefs[idx]
            old_dc = new_dc
        raise RuntimeError(
            'Not able to detect a turning point in the impulse response '
            'defined by the MA coefficients %s.'
            % objecttools.repr_values(coefs))
495748
def predict(self, dataframe):
        """"""
        if self.mean > 0.5:
            return numpy.ones(dataframe.shape[0])
        else:
            return numpy.zeros(dataframe.shape[0])
281814
def triangular_moving_average(data, period):
    
    catch_errors.check_for_period_error(data, period)

    tma = sma(sma(data, period), period)
    return tma
1112301
def detect_autocorrelation(time)
      analysis[time.to_sym].detect_autocorrelation(
        self.case.autocorrelation.max_lags.to_i,
        self.case.autocorrelation.alpha_level.abs)
    end
361150
def timeseries(self, dataframe=False):
        

        self.query.get_cardinality("author_uuid").by_period()
        return super().timeseries(dataframe)
1325658
def plot_fit(self,intervals=False,**kwargs):
        
        import matplotlib.pyplot as plt
        import seaborn as sns

        figsize = kwargs.get('figsize',(10,7))
        series_type = kwargs.get('series_type','Smoothed')

        if self.latent_variables.estimated is False:
            raise Exception("No latent variables estimated!")
        else:
            date_index = copy.deepcopy(self.index)
            date_index = date_index[:self.y.shape[0]+1]

            if series_type == 'Smoothed':
                mu, V = self.smoothed_state(self.data,self.latent_variables.get_z_values())
            elif series_type == 'Filtered':
                mu, V, _, _, _ = self._model(self.data,self.latent_variables.get_z_values())
            else:
                mu, V = self.smoothed_state(self.data,self.latent_variables.get_z_values())

            # Create smoothed/filtered aggregate series
            _, Z, _, _, _ = self._ss_matrices(self.latent_variables.get_z_values())
            smoothed_series = np.zeros(self.y.shape[0])

            for t in range(0,self.y.shape[0]):
                smoothed_series[t] = np.dot(Z[t],mu[:,t])

            plt.figure(figsize=figsize) 
            
            plt.subplot(self.z_no+1, 1, 1)
            plt.title(self.y_name + " Raw and " + series_type)  
            plt.plot(date_index,self.data,label='Data')
            plt.plot(date_index,smoothed_series,label=series_type,c='black')
            plt.legend(loc=2)

            for coef in range(0,self.z_no-1):
                V_coef = V[0][coef][:-1]    
                plt.subplot(self.z_no+1, 1, 2+coef)
                plt.title("Beta " + self.X_names[coef]) 

                if intervals == True:
                    alpha =[0.15*i/float(100) for i in range(50,12,-2)]
                    plt.fill_between(date_index[5:], mu[coef,0:mu.shape[1]-1][5:] + 1.98*np.sqrt(V_coef[5:]), mu[coef,0:mu.shape[1]-1][5:] - 1.98*np.sqrt(V_coef[5:]), alpha=0.15,label='95% C.I.') 
                plt.plot(date_index,mu[coef,0:mu.shape[1]-1],label='Data')
                plt.legend(loc=2)               
            
            plt.subplot(self.z_no+1, 1, self.z_no+1)
            plt.title("Measurement Error")
            plt.plot(date_index,self.data-smoothed_series,label='Irregular')
            plt.legend(loc=2)   

            plt.show()
970270
def _sanity_check(self):
        
        if len(self.time_series_a) < 2 or len(self.time_series_b) < 2:
            raise exceptions.NotEnoughDataPoints('luminol.Correlator: Too few data points!')
242167
def _get_r(cls, df, column, shifts):
        
        shift = cls.to_int(shifts)
        rate_key = '{}_{}_r'.format(column, shift)
        df[rate_key] = df[column].pct_change(periods=-shift) * 100