def estimate_components_orthomax(d):
    """
    Compute the PCA/FA components based on the input data d
    as returned by GeoField bootstrap constructor.
    """
    U, s, _ = pca_components_gf(d)
    U = U[:, :NUM_COMPONENTS]
    if not ROTATE_NORMALIZED:
        U *= s[np.newaxis, :NUM_COMPONENTS]
    Ur, _, _ = orthomax(U, gamma=GAMMA, norm_rows=True)
    Ur /= np.sum(Ur**2, axis=0)**0.5
    return Ur
def estimate_components_orthomax(d):
    """
    Compute the PCA/FA components based on the input data d
    as returned by GeoField bootstrap constructor.
    """
    U, s, _ = pca_components_gf(d)
    U = U[:, :NUM_COMPONENTS]
    if not ROTATE_NORMALIZED:
        U *= s[np.newaxis, :NUM_COMPONENTS]
    Ur, _, _ = orthomax(U, gamma = GAMMA, norm_rows=True)
    Ur /= np.sum(Ur**2, axis = 0) ** 0.5
    return Ur
def estimate_components_orthomax(d):
    """
    Compute the PCA/FA components based on the input data d
    as returned by GeoField bootstrap constructor.
    """
    U, s, _ = pca_components_gf(d)
    U = U[:, :NUM_COMPONENTS]
    if not ROTATE_NORMALIZED:
        U *= s[np.newaxis, :NUM_COMPONENTS]
    Ur, _, iters = orthomax(U, rtol = np.finfo(np.float32).eps ** 0.5,
                            gamma = GAMMA,
                            maxiter = 500,
                            norm_rows = ROTATE_NORM_ROWS)
    Ur /= np.sum(Ur**2, axis = 0) ** 0.5
    if iters >= 499:
        print('Warning: max iters reached.')
        return None
    else:
        return Ur
def estimate_components_orthomax(d):
    """
    Compute the PCA/FA components based on the input data d
    as returned by GeoField bootstrap constructor.
    """
    U, s, _ = pca_components_gf(d)
    U = U[:, :NUM_COMPONENTS]
    if not ROTATE_NORMALIZED:
        U *= s[np.newaxis, :NUM_COMPONENTS]
    Ur, _, iters = orthomax(U,
                            rtol=np.finfo(np.float32).eps**0.5,
                            gamma=GAMMA,
                            maxiter=500,
                            norm_rows=ROTATE_NORM_ROWS)
    Ur /= np.sum(Ur**2, axis=0)**0.5
    if iters >= 499:
        print('Warning: max iters reached.')
        return None
    else:
        return Ur
def estimate_components_orthomax(d):
    """
    Compute the PCA/FA components based on the input data d
    as returned by GeoField bootstrap constructor.
    """
    try:
        U, s, _ = pca_components_gf(d)
        U = U[:, :NUM_COMPONENTS]
        Ur, T, iters = orthomax(U,
                                rtol = np.finfo(np.float32).eps ** 0.5,
                                gamma = GAMMA,
                                maxiter = 500)
        if iters >= 499:
            return None
        else:
            return Ur, T
    except LinAlgError as e: 
        print("**LINALG ERROR** code: %d text : %s" % (e.errno, e.strerror))
    except:
        print("**UNEXPECTED ERROR** %s" % sys.exc_info()[0])
def compute_lno_sample_components(x):
    gf, Urd, i, j = x
    b = gf.data()
    b = np.vstack([b[:i, ...], b[j:, ...]])
    U, _, _ = pca_components_gf(b)
    Ur, _, _ = orthomax(U[:, :NUM_COMPONENTS])

    # compute closeness of components
    C = np.dot(Ur.T, Urd)

    # find optimal matching of components
    m = Munkres()
    match = m.compute(1.0 - np.abs(C))
    perm = np.zeros((NUM_COMPONENTS, ), dtype=np.int)
    for i in range(len(match)):
        m_i = match[i]
        perm[m_i[0]] = m_i[1]
        # flip the sign in the matched boostrap component if the correlation was negative
        Ur[m_i[1]] = -Ur[m_i[1]] if C[m_i[0], m_i[1]] < 0.0 else Ur[m_i[1]]

    # reorder the bootstrap components according to the best matching
    Ur = Ur[:, perm]

    return Ur
def compute_lno_sample_components(x):
    gf, Urd, i, j = x
    b = gf.data()
    b = np.vstack([b[:i,...], b[j:,...]])
    U, _, _ = pca_components_gf(b)
    Ur, _, _ = orthomax(U[:, :NUM_COMPONENTS])
    
    # compute closeness of components
    C = np.dot(Ur.T, Urd)
    
    # find optimal matching of components
    m = Munkres()
    match = m.compute(1.0 - np.abs(C))
    perm = np.zeros((NUM_COMPONENTS,), dtype = np.int)
    for i in range(len(match)):
        m_i = match[i]
        perm[m_i[0]] = m_i[1]
        # flip the sign in the matched boostrap component if the correlation was negative
        Ur[m_i[1]] = - Ur[m_i[1]] if C[m_i[0], m_i[1]] < 0.0 else Ur[m_i[1]]
        
    # reorder the bootstrap components according to the best matching 
    Ur = Ur[:, perm]
    
    return Ur
#gf.slice_months([12, 1, 2])

#S = np.zeros(shape = (5, 10), dtype = np.int32)
#S[1:4, 0:2] = 1
#S[0:3, 6:9] = 2
#v, Sr = constructVAR(S, [0.0, 0.191, 0.120], [-0.1, 0.1], [0.00, 0.00], [0.01, 0.01])
#ts = v.simulate(768)
#gf = make_model_geofield(S, ts)

# initialize a parallel pool
pool = Pool(POOL_SIZE)

# compute components for data
Ud, sd, Vtd = pca_components_gf(gf.data())
Ud = Ud[:, :NUM_COMPONENTS]
Ur, _, its = orthomax(Ud)
print("Finished after %d iterations." % its)

t_start = datetime.now()

LNO_COUNT = len(gf.tm) // LNO_PAR
#LNO_COUNT = 4
print("Running leave one out analysis [%d samples] at %s" %
      (LNO_COUNT, str(t_start)))

# initialize maximal and minimal boostraps
EXTREMA_MEMORY = math.ceil(DISCARD_RATE * LNO_COUNT)
max_comp = np.tile(np.abs(Ur.copy()), (EXTREMA_MEMORY + BULK_STEP, 1, 1))
min_comp = np.tile(np.abs(Ur.copy()), (EXTREMA_MEMORY + BULK_STEP, 1, 1))
mean_comp = np.zeros_like(Ur)
var_comp = np.zeros_like(Ur)
Пример #9
0
    v_sel = [0, 0, 0, 0, 1, 1, 1, 1, 2, 2]
    for i in range(10):
        X[:, i] = V[:, v_sel[i]] + np.random.normal(size=(N, ), scale=sigma)

    return X


if __name__ == '__main__':

    # program the prolific test example used by everybody

    X = make_test_data1(1000, 1.0)

    C = spca_meng.extract_sparse_components(X.T, 3, 3)
    PC, _, _ = component_analysis.pca_components(X.T)
    PC = PC[:, :3]
    UR, _, _ = component_analysis.orthomax(PC)

    print C

    plt.figure()
    plt.subplot(311)
    plt.plot(C)
    plt.title('Sparse PCA')
    plt.subplot(312)
    plt.plot(PC)
    plt.title('PCA components')
    plt.subplot(313)
    plt.plot(UR)
    plt.title('Rotated components')
    plt.show()
Пример #10
0
    v_sel = [0, 0, 0, 0, 1, 1, 1, 1, 2, 2]
    for i in range(10):
        X[:,i] = V[:,v_sel[i]] + np.random.normal(size = (N,), scale = sigma)
    
    return X
    

if __name__ == '__main__':
    
    # program the prolific test example used by everybody
    
    X = make_test_data1(1000, 1.0)
    
    C = spca_meng.extract_sparse_components(X.T, 3, 3)
    PC,_,_ = component_analysis.pca_components(X.T)
    PC = PC[:,:3]
    UR, _, _ = component_analysis.orthomax(PC)
    
    print C
    
    plt.figure()
    plt.subplot(311)
    plt.plot(C)
    plt.title('Sparse PCA')
    plt.subplot(312)
    plt.plot(PC)
    plt.title('PCA components')
    plt.subplot(313)
    plt.plot(UR)
    plt.title('Rotated components')
    plt.show()
#S = np.zeros(shape = (5, 10), dtype = np.int32)
#S[1:4, 0:2] = 1
#S[0:3, 6:9] = 2
#v, Sr = constructVAR(S, [0.0, 0.191, 0.120], [-0.1, 0.1], [0.00, 0.00], [0.01, 0.01])
#ts = v.simulate(768)
#gf = make_model_geofield(S, ts)


# initialize a parallel pool
pool = Pool(POOL_SIZE)

# compute components for data
Ud, sd, Vtd = pca_components_gf(gf.data())
Ud = Ud[:, :NUM_COMPONENTS]
Ur, _, its = orthomax(Ud)
print("Finished after %d iterations." % its)

t_start = datetime.now()

LNO_COUNT = len(gf.tm) // LNO_PAR
#LNO_COUNT = 4
print("Running leave one out analysis [%d samples] at %s" % (LNO_COUNT, str(t_start)))

# initialize maximal and minimal boostraps
EXTREMA_MEMORY = math.ceil(DISCARD_RATE * LNO_COUNT)
max_comp = np.tile(np.abs(Ur.copy()), (EXTREMA_MEMORY + BULK_STEP, 1, 1))
min_comp = np.tile(np.abs(Ur.copy()), (EXTREMA_MEMORY + BULK_STEP, 1, 1))
mean_comp = np.zeros_like(Ur)
var_comp = np.zeros_like(Ur)