<commit_before>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
#include <stdio.h>
#include <cmath>
#include <cblas.h>
#include <random>
#include <lapacke.h>
const double PI = 3.141592653589793238463;
const double SQRT_1_2PI = 1/sqrt(2*PI);
const double SQRT_1_2 =  1/sqrt(2);


double pdf(double z){
    //if (-0.5*pow(z,2)<-704.8){printf(" pdf underflow");}
    return SQRT_1_2PI*exp(-0.5*pow(z,2));
}
double lpdf(double z){
    //if (-0.5*pow(z,2)<-704.8){printf(" pdf underflow");}
    return log(SQRT_1_2PI) - 0.5*pow(z,2);
}

double cdf(double z){
    //if (-SQRT_1_2*z>26.55){printf(" cdf underflow");}
    return 0.5 * erfc(-SQRT_1_2*z);
}

extern "C" int EI(double* m, double* s, double y, int N, double* R){
    double S;
    double c;
    double p;
    for (int i=0; i<N; i++){
        S = (y-m[i])/s[i];
        c = cdf(S);
        p = pdf(S);
        R[i] = (y-m[i])*c+s[i]*p;
        //printf("\nm: %f s: %f y: %f S: %f cdf: %f pdf: %f",m[i],s[i],y,S,cdf(S),pdf(S));
        //TODO fix EI for values that make pdf and cdf zero
        //if (R[i]==0.){printf("_ei=0 %f %f %f||%f %f %f_[ %f %f %f]\n",y,m[i],s[i],S,pdf(S),cdf(S),log(s[i]),lpdf(S),exp(log(s[i])+lpdf(S)));}
    }
    return 0;
}

extern "C" int lEI(double* m, double* s, double y, int N, double* R){
    double S;
    double c;
    double p;
    for (int i=0; i<N; i++){
        EI(m,s,y,1,&R[i]);
        //TODO this switch to log puts a kink in the curve
        if (R[i]<=0.){
            
            S = (y-m[i])/s[i];
            R[i] = log(s[i])+lpdf(S);
            //printf("logversion: %f\n",R[i]);
        }
        else{
            //printf("regversion: %f %f\n",R[i], log(R[i]));
            R[i] = log(R[i]);
        }
    }
    return 0;
}

//make m draws from a multivariate Gaussian of size n where the cholesky deom of the covariance is the lower triangular matrix K
//matrix mult means the draws are down the columns of R
extern "C" int drawcov(double* K, int n, double* R, int m){
    std::random_device rand_dev;          // Use random_device to get a random seed.
    std::mt19937 rand_engine(rand_dev()); //seed merseene twister with random number
    std::normal_distribution<double> nmdist(0., 1.); //normal unit dist
    for (int i=0;i<n*m;i++){
        R[i] = nmdist(rand_engine);
        
    }
    cblas_dtrmm(CblasRowMajor,CblasLeft,CblasLower,CblasNoTrans,CblasNonUnit,n,m,1.,K,n,R,m);
    return 0;
    
}

//as drawcov but for the original covariance matrix. K is overwritten.
extern "C" int drawk(double* K, int n, double* R, int m){
    int j = -9;
    printf("adding 10e%d diagonal to covariance in drawk\n",j);
    for (int i=0; i<n; i++){
        K[i*n+i]+=pow(10,j);
    }
    LAPACKE_dpotrf(LAPACK_ROW_MAJOR,'L',n,&K[0],n);
    drawcov(&K[0],n, &R[0], m);
    return 0;
}<commit_msg>step up conditioning noise with retrys<commit_after>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
#include <stdio.h>
#include <cmath>
#include <cblas.h>
#include <random>
#include <lapacke.h>
const double PI = 3.141592653589793238463;
const double SQRT_1_2PI = 1/sqrt(2*PI);
const double SQRT_1_2 =  1/sqrt(2);


double pdf(double z){
    //if (-0.5*pow(z,2)<-704.8){printf(" pdf underflow");}
    return SQRT_1_2PI*exp(-0.5*pow(z,2));
}
double lpdf(double z){
    //if (-0.5*pow(z,2)<-704.8){printf(" pdf underflow");}
    return log(SQRT_1_2PI) - 0.5*pow(z,2);
}

double cdf(double z){
    //if (-SQRT_1_2*z>26.55){printf(" cdf underflow");}
    return 0.5 * erfc(-SQRT_1_2*z);
}

extern "C" int EI(double* m, double* s, double y, int N, double* R){
    double S;
    double c;
    double p;
    for (int i=0; i<N; i++){
        S = (y-m[i])/s[i];
        c = cdf(S);
        p = pdf(S);
        R[i] = (y-m[i])*c+s[i]*p;
        //printf("\nm: %f s: %f y: %f S: %f cdf: %f pdf: %f",m[i],s[i],y,S,cdf(S),pdf(S));
        //TODO fix EI for values that make pdf and cdf zero
        //if (R[i]==0.){printf("_ei=0 %f %f %f||%f %f %f_[ %f %f %f]\n",y,m[i],s[i],S,pdf(S),cdf(S),log(s[i]),lpdf(S),exp(log(s[i])+lpdf(S)));}
    }
    return 0;
}

extern "C" int lEI(double* m, double* s, double y, int N, double* R){
    double S;
    double c;
    double p;
    for (int i=0; i<N; i++){
        EI(m,s,y,1,&R[i]);
        //TODO this switch to log puts a kink in the curve
        if (R[i]<=0.){
            
            S = (y-m[i])/s[i];
            R[i] = log(s[i])+lpdf(S);
            //printf("logversion: %f\n",R[i]);
        }
        else{
            //printf("regversion: %f %f\n",R[i], log(R[i]));
            R[i] = log(R[i]);
        }
    }
    return 0;
}

//make m draws from a multivariate Gaussian of size n where the cholesky deom of the covariance is the lower triangular matrix K
//matrix mult means the draws are down the columns of R
extern "C" int drawcov(double* K, int n, double* R, int m){
    std::random_device rand_dev;          // Use random_device to get a random seed.
    std::mt19937 rand_engine(rand_dev()); //seed merseene twister with random number
    std::normal_distribution<double> nmdist(0., 1.); //normal unit dist
    for (int i=0;i<n*m;i++){
        R[i] = nmdist(rand_engine);
        
    }
    cblas_dtrmm(CblasRowMajor,CblasLeft,CblasLower,CblasNoTrans,CblasNonUnit,n,m,1.,K,n,R,m);
    return 0;
    
}

//as drawcov but for the original covariance matrix. K is overwritten.
extern "C" int drawk(double* K_in, int n, double* R, int m){
    int j = -19;
    int info = 1;
    std::vector<double>K = std::vector<double>(n*n);
    while (info != 0){
        printf("adding 10e%d diagonal to covariance in drawk\n",j);
        for (int i=0; i<n; i++){
            for (int j=0; j<n; j++){
                K[i*n+j]=K_in[i*n+j];
            }
            K[i*n+i]+=pow(10,j);
        }

        info = LAPACKE_dpotrf(LAPACK_ROW_MAJOR,'L',n,&K[0],n);
        printf("info%d\n",info);
        j+=1;
    }
    drawcov(&K[0],n, &R[0], m);
    return 0;
}
{'fraction_non_alphanumeric': 0.1885232614686335, 'fraction_numerical': 0.03063705624898687, 'mean_word_length': 2.6617210682492582, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2938946', 'n_tokens_mistral': 2655, 'n_tokens_neox': 2532, 'n_words': 658}