Ejemplo n.º 1
0
def drift(d_param, x):
    evaluated_basis = np.zeros((prm.dim, x.shape[0], prm.dof))
    out = np.zeros((x.shape[0], prm.dim))

    # one dimension at a time, each row of x gets mapped to the hermite basis.
    # both dimensions of x are passed to the hermite function since the hermite
    # functions depend on all dimensions of x.
    for i in range(prm.dim):
        evaluated_basis[i, :, :] = hermite_basis(x)
        out[:, i] = np.sum(np.dot(evaluated_basis[i, :, :], d_param.theta[:, i]))

    return out
Ejemplo n.º 2
0
def mcmc(allx, allt, d_param, em_param, path_index, step_index):
    mmat = np.zeros((prm.dim, prm.dof, prm.dof))
    rvec = np.zeros((prm.dim, prm.dof))
    gammavec = np.zeros((prm.dim))

    # one time series, one interval, all dimensions at a time
    x = allx[path_index, step_index:(step_index + 2), :]
    t = allt[path_index, step_index:(step_index + 2)]
    tdiff = (t[1] - t[0]) / em_param.numsubintervals

    samples = np.zeros((em_param.numsubintervals, prm.dim))
    _, xcur = brownianbridge(d_param, em_param, x, t)
    oldlik = girsanov(d_param, em_param, xcur, tdiff)

    arburn = np.zeros(em_param.burninpaths)
    for jj in range(em_param.burninpaths):
        _, prop = brownianbridge(d_param, em_param, x, t)
        proplik = girsanov(d_param, em_param, prop, tdiff)

        rho = proplik - oldlik
        if (rho > np.log(np.random.uniform())):
            xcur = prop
            oldlik = proplik
            arburn[jj] = 1
    meanBurnin = np.mean(arburn)

    # for each path being sampled (r = 0 to r = R)
    arsamp = np.zeros(em_param.mcmcpaths)
    for jj in range(em_param.mcmcpaths):
        _, prop = brownianbridge(d_param, em_param, x, t)
        proplik = girsanov(d_param, em_param, prop, tdiff)

        rho = proplik - oldlik
        if (rho > np.log(np.random.uniform())):
            xcur = prop
            oldlik = proplik
            arsamp[jj] = 1

        samples = xcur
        pp = hermite_basis(samples[:(-1)])
        mmat += tdiff * np.matmul(pp.T, pp) / em_param.mcmcpaths
        rvec += np.matmul(
            (np.diff(samples, axis=0)).T, pp) / em_param.mcmcpaths
        gammavec += np.sum(np.square(
            np.diff(samples, axis=0) - tdiff * np.matmul(pp, d_param.theta)),
                           axis=0) / (tdiff * em_param.mcmcpaths *
                                      (em_param.numsubintervals *
                                       (allx.shape[1] - 1) + 1))

    meanSample = np.mean(arsamp)

    return (mmat, rvec, gammavec, meanBurnin, meanSample, path_index,
            step_index)
Ejemplo n.º 3
0
def comparison(x, threshold, method_type = 'lasso'):
    y = np.diff(x, axis = 0)
    M = hermite_basis(x[:(-1)])
    clf = linear_model.Lasso(alpha = threshold)
    clf.fit(M, y)
    return theta