Ejemplo n.º 1
0
double K_crossval_l1o(ok_kernel* k, int minalgo, int maxiter, double params[]) {
    
    int nd = K_getNdata(k);
    
    
    int np = omp_get_max_threads();
    ok_kernel* ks[np];
    double lh[np];
    ok_progress prog = k->progress;
    
    for (int p = 0; p < np; p++) {
        ks[p] = K_clone(k);
        ks[p]->progress = NULL;
        lh[p] = 0.;
    }
    bool invalid = false;
    
    #pragma omp parallel for
    for (int i = 0; i < nd; i++) {
        if (invalid)
            continue;
        
        int p = omp_get_thread_num();
        gsl_matrix_memcpy(ks[p]->system->elements, k->system->elements);
        gsl_vector_memcpy(ks[p]->params, k->params);
        
        K_calculate(ks[p]);
        double err = ks[p]->compiled[i][T_ERR];
        int set = (int) (ks[p]->compiled[i][T_SET]);
        double n = K_getPar(ks[p], set + DATA_SETS_SIZE);
        
        
        ks[p]->compiled[i][T_ERR] = 100000.;
        
        K_minimize(ks[p], minalgo, maxiter, params);
        
        double s = sqrt(err*err + n*n);
        double diff = fabs(ks[p]->compiled[i][T_SVAL] - ks[p]->compiled[i][T_PRED]);
        lh[p] += log10(diff/s);
        ks[p]->compiled[i][T_ERR] = err;
        
        if (prog != NULL && omp_get_thread_num() == 0) {
            int ret = prog(i * np, nd, ks[p],
                    "K_crossVal_l1o");
            if (ret == PROGRESS_STOP) {
                invalid = true;
            }
        }
    }
 
    double lh2 = 0;
    for (int p = 0; p < np; p++) {
        lh2 += lh[p];
        K_free(ks[p]);
    }
    if (invalid)
        return -1;
    
    return fabs(lh2);
}
Ejemplo n.º 2
0
int main() {
    FILE* fid = fopen("test.fit", "r");
    ok_kernel* k = K_load(fid, 0);
    k = K_clone(k);
    printf("%s %s\n", K_getDataName(k, 0), K_getDataName(k, 1));
    
    
}
Ejemplo n.º 3
0
gsl_matrix* ok_periodogram_full(ok_kernel* k, int type, int algo, bool circular, unsigned int sample,
                                const unsigned int samples, const double Pmin, const double Pmax) {

    k = K_clone(k);
    K_calculate(k);

    // Input data for LS periodogram
    gsl_matrix* data = ok_buf_to_matrix(K_compileData(k), K_getNdata(k), DATA_SIZE);


    if (type == PS_TYPE_RESIDUALS) {
        // If residuals periodogram, subtract signal from data
        for (int i = 0; i < data->size1; i++)
            MSET(data, i, T_SVAL, MGET(data, i, T_SVAL) - MGET(data, i, T_PRED));
    } else if (type == PS_TYPE_DATA) {
        // If full periodogram, then start with no planets
        K_removePlanet(k, -1);
    }

    // Calculate LS periodogram
    gsl_matrix* ret = ok_periodogram_ls(data, samples, Pmin, Pmax, 0, T_TIME, T_SVAL, T_ERR, NULL);
    int np = K_getNplanets(k) + 1;

    // Number of minimizable offsets
    int no = 0;
    for (int i = 0; i < DATA_SETS_SIZE; i++)
        if (VIGET(k->parFlags, i) & MINIMIZE) {
            no++;
        }

    // Calculate baseline chi^2 (Chi^2_H)

    double Chi2_H = _kminimize(k, algo);

    // Normalizing factor for power
    double nd = 0.5 * (K_getNdata(k) - no);



    #pragma omp parallel for
    for (int r = 0; r < samples; r++) {
        double P = MGET(ret, r, PS_TIME);
        double K = sqrt(MGET(ret, r, PS_Z));

        ok_kernel* k2 = K_clone(k);
        K_calculate(k2);

        double args[] = {PER, P, DONE};
        K_addPlanet(k2, args);
        K_setElement(k2, np, SEMIAMP, K);

        K_setElementFlag(k2, np, PER, ACTIVE);

        if (circular) {
            K_setElementFlag(k2, np, ECC, ACTIVE);
            K_setElementFlag(k2, np, LOP, ACTIVE);
        }

        double Chi2_K = _kminimize(k2, algo);

        double z = nd * (Chi2_H - Chi2_K) / Chi2_H;
        MSET(ret, r, PS_Z, z);
        fflush(stdout);
    }

    return ret;

}