Beispiel #1
0
def Pre_Comp_YX(L, T, Xdata, Y, Kbeta, J):
    t = sy.Symbol("t")
    s = sy.Symbol("s")
    # Récupération des variables et paramètres
    N = len(L)
    D = len(L[0])
    # ----------------- Construction de la base fonctionnelle
    syPhi = sy.ones(Kbeta ** 2, 1)
    syb = sy.ones(1, Kbeta ** 2)
    v = [np.arange(Kbeta), np.arange(Kbeta)]
    expo = cg.expandnp(v)
    Phi_fonc = [[] for j in range(Kbeta ** 2)]
    for x in range(len(expo[:, 0])):
        syPhi[x] = (t ** expo[x, 0]) * (s ** expo[x, 1])
        Phi_fonc[x] = sy.lambdify((t, s), syPhi[x], "numpy")
        syb[x] = sy.Symbol("b" + str(x))
    syBeta = syb * syPhi
    Phi_mat = Comp_Phi(Phi_fonc, T, J)
    I_pen = J22_fast(syPhi, np.max(T), 50)[3]
    # ----------------- Construction des noyaux et leurs dérivées
    # Construction de la forme du noyau
    el1 = sy.Symbol("el1")
    per1 = sy.Symbol("per1")
    sig1 = sy.Symbol("sig1")
    args1 = [el1, per1, sig1]
    el2 = sy.Symbol("el2")
    sig2 = sy.Symbol("sig2")
    args2 = [el2, sig2]
    syk = cg.sy_Periodic((s, t), *args1) + cg.sy_RBF((s, t), *args2)
    args = [t, s] + args1 + args2
    # Dérivation et construction des fonctions vectorielles associées
    k_fonc = sy.lambdify(tuple(args), syk, "numpy")
    n_par = len(args) - 2
    k_der = [[] for i in range(n_par)]
    for i in range(n_par):
        func = syk.diff(args[i + 2])
        k_der[i] = sy.lambdify(tuple(args), func, "numpy")
    return (Phi_mat, k_fonc, k_der, I_pen)
Beispiel #2
0
def Pre_Comp(L, T, Xdata, Y, Kbeta, J):
    t = sy.Symbol("t")
    s = sy.Symbol("s")
    # Récupération des variables et paramètres
    N = len(L)
    D = len(L[0])
    # ----------------- INFERENCES DES PARAMS LONGITUDINAUX
    # print("[   ] Inférence des paramètres fonctionnels")
    model = pyGPs.GPR()
    kern1 = pyGPs.cov.RBF(log_ell=0.0, log_sigma=0.0)
    kern2 = pyGPs.cov.Periodic(log_ell=0.0, log_p=0.0, log_sigma=0.0)
    kern = pyGPs.cov.SumOfKernel(kern1, kern2)
    m = pyGPs.mean.Const()
    model.setPrior(mean=m, kernel=kern)
    model.setNoise(log_sigma=-2.30258)
    Theta = np.zeros((N, D, len(model.covfunc.hyp)), float)
    Gamma = np.zeros((N, D), float)
    moy_est = np.zeros((N, D), float)
    for i in range(N):
        for j in range(D):
            y = np.asarray(Xdata[i][j])
            x = np.asarray(L[i][j])
            try:
                model.optimize(x, y)
                moy_est[i, j] = model.meanfunc.hyp[0]
                Theta[i, j, :] = np.array(np.exp(model.covfunc.hyp))
                Gamma[i, j] = np.exp(model.likfunc.hyp)
            except:
                # Problème d'inférence, paramètres défaut attribués
                moy_est[i, j] = np.mean(x)
                Theta[i, j, :] = np.array([0.05, np.std(x) ** 2, 0.05, 1.0, 0.0])
                Gamma[i, j] = 1.0
                pass

    # ----------------- RECUPERATION DES QUANTITES D'INTERET
    # print("[-  ]  Récupération des quantités d'intérêt")
    # Construction de la forme du noyau
    el1 = sy.Symbol("el1")
    sig1 = sy.Symbol("sig1")
    args1 = [el1, sig1]
    el2 = sy.Symbol("el2")
    per2 = sy.Symbol("per2")
    sig2 = sy.Symbol("sig2")
    args2 = [el2, per2, sig2]
    syk = cg.sy_RBF((s, t), *args1) + cg.sy_Periodic((s, t), *args2)
    args = [t, s] + args1 + args2
    k_fonc = sy.lambdify(tuple(args), syk, "numpy")
    Psi = Comp_Psi(L, k_fonc, Theta, Gamma)[0]
    # ----------------- Construction de la base fonctionnelle
    # print("[-- ]  Calcul des quantités d'intérêt")
    syPhi = sy.ones(Kbeta ** 2, 1)
    syb = sy.ones(1, Kbeta ** 2)
    v = [np.arange(Kbeta), np.arange(Kbeta)]
    expo = cg.expandnp(v)
    Phi_fonc = [[] for j in range(Kbeta ** 2)]
    for x in range(len(expo[:, 0])):
        syPhi[x] = (t ** expo[x, 0]) * (s ** expo[x, 1])
        Phi_fonc[x] = sy.lambdify((t, s), syPhi[x], "numpy")
        syb[x] = sy.Symbol("b" + str(x))
    syBeta = syb * syPhi
    I_pen = J22_fast(syPhi, np.max(T), 50)[3]
    # ----------------- Construction de l et V
    Un = np.ones(J + 1, float)
    Un[0] = 0.5
    Un[J] = 0.5
    Phi_mat = Comp_Phi(Phi_fonc, T, J)
    l = np.zeros((N, D * Kbeta ** 2), float)
    V = [[] for i in range(N)]
    vl = [[] for j in range(D)]
    for i in range(N):
        Phi_i = Phi_mat[(i * (J + 1)) : (i * (J + 1) + J + 1), :].T
        for j in range(D):
            Xij = Xdata[i][j]
            # Moyenne de F_ij estimée en amont
            Etaij = moy_est[i, j]
            t = L[i][j]
            grid = T[i] * 1.0 * np.arange(J + 1) / J
            vec = cg.expandnp([t, grid])
            args = list(vec.T) + list(Theta[i, j, :])
            K_ij = np.apply_along_axis(k_fonc, 0, *args).reshape(J + 1, len(t))
            K_ij[0, :] = K_ij[0, :] * 0.5
            K_ij[J, :] = K_ij[J, :] * 0.5
            KPsi = K_ij.dot(Psi[i][j])
            l[i, (j * Kbeta ** 2) : ((j + 1) * Kbeta ** 2)] = (
                Phi_i.dot(KPsi.dot(Xij - Etaij) + Etaij * Un).reshape(-1) / J
            )
            # on calcule pour k_ij, la matrice des k(s,t) pour s,t dans la grille de [0,Ti]
            vec = cg.expandnp([grid, grid])
            args = list(vec.T) + list(Theta[i, j, :])
            k_ij = np.apply_along_axis(k_fonc, 0, *args).reshape(J + 1, J + 1)
            k_ij[0, :] = k_ij[0, :] * 0.5
            k_ij[J, :] = k_ij[J, :] * 0.5
            k_ij[:, 0] = k_ij[:, 0] * 0.5
            k_ij[:, J] = k_ij[:, J] * 0.5
            Cov_FF = k_ij - KPsi.dot(K_ij.T)
            vl[j] = Phi_i.dot(Cov_FF).dot(Phi_i.T) / J ** 2
        V[i] = sc.sparse.block_diag(tuple(vl))
        # On ajoute une matrice diagonale pour rendre V[i] définie positive, mais on fait en sorte que ses valeurs propres soient petites par rapport à celles de V[i] pour ne pas trop affecter la vraisemblances sur l'espace autour de 0.
        # V[i]=V[i]+np.eye(D*Kbeta**2)*0.01*np.trace(V[i].toarray())/(D*Kbeta**2)
    return (l, V, I_pen)