def time_derivative_of_interpolatated_path(self, q_n_i, q_n_plus_1_i, t_lower, t_upper, t):
        """
        Interpolate the path of a single q degree of freedom.
        This function should be evaluated for each DOF in the solution separately.
        """

        # path = q_n_i + (q_n_plus_1_i - q_n_i) * 1.0 / (t_upper - t_lower)
        # return path

        if hasattr(q_n_i, '_value'):
            q_n_i_unpacked = q_n_i._value
        else:
            q_n_i_unpacked = q_n_i

        if hasattr(q_n_plus_1_i, '_value'):
            q_n_plus_1_i_unpacked = q_n_plus_1_i._value
        else:
            q_n_plus_1_i_unpacked = q_n_plus_1_i

        coefficients = np.flip(
            np.polyfit([t_lower, t_upper], [q_n_i_unpacked, q_n_plus_1_i_unpacked], self.order_of_integrator))
        coefficients_of_differentiated_path = [0 for i in coefficients]
        for index, c in enumerate(coefficients):
            if index < len(coefficients) - 1:
                coefficients_of_differentiated_path[index] = coefficients[index + 1] * (index + 1)
            else:
                coefficients_of_differentiated_path[index] = 0

        return self.evaluate_polynomial(coefficients_of_differentiated_path, t)
Beispiel #2
0
    def mpp(self,
            x,
            c=None,
            n=None,
            heuristic="Nelson-Aalen",
            rr='y',
            on_d_is_0=False,
            offset=False):
        assert rr in ['x', 'y']
        x_pp, r, d, F = nonp.plotting_positions(x,
                                                c=c,
                                                n=n,
                                                heuristic=heuristic)

        if not on_d_is_0:
            x_pp = x_pp[d > 0]
            F = F[d > 0]

        # Linearise
        y_pp = self.mpp_y_transform(F)

        mask = np.isfinite(y_pp)
        if mask.any():
            warnings.warn(
                "Some Infinite values encountered in plotting points and have been ignored.",
                stacklevel=2)
            y_pp = y_pp[mask]
            x_pp = x_pp[mask]

        if offset:
            if rr == 'y':
                params = np.polyfit(x_pp, y_pp, 1)
            elif rr == 'x':
                params = np.polyfit(y_pp, x_pp, 1)
            failure_rate = params[0]
            offset = -params[1] * (1. / failure_rate)
            return tuple([offset, failure_rate])
        else:
            if rr == 'y':
                x_pp = x_pp[:, np.newaxis]
                failure_rate = np.linalg.lstsq(x_pp, y_pp, rcond=None)[0]
            elif rr == 'x':
                y_pp = y_pp[:, np.newaxis]
                mttf = np.linalg.lstsq(y_pp, x_pp, rcond=None)[0]
                failure_rate = 1. / mttf
            return tuple([failure_rate[0]])
Beispiel #3
0
    def linear_fit_params(params, samples):
        '''Pooled least square fit'''

        degree = params['linear_mean_coef'].shape[0] - 1
        t = np.concatenate([t for y, (t, rx) in samples])
        y = np.concatenate([y for y, (t, rx) in samples])
        params['linear_mean_coef'] = np.polyfit(t, y, degree)
        return params
Beispiel #4
0
def QuadFitCurvatureMap(x):
    curv = []

    for i in range(len(x)):
        # do a look-ahead quadratic fit, just like the car would do
        pts = x[(np.arange(6) + i) % len(x)] / 100  # convert to meters
        basis = (pts[1] - pts[0]) / np.abs(pts[1] - pts[0])

        # project onto forward direction
        pts = (np.conj(basis) * (pts - pts[0]))

        p = np.polyfit(np.real(pts), np.imag(pts), 2)
        curv.append(p[0] / 2)

    return np.float32(curv)
Beispiel #5
0
def QuadFitCurvatureMap(x):
    curv = []

    for i in range(len(x)):
        # do a look-ahead quadratic fit, just like the car would do
        pts = x[(np.arange(6) + i) % len(x)] / 100  # convert to meters
        basis = (pts[1] - pts[0]) / np.abs(pts[1] - pts[0])

        # project onto forward direction
        pts = (np.conj(basis) * (pts - pts[0]))

        p = np.polyfit(np.real(pts), np.imag(pts), 2)
        curv.append(p[0] / 2)

    return np.float32(curv)
Beispiel #6
0
 def phi_init(self, life, X):
     X = X.flatten()
     a, c = np.polyfit(1. / X, np.log(1. / life) + np.log(X), 1)
     return [a, -c]
Beispiel #7
0
 def phi_init(self, life, X):
     X = X.flatten()
     a, b = (np.polyfit(1. / X, np.log(life), 1))
     return [a, np.exp(b)]
Beispiel #8
0
    def mpp(self,
            x,
            c=None,
            n=None,
            heuristic="Nelson-Aalen",
            rr='y',
            on_d_is_0=False,
            offset=False):

        x_pp, r, d, F = nonp.plotting_positions(x,
                                                c=c,
                                                n=n,
                                                heuristic=heuristic)

        results = {}

        if on_d_is_0:
            pass
        else:
            F = F[d > 0]
            x_pp = x_pp[d > 0]

        if (F == 1).any():
            mask = F != 1
            warnings.warn("Some heuristic values for CDF = 1 have been " +
                          "encountered in plotting points and have been " +
                          "ignored.",
                          stacklevel=2)
            F = F[mask]
            x_pp = x_pp[mask]

        init = self._parameter_initialiser(x_pp, c, n)

        mask = np.isfinite(F)
        if mask.any():
            warnings.warn("Some Infinite values encountered in plotting " +
                          "points and have been ignored.",
                          stacklevel=2)
            F = F[mask]
            x_pp = x_pp[mask]

        if offset:

            def fun(a):
                return -pearsonr(x_pp, self.mpp_y_transform(F, a, 1.))[0]

            res = minimize(fun, init[0], bounds=((0, None), ))
            alpha = res.x[0]

            y_pp = self.mpp_y_transform(F, alpha)

            if rr == 'y':
                params = np.polyfit(x_pp, y_pp, 1)
                beta = params[0]
                gamma = -params[1] / beta
            elif rr == 'x':
                params = np.polyfit(y_pp, x_pp, 1)
                beta = 1. / params[0]
                gamma = -params[1] / beta

            results['gamma'] = gamma
            results['params'] = [alpha, beta]

            return results
        else:
            if rr == 'y':
                x_pp = x_pp[:, np.newaxis]

                def fun(alpha):
                    y_pp = self.mpp_y_transform(F, alpha, 1.)
                    return np.linalg.lstsq(x_pp, y_pp)[1]

                res = minimize(fun, init[0], bounds=((0, None), ))
                alpha = res.x[0]
                y_pp = self.mpp_y_transform(F, alpha, 1.)
                beta, residuals, _, _ = np.linalg.lstsq(x_pp, y_pp)
                beta = beta[0]
            else:

                def fun(a):
                    y = self.mpp_y_transform(F, a, 1.)[:, np.newaxis]
                    return np.linalg.lstsq(y, x)[1]

                res = minimize(fun, init[0], bounds=((0, None), ))
                alpha = res.x[0]
                beta = np.linalg.lstsq(
                    self.mpp_y_transform(F, alpha, 1.)[:, np.newaxis], x)[0]
                beta = 1. / beta

            results['params'] = [alpha, beta]

            return results
Beispiel #9
0
def mpp(model):
    """
    MPP: Method of Probability Plotting

    This is the classic probability plotting paper method. This method
    creates the plotting points, transforms it to Weibull scale and then fits
    the line of best fit.
    """
    dist = model.dist
    x, c, n, t = (model.data['x'], model.data['c'], model.data['n'],
                  model.data['t'])

    heuristic = model.fitting_info['heuristic']
    on_d_is_0 = model.fitting_info['on_d_is_0']
    offset = model.offset
    rr = model.fitting_info['rr']
    turnbull_estimator = model.fitting_info['turnbull_estimator']

    if rr not in ['x', 'y']:
        raise ValueError("rr must be either 'x' or 'y'")

    if hasattr(dist, 'mpp'):
        return dist.mpp(x,
                        c,
                        n,
                        heuristic=heuristic,
                        rr=rr,
                        on_d_is_0=on_d_is_0,
                        offset=offset)

    x_, r, d, F = plotting_positions(x=x,
                                     c=c,
                                     n=n,
                                     t=t,
                                     heuristic=heuristic,
                                     turnbull_estimator=turnbull_estimator)

    x_mask = np.isfinite(x_)
    x_ = x_[x_mask]
    F = F[x_mask]
    d = d[x_mask]
    r = r[x_mask]

    if not on_d_is_0:
        x_ = x_[d > 0]
        y_ = F[d > 0]
    else:
        y_ = F

    mask = (y_ != 0) & (y_ != 1)
    y_pp = y_[mask]
    x_pp = x_[mask]
    y_pp = dist.mpp_y_transform(y_pp)

    if offset:
        # I think this should be x[c != 1] and not in xl
        x_min = np.min(x_pp)

        # fun = lambda gamma : -pearsonr(np.log(x - gamma), y_)[0]
        def fun(gamma):
            g = x_min - np.exp(-gamma)
            out = -pearsonr(dist.mpp_x_transform(x_pp - g), y_pp)[0]
            return out

        res = minimize(fun, 0.)
        gamma = x_min - np.exp(-res.x[0])
        x_pp = x_pp - gamma

    x_pp = dist.mpp_x_transform(x_pp)

    if rr == 'y':
        params = np.polyfit(x_pp, y_pp, 1)
    elif rr == 'x':
        params = np.polyfit(y_pp, x_pp, 1)

    params = dist.unpack_rr(params, rr)

    results = {}

    if offset:
        results['gamma'] = gamma

    results['params'] = params
    results['rr'] = rr

    return results
Beispiel #10
0
 def phi_init(self, life, X):
     X = X.flatten()
     n, a = (np.polyfit(np.log(X), np.log(life), 1))
     return [np.exp(a), n]
Beispiel #11
0
 def phi_init(self, life, X):
     X = X.flatten()
     b, a = np.polyfit(X, life, 1)
     return [a, b]