Exemplo n.º 1
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;

}
Exemplo n.º 2
0
/**
 * Estimates the FAP by Monte Carlo bootstrapping of the original data. "trials" bootstrapped data sets are
 * generated using the random number generator "rng"; for each data set, the routine ok_periodogram_ls estimates
 * z_max, which is collected into an array and returned into "zmax". Bootstrapped datasets are built by selecting
 * with replacement from the input dataset, keeping times of observation fixed. 
 * @param data Input matrix containing the data; each row containing (t_i, x_i, sigma_i)
 * @param trials Number of bootstrap trials
 * @param samples Number of frequencies sampled
 * @param Pmin Minimum period sampled
 * @param Pmax Maximum period sampled
 * @param method Method used to compute periodogram (ignored)
 * @param timecol Time column (e.g. 0) in the matrix data
 * @param valcol Value column (e.g. 1) in the matrix data
 * @param sigmacol Sigma column (e.g. 2) in the matrix data
 * @param rng A pre-allocated random number generator
 * @param p If specified, returns additional info for the periodogram and reuses matrices to save space/speed. If you pass
 * a value different than NULL, you are responsible for deallocating the workspace and its fields. p->zm returns a sorted
 * vector of the maximum powers in each synthetic trial.
 * @param prog An ok_progress* callback; if different from NULL, can be used to stop or report progress.
 * @return A matrix containing: {PS_TIME, PS_Z, PS_FAP, PS_Z_LS} (period, power, bootstrapped FAP, unnormalized
 * LS power). You are responsible for deallocating it.

 */
gsl_matrix* ok_periodogram_boot(const gsl_matrix* data, const unsigned int trials, const unsigned int samples,
                                const double Pmin, const double Pmax, const int method,
                                const unsigned int timecol, const unsigned int valcol, const unsigned int sigcol,
                                const unsigned long int seed, ok_periodogram_workspace* p, ok_progress prog) {


    int nthreads = omp_get_max_threads();

    ok_periodogram_workspace * w[nthreads];
    gsl_matrix * mock[nthreads];
    gsl_rng * rng[nthreads];

    rng[0] = gsl_rng_alloc(gsl_rng_default);
    gsl_rng_set(rng[0], seed);

    for (int i = 0; i < nthreads; i++) {
        w[i] = (ok_periodogram_workspace*) malloc(sizeof (ok_periodogram_workspace));
        w[i]->per = NULL;
        w[i]->buf = NULL;
        w[i]->calc_z_fap = false;
        mock[i] = ok_matrix_copy(data);
        if (i > 0) {
            rng[i] = gsl_rng_alloc(gsl_rng_default);
            gsl_rng_set(rng[i], seed + i);
        }
    }

    gsl_matrix* ret = ok_matrix_copy(ok_periodogram_ls(data, samples, Pmin, Pmax, method, timecol, valcol, sigcol, NULL));

    gsl_vector* zmax = (p != NULL && p->zm != NULL ? p->zm : gsl_vector_alloc(trials));

    bool abort = false;
    #pragma omp parallel for
    for (int i = 0; i < trials; i++) {
        if (!abort) {
            int nt = omp_get_thread_num();

            ok_bootstrap_matrix_mean(data, T_TIME, T_VAL, mock[nt], rng[nt]);
            ok_periodogram_ls(mock[nt], samples, Pmin, Pmax, method, timecol, valcol, sigcol, w[nt]);
            zmax->data[i] = w[nt]->zmax;

            if (nt == 0 && prog != NULL) {
                int ret = prog(i * nthreads, trials, NULL,
                               "ok_periodogram_boot");
                if (ret == PROGRESS_STOP) {
                    abort = true;
                    #pragma omp flush (abort)
                }
            }
        }
    }

    gsl_sort(zmax->data, 1, trials);

    for (int i = 0; i < ret->size1; i++) {
        if (MGET(ret, i, PS_Z) > zmax->data[trials - 1])
            MSET(ret, i, PS_FAP, 1. / (double) trials);
        else if (MGET(ret, i, PS_Z) < zmax->data[0])
            MSET(ret, i, PS_FAP, 1.);
        else {
            int idx = ok_bsearch(zmax->data, MGET(ret, i, PS_Z), trials);
            MSET(ret, i, PS_FAP, (double) (trials - idx) / (double) trials);
        }
    }



    for (int i = 0; i < nthreads; i++) {
        gsl_matrix_free(w[i]->buf);
        gsl_matrix_free(w[i]->per);
        free(w[i]);
        gsl_matrix_free(mock[i]);
        gsl_rng_free(rng[i]);
    }

    if (p != NULL) {
        if (p->zm != NULL)
            gsl_vector_free(zmax);
    }
    return ret;
}
Exemplo n.º 3
0
double K_getPeriodogramAt(ok_kernel* k, int row, int col) {

    static int length;
    static int samples = 15000;
    static double Pmin = 1.;
    static double Pmax = 20000.;
    static ok_periodogram_workspace* p = NULL;
    static gsl_matrix* ps = NULL;
    static const int top_freqs = 10;
    static double* top = NULL;
    static double tolerance[1] = {1e-3};

    if (p == NULL) {
        p = (ok_periodogram_workspace*) malloc(sizeof (ok_periodogram_workspace));
        p->buf = NULL;
        p->per = NULL;
        p->calc_z_fap = true;
    }
    if (row == JS_PS_GET_TOP_PERIODS) {
        return top[col];
    } else if (row == JS_PS_GET_TOP_POWERS) {
        return top[col + top_freqs];
    } else if (row == JS_PS_GET_TOP_FAPS) {
        return top[col + 2 * top_freqs];
    } else if (row == JS_PS_SET_PMIN) {
        Pmin = (double) col;
        return 0;
    } else if (row == JS_PS_SET_PMAX) {
        Pmax = (double) col;
        return 0;
    } else if (row == JS_PS_SETUP) {
        if (ps != NULL) {
            gsl_matrix_free(ps);
            ps = NULL;
        }
        if (top == NULL)
            top = (double*) malloc(top_freqs * 3 * sizeof (double));
        gsl_matrix* data = K_getCompiledDataMatrix(k);
        for (int i = 0; i < MROWS(data); i++)
            MSET(data, i, T_SVAL, MGET(data, i, T_SVAL) - MGET(data, i, T_PRED));

        gsl_matrix* ret = ok_periodogram_ls(data, samples, Pmin, Pmax,
                                            0, T_TIME, T_SVAL, T_ERR, p);

        ps = ok_resample_curve(ret, 0, 1, 0.1, 800,
                               100, tolerance, 0, true);
        length = MROWS(ps);

        ok_sort_matrix(ret, PS_Z);
        double dt = 0.5;
        int idx = MROWS(ret);
        int i = 0;
        while (idx > 0 && i < top_freqs) {
            idx--;
            bool skip = false;
            for (int n = 0; n < i; n++)
                if (fabs(top[n] - MGET(ret, idx, PS_TIME)) < dt)
                    skip = true;

            if (!skip) {
                top[i] = MGET(ret, idx, PS_TIME);
                top[i + top_freqs] = MGET(ret, idx, PS_Z);
                top[i + 2 * top_freqs] = MGET(ret, idx, PS_FAP);
                i++;
            }
        }
        gsl_matrix_free(data);

        return (double) length;
    } else if (row == JS_PS_GET_FAPS_LEVELS) {
        if (p == NULL || ps == NULL)
            return 0;
        if (col == 1)
            return p->z_fap_1;
        else if (col == 2)
            return p->z_fap_2;
        else if (col == 3)
            return p->z_fap_3;
        else
            return 0.;
    } else {
        if (ps == NULL)
            return 0;
        return MGET(ps, row, col);
    }
}