Example #1
0
    def fit_predict(self,
                    X,
                    period_frequencies=None,
                    full_series=False,
                    num_iter=20):

        if not isinstance(X, TimeSeriesDataset):
            X = np.asanyarray(X, dtype='d')
        else:
            X = X.np_like_firstn()

        n = X.shape[0]
        if period_frequencies is None:
            period_frequencies = np.ones(n)
        else:
            period_frequencies = np.asanyarray(period_frequencies, dtype='d')

        assert period_frequencies.shape[0] == n

        P = np.zeros(shape=(n, TOTAL_PARAMS), dtype='d')
        with np.errstate(over='raise'):
            for i in xrange(X.shape[0]):
                P[i] = fit_one(X[i], period_frequencies[i], num_iter)

        if full_series:
            Y = np.zeros((n, X.shape[1] + self.steps_ahead), dtype='d')
        else:
            Y = np.zeros((n, ), dtype='d')

        for i in xrange(X.shape[0]):
            if full_series:
                Y[i] = spikem(P[i], X.shape[1] + self.steps_ahead)
            else:
                Y[i] = spikem(P[i], X.shape[1] + self.steps_ahead)[-1]
        return Y
Example #2
0
def fit_one(tseries, period, num_iter=20):
    duration = tseries.shape[0]

    init_params, bounds = get_initial_parameters(tseries, period)
    curr_params = init_params.copy()

    nonz = np.where(tseries > 0)[0]
    if nonz.shape[0] > 0:
        st = max(nonz[0] - 1, 0)
    else:
        st = 0

    ed = tseries.argmax()

    def mse_func(lm_params, param_name, all_parameters):
        param_value = lm_params[param_name].value
        parameter_idx = PARAMETERS[param_name]
        predicted = spikem_wrapper(param_value, parameter_idx, all_parameters,
                                   tseries.shape[0])

        return np.array([np.sqrt(((predicted - tseries)**2).mean())])

    for _ in range(num_iter):
        for param_name in TO_FIT:
            param_idx = PARAMETERS[param_name]

            if param_name == 'nb':
                best = 0
                min_mse = float('inf')

                copy_params = curr_params.copy()
                for i in xrange(st, ed):
                    copy_params[param_idx] = i
                    predicted = spikem(copy_params, tseries.shape[0])

                    try:
                        mse = np.sqrt(((predicted - tseries)**2).mean())
                    except:  #overflow
                        mse = float('inf')

                    if mse < min_mse:
                        best = i

                curr_params[param_idx] = best
            else:
                bound = bounds[param_idx]
                lm_params = to_lmstyle(param_name, curr_params, bounds)
                try:
                    lmfit.minimize(mse_func, lm_params, \
                            args=(param_name, curr_params))
                    curr_params[param_idx] = lm_params[param_name].value
                except:  #overflow
                    pass

    return curr_params