Exemplo n.º 1
0
void gen_rand_state_synth(double *const x, unsigned int N, gsl_rng *r, TMCMCParams &params) {
	assert(N == 1 + params.N_DM + 4*params.N_stars);
	
	// R_V
	x[0] = 3.1 + gsl_ran_gaussian_ziggurat(r, 0.2);
	
	// Delta_EBV
	for(size_t i=0; i<params.N_DM; i++) { x[i+1] = params.data->EBV / (double)params.N_DM * gsl_ran_chisq(r, 1.); }
	
	// Stars
	TSED sed_tmp(true);
	for(size_t i = 1 + params.N_DM; i < 1 + params.N_DM + 4*params.N_stars; i += 4) {
		// DM
		x[i] = 5. + 13. * gsl_rng_uniform(r);
		
		// Stellar type
		double logMass, logtau, FeH, tau;
		bool in_lib = false;
		while(!in_lib) {
			logMass = gsl_ran_gaussian_ziggurat(r, 0.5);
			tau = -1.;
			while(tau <= 0.) {
				tau = 1.e9 * (5. + gsl_ran_gaussian_ziggurat(r, 2.));
			}
			logtau = log10(tau);
			FeH = -1.0 + gsl_ran_gaussian_ziggurat(r, 1.);
			
			in_lib = params.synth_stellar_model->get_sed(logMass, logtau, FeH, sed_tmp);
		}
		x[i+1] = logMass;
		x[i+2] = logtau;
		x[i+3] = FeH;
	}
}
Exemplo n.º 2
0
std::vector<double> Random::normal3D()
{
	std::vector<double> randomArray = {0.,0.,0.};
	randomArray[0] = gsl_ran_gaussian_ziggurat(this->randomGeneratorHandle,1.0);
	randomArray[1] = gsl_ran_gaussian_ziggurat(this->randomGeneratorHandle,1.0);
	randomArray[2] = gsl_ran_gaussian_ziggurat(this->randomGeneratorHandle,1.0);
	return randomArray;
}
Exemplo n.º 3
0
int DPMHC_K(struct str_DPMHC *ptr_DPMHC_data)
{
    int i_K = ptr_DPMHC_data->i_K;
    gsl_vector *v_u = ptr_DPMHC_data->v_u;
    gsl_vector *v_v = ptr_DPMHC_data->v_v;
    gsl_vector *v_w  = ptr_DPMHC_data->v_w;
    gsl_matrix *m_DPtheta = ptr_DPMHC_data->m_DPtheta;
    double d_DPalpha = ptr_DPMHC_data->d_DPalpha;

    int K_tmp, K_new,j;
    double a,v_j,w_j,csum,min_u;
    //gsl_vector_view theta_j;

  //int k_asset_number = P -> size1; /* number of assets in model */

    K_tmp = i_K;
    min_u = gsl_vector_min ( v_u );
    a = 1.0 - min_u;

    if( a == 1.0 )
        printf("**********min_u = %g *************\n",min_u);

    csum = 0.0;
    j=0;

    while ( csum <= a ){

        /* check if new v_j,w_j and theta_j should be generated */
        if( j >= K_tmp ){

            v_j = gsl_ran_beta ( rng , 1.0, d_DPalpha );
            vset( v_v, j, v_j);

            w_j = v_j * (vget( v_w, j-1 )/vget(v_v,j-1))*(1.0-vget(v_v,j-1));
            vset( v_w, j, w_j);

        /* generate new mu, xi, tau from prior G_0 */
            mset(m_DPtheta, j, 0,
                ptr_DPMHC_data->d_m0 + gsl_ran_gaussian_ziggurat(rng, sqrt(ptr_DPMHC_data->d_s2m)));

            mset(m_DPtheta, j, 1,
                gsl_ran_gaussian_ziggurat(rng, ptr_DPMHC_data->d_A));

            mset(m_DPtheta, j, 2,
                gsl_ran_gamma(rng, 0.5, 0.5) );
        }

        csum += vget(v_w,j);
        K_new = j + 1;
        j++;
    }

    ptr_DPMHC_data->i_K = K_new;

    return 0;
}
Exemplo n.º 4
0
void
gslu_rand_gaussian_matrix (const gsl_rng *r, gsl_matrix *A, 
                           const gsl_vector *mu, const gsl_matrix *Sigma, const gsl_matrix *L)
{
    assert (A->size1 == mu->size && (Sigma || L));
    for (size_t i=0; i<A->size1; i++)
        for (size_t j=0; j<A->size2; j++)
            gsl_matrix_set (A, i, j, gsl_ran_gaussian_ziggurat (r, 1.0));
    
    if (L) {
        assert (L->size1 == L->size2 && L->size1 == mu->size);
        gsl_blas_dtrmm (CblasLeft, CblasLower, CblasNoTrans, CblasNonUnit, 1.0, L, A);
    }
    else {
        assert (Sigma->size1 == Sigma->size2 && Sigma->size1 == mu->size);
        gsl_matrix *_L = gsl_matrix_alloc (Sigma->size1, Sigma->size2);
        gsl_matrix_memcpy (_L, Sigma);
        gsl_linalg_cholesky_decomp (_L);
        gsl_blas_dtrmm (CblasLeft, CblasLower, CblasNoTrans, CblasNonUnit, 1.0, _L, A);
        gsl_matrix_free (_L);
    }

    for (size_t j=0; j<A->size2; j++) {
        gsl_vector_view a = gsl_matrix_column (A, j);
        gsl_vector_add (&a.vector, mu);
    }
}
Exemplo n.º 5
0
void gen_rand_state_indiv_emp(double *const x, unsigned int N, gsl_rng *r, TMCMCParams &params) {
	if(params.vary_RV) { assert(N == 5); } else { assert(N == 4); }
	
	// Stars
	TSED sed_tmp(true);
	
	// E(B-V)
	//x[0] = 3. * gsl_rng_uniform(r);
	x[0] = 1.5 * params.data->EBV * gsl_rng_uniform(r);
	
	// DM
	x[1] = 6. + 12. * gsl_rng_uniform(r);
	
	// Stellar type
	double Mr, FeH;
	Mr = -0.5 + 15.5 * gsl_rng_uniform(r);
	FeH = -2.45 + 2.4 * gsl_rng_uniform(r);
	
	x[2] = Mr;
	x[3] = FeH;
	
	if(params.vary_RV) {
		double RV = -1.;
		while((RV <= 2.1) || (RV >= 5.)) {
			RV = params.RV_mean + gsl_ran_gaussian_ziggurat(r, 1.5*params.RV_variance*params.RV_variance);
		}
		x[4] = RV;
	}
}
Exemplo n.º 6
0
// Draw a normal varariate from a covariance matrix. The square-root of the covariance (as defined in sqrt_matrix) must be provided.
void draw_from_cov(double* x, const gsl_matrix* sqrt_cov, unsigned int N, gsl_rng* r) {
	double tmp;
	for(unsigned int i=0; i<N; i++) { x[i] = 0.; }
	for(unsigned int j=0; j<N; j++) {
		tmp = gsl_ran_gaussian_ziggurat(r, 1.);
		for(unsigned int i=0; i<N; i++) { x[i] += gsl_matrix_get(sqrt_cov, i, j) * tmp; }
	}
}
Exemplo n.º 7
0
double randomGauss(double min=0, double max=1, double sigma=C_SIGMA, double miu=C_MIU) {
    while (true) {
        double new_random = C_MIU+gsl_ran_gaussian_ziggurat(r, C_SIGMA);
        if (min <= new_random && new_random <= max) {
            return new_random;
        }
    }
}
Exemplo n.º 8
0
int DPMHC_mu_smplr(struct str_DPMHC *ptr_DPMHC_data)
{
    int i,j;
    int i_n = ptr_DPMHC_data->v_y->size;
    int i_K = ptr_DPMHC_data->i_K;
    int i_nj;
    double d_muj,d_s2j,d_yhatj;

    double d_xij,d_tauj;

    double d_m0 = ptr_DPMHC_data->d_m0;
    double d_s2m = ptr_DPMHC_data->d_s2m;

   gsl_vector *v_y = ptr_DPMHC_data->v_y;
   gsl_vector_int *vi_S = ptr_DPMHC_data->vi_S;
   gsl_vector_int *vi_n = ptr_DPMHC_data->vi_n;
   gsl_matrix *m_theta = ptr_DPMHC_data->m_DPtheta;


 //  printf("\ni_K = %d\n",i_K);

   for(j=0;j<i_K;j++){

        d_yhatj = 0.;
        i_nj = 0;
        for(i=0;i<i_n;i++){
            if( vget_int(vi_S,i) == j ){
                d_yhatj += vget(v_y,i);
                i_nj++;
            }
        }
        if (vget_int(vi_n,j) != i_nj){
            fprintf(stderr,"Error in  DPMN_theta_polya_smplr(): vi_n[%d] does not equal i_nj\n", j);
            exit(1);
        }
        d_xij = mget(m_theta,j,1);
        d_tauj = mget(m_theta,j,2);
        d_yhatj /= (d_xij * d_xij / d_tauj);

        d_s2j = 1./( 1./d_s2m + i_nj/ (d_xij * d_xij / d_tauj) );
        d_muj = d_s2j * ( d_m0/d_s2m + d_yhatj );


        d_muj = d_muj + gsl_ran_gaussian_ziggurat(rng, sqrt(d_s2j));

        mset(m_theta,j,0, d_muj);


       // printf("%d: eta = %g lambda^2 = %g\n",j, mget(m_theta,j,0), mget(m_theta,j,1) );
     }





    return 0;
}
/*
  ORDER: 
  [0] Dxx, [1] Dxy, [2] Dyy, [3] Dxz, [4] Dyz, [5] Dzz
*/
int RicianNoiseDWIs( float **dwi,
                     int N,
                     int Ngrad,
                     THD_3dim_dataset *D,
                     float NOISE_DWI,    
                     float NOISE_B0,
                     MRI_IMAGE *g,
                     byte *M,
                     float S0,
                     float bval,
                     gsl_rng *r)
{
   int i,j,k;
   float *grad;
   double sig;
   double sval;
   double riced;

   grad = MRI_FLOAT_PTR(g);

   for( k=0 ; k<N ; k++) 
      if(M[k]) {
         sval = 1 + gsl_ran_gaussian_ziggurat(r,1.0) * NOISE_B0;
         riced = gsl_ran_gaussian_ziggurat(r,1.0) * NOISE_B0;
         dwi[0][k] = S0 * sqrt(sval*sval + riced*riced);

         for( i=0 ; i<Ngrad ; i++) {
            sig = 0;
            sig+= THD_get_voxel(D,k,0)*grad[3*i]*grad[3*i];
            sig+= THD_get_voxel(D,k,2)*grad[3*i+1]*grad[3*i+1];
            sig+= THD_get_voxel(D,k,5)*grad[3*i+2]*grad[3*i+2];
            sig+= 2*THD_get_voxel(D,k,1)*grad[3*i]*grad[3*i+1];
            sig+= 2*THD_get_voxel(D,k,3)*grad[3*i]*grad[3*i+2];
            sig+= 2*THD_get_voxel(D,k,4)*grad[3*i+1]*grad[3*i+2];

            sval = exp(-bval*sig);
            sval+= gsl_ran_gaussian_ziggurat(r,1.0) * NOISE_DWI;
            riced = gsl_ran_gaussian_ziggurat(r,1.0) * NOISE_DWI;
            dwi[i+1][k] = S0 * sqrt(sval*sval + riced*riced);
         }
      }
   RETURN (1);
}
Exemplo n.º 10
0
/* Step function */ 
void S1(const gsl_rng * r, void *xp, double step_size)
{
	block *x = ((block *) xp);
	int i = (int) round(gsl_rng_uniform(r) * (x->vec->size-1));
	gsl_vector_set(x->vec,i,
		//GSL_MAX(0, 
		gsl_vector_get(x->vec,i) + 	gsl_ran_gaussian_ziggurat(r,step_size)
		//)
	); 
}
Exemplo n.º 11
0
void UpdateWeinerProcessGSL(parameterStruct *param, double beadDW[][DIM], const gsl_rng *r){

	int i=0;
	int N = param->N;
	var = sqrt(param->dt);

	for(i=2;i<N;i++){
		beadDW[i][0] = gsl_ran_gaussian_ziggurat(r,var);
		beadDW[i][1] = gsl_ran_gaussian_ziggurat(r,var);
		beadDW[i][2] = gsl_ran_gaussian_ziggurat(r,var);
	}

	beadDW[0][0] = 0.; 
	beadDW[0][1] = 0.;
	beadDW[0][2] = 0.;
	beadDW[1][0] = 0.; 
	beadDW[1][1] = 0.;
	beadDW[1][2] = 0.;
}
Exemplo n.º 12
0
int DPMHC_init(struct str_DPMHC *ptr_DPMHC_data, int i_draws){

    int j;
    int i_T = (ptr_DPMHC_data->vi_S)->size;
    if (i_T == 0){
        fprintf(stderr,"Error in DPMHC_init(): DPMHC_alloc() has not been called.\n");
        exit(1);
    }

    int i_K;
    gsl_matrix *m_DPtheta = ptr_DPMHC_data->m_DPtheta;

    ptr_DPMHC_data->m_DPmcmc = gsl_matrix_alloc(i_draws, 2); // for draw of i_K and d_DPalpha

    // initialize slice truction to K = 4 and one alive cluster, i_m = 1
    i_K = ptr_DPMHC_data->i_K = 4;
    ptr_DPMHC_data->i_m = 1;
    gsl_vector_int_set_all(ptr_DPMHC_data->vi_S,0);
    vset_int(ptr_DPMHC_data->vi_n,0,i_T);

    // draw DP precision parameter d_DPalpha ~ Gamma(a,b)
    double d_DPalpha;
    d_DPalpha = ran_gamma(rng, ptr_DPMHC_data->d_a, ptr_DPMHC_data->d_b);
    ptr_DPMHC_data->d_DPalpha = d_DPalpha;

    // Draw initial mixture locations for K clusters
    for(j = 0; j < i_K; j++){

        mset(m_DPtheta, j, 0,
                ptr_DPMHC_data->d_m0 + gsl_ran_gaussian_ziggurat(rng, sqrt(ptr_DPMHC_data->d_s2m)));

        mset(m_DPtheta, j, 1,
                gsl_ran_gaussian_ziggurat(rng, ptr_DPMHC_data->d_A));

        mset(m_DPtheta, j, 2,
                gsl_ran_gamma(rng, 0.5, 0.5) );

    }

    return 0;
}
Exemplo n.º 13
0
 //! \brief Update some property of the offspring based on
 //! properties of the parents
 virtual void
 update(const gsl_rng *r, diploid_t &offspring, const diploid_t &,
        const diploid_t &, const gcont_t &gametes,
        const mcont_t &mutations,
        const single_region_fitness_fxn &ff) noexcept
 {
     offspring.g = ff(offspring, gametes, mutations);
     offspring.e = gsl_ran_gaussian_ziggurat(r, sigE);
     double dev = (offspring.g + offspring.e - optimum);
     offspring.w = std::exp(-(dev * dev) / (2. * VS));
     assert(std::isfinite(offspring.w));
     return;
 }
Exemplo n.º 14
0
 void
 update(const gsl_rng *r, diploid_t &offspring, const diploid_t &,
        const diploid_t &, const gcont_t &gametes,
        const mcont_t &mutations,
        const multi_locus_fitness_fxn &genetic_value_fxn) const
 {
     offspring[0].g
         = genetic_value_fxn(offspring, gametes, mutations);
     offspring[0].e = gsl_ran_gaussian_ziggurat(r, sigE);
     double dev = (offspring[0].g + offspring[0].e - optimum);
     offspring[0].w = std::exp(-(dev * dev) / (2. * VS));
     assert(std::isfinite(offspring[0].w));
 }
Exemplo n.º 15
0
int LogisticMap_gene_seq(LogisticMap *self)
{
  int i;
  double eta;
  for (i = 1; i < self->num_i; ++i){
    eta = gsl_ran_gaussian_ziggurat(self->rng, self->sigma);
    self->xt[i] = self->mu * self->xt[i-1] * (1 - self->xt[i-1]) + eta;
    if (self->xt[i] < 0){
      self->xt[i] = 0;
    }
  }
  return 0;
}
Exemplo n.º 16
0
void gen_rand_state_indiv_synth(double *const x, unsigned int N, gsl_rng *r, TMCMCParams &params) {
	assert(N == 5);
	
	// Stars
	TSED sed_tmp(true);
	
	// E(B-V)
	x[0] = 1.5 * params.data->EBV * gsl_rng_uniform(r);
	
	// DM
	x[1] = 5. + 13. * gsl_rng_uniform(r);
	
	// Stellar type
	double logMass, logtau, FeH, tau;
	bool in_lib = false;
	while(!in_lib) {
		logMass = gsl_ran_gaussian_ziggurat(r, 0.5);
		//tau = -1.;
		//while(tau <= 0.) {
		//	tau = 1.e9 * (5. + gsl_ran_gaussian_ziggurat(r, 2.));
		//}
		//logtau = log10(tau);
		logtau = 8. + gsl_ran_gaussian_ziggurat(r, 1.);
		FeH = -1.0 + gsl_ran_gaussian_ziggurat(r, 1.);
		
		in_lib = params.synth_stellar_model->get_sed(logMass, logtau, FeH, sed_tmp);
	}
	x[2] = logMass;
	x[3] = logtau;
	x[4] = FeH;
	
	if(params.vary_RV) {
		double RV = -1.;
		while((RV <= 2.1) || (RV >= 5.)) {
			RV = params.RV_mean + gsl_ran_gaussian_ziggurat(r, 1.5*params.RV_variance*params.RV_variance);
		}
		x[5] = RV;
	}
}
Exemplo n.º 17
0
void TGaussianMixture::draw(double* x) {
	double tmp = gsl_rng_uniform(r);
	double sum = 0.;
	for(unsigned int k=0; k<nclusters; k++) {
		sum += w[k];
		if(sum >= tmp) {
			for(unsigned int i=0; i<ndim; i++) { x[i] = mu[k*ndim + i]; }
			for(unsigned int j=0; j<ndim; j++) {
				tmp = gsl_ran_gaussian_ziggurat(r, 1.);
				for(unsigned int i=0; i<ndim; i++) { x[i] += gsl_matrix_get(sqrt_cov[k], i, j) * tmp; }
			}
			break;
		}
	}
}
Exemplo n.º 18
0
static double rhodens_unvisited (const gsl_rng *r, void *dens_data) {
  // Pointer to the structure: d 
  struct dens_par *d;
  d=dens_data;
  // Indicating the rank of the parameter of interest
  int i=d->pos_rho; //
  // Draw directly in the posterior distribution
  int nNeighbors=d->nNeigh[i];
  double sumNeighbors=0.0;
  for (int m=0;m<nNeighbors;m++) {
    sumNeighbors+=d->rho_run[d->Neigh[i][m]];
  }
  double meanNeighbors=sumNeighbors/nNeighbors;
  double sample=meanNeighbors+gsl_ran_gaussian_ziggurat(r,sqrt(d->Vrho_run/nNeighbors)); 
  return sample;
}
Exemplo n.º 19
0
void gen_rand_los_extinction_from_guess(double *const logEBV, unsigned int N, gsl_rng *r, TLOSMCMCParams &params) {
	assert(params.EBV_prof_guess.size() == N);
	double EBV_ceil = params.img_stack->rect->max[1];
	double EBV_sum = 0.;
	for(size_t i=0; i<N; i++) {
		logEBV[i] = params.EBV_prof_guess[i] + gsl_ran_gaussian_ziggurat(r, 1.);
		EBV_sum += logEBV[i];
	}
	
	// Ensure that reddening is not more than allowed
	if(EBV_sum >= 0.95 * EBV_ceil) {
		double factor = log(0.95 * EBV_ceil / EBV_sum);
		for(size_t i=0; i<N; i++) {
			logEBV[i] += factor;
		}
	}
}
Exemplo n.º 20
0
void gen_rand_los_extinction(double *const logEBV, unsigned int N, gsl_rng *r, TLOSMCMCParams &params) {
	double EBV_ceil = params.img_stack->rect->max[1] / params.subpixel_max;
	double mu = log(1.5 * params.EBV_guess_max / params.subpixel_max / (double)N);
	double EBV_sum = 0.;
	for(size_t i=0; i<N; i++) {
		logEBV[i] = mu + gsl_ran_gaussian_ziggurat(r, 0.5);
		EBV_sum += exp(logEBV[i]);
	}
	
	// Ensure that reddening is not more than allowed
	if(EBV_sum >= 0.95 * EBV_ceil) {
		double factor = log(0.95 * EBV_ceil / EBV_sum);
		for(size_t i=0; i<N; i++) {
			logEBV[i] += factor;
		}
	}
}
Exemplo n.º 21
0
void event_and_rates(gsl_rng * rng, vector<pop> &poplist, double sum, vector<CRow> &cmatrix, par_list * pars)
{
	vector<pop>::iterator p;
	double threshhold = sum*gsl_rng_uniform(rng);
	double cumulative = 0;
	int id = 0; 
	char type='b';
	int who=0;
	for(p=poplist.begin(); p != poplist.end(); p++){
		cumulative += p->birth;
		if( cumulative  > threshhold ){ 
			if(gsl_rng_uniform(rng) > pars->mu){
				++p->popsize;
				type = 'b';
				who = id;

			} else {
				double y = p->trait + gsl_ran_gaussian_ziggurat(rng,pars->sigma_mu);
				double c = init_compete(poplist, y, pars);
				pop pop_i = {1, y, 1., 1., p->trait, c};
				poplist.push_back(pop_i);

				grow_C(poplist, cmatrix, pars);
				type = 'b';
				who = -1;

			}
			break;
		}
		cumulative += p->death;
		if( cumulative  > threshhold ){ 
			--p->popsize;
			type = 'd';
			who = id;
			if( p->popsize == 0 ){
				poplist.erase( p );

				shrink_C(cmatrix, id); // destroy row and column of cmatrix
			}
			break;
		}
		id++;
	}
	fast_update_rates(poplist, cmatrix, who, type, pars);
}
Exemplo n.º 22
0
void _simulateSDE(const double * abgth, const int N_spikes, const double dt,
					double * Ss) {
	const double V_THRESH = 1.0;

	double alpha, beta, gamma, theta;
	alpha = abgth[0]; beta= abgth[1]; gamma =abgth[2]; theta = abgth[3];

	int recorded_spikes = 0;

	double v = .0;
	double sqrt_dt = sqrt(dt);

	//RNG stuff:
    const gsl_rng_type * T;
    gsl_rng * G;
    /* create a generator chosen by the environment variable GSL_RNG_TYPE */
    gsl_rng_env_setup();
    T = gsl_rng_default;
    G = gsl_rng_alloc (T);

    struct timeval tv;
    gettimeofday(&tv,NULL);
    srand((tv.tv_sec * 1000) + (tv.tv_usec / 1000));
    unsigned long int S = rand();
//    printf("S=%d\n",S);
    gsl_rng_set (G, S);

    double dB, dv;
    double t = .0;
	while (recorded_spikes < N_spikes){
				dB =  gsl_ran_gaussian_ziggurat(G, sqrt_dt);
	            dv = ( alpha - v + gamma*sin(theta*t) )*dt  + beta*dB;

	            v += dv;
	            t += dt;
	            if (v >= V_THRESH){
	                Ss [recorded_spikes++] = t;
	                v = .0;
	            }
	}

	gsl_rng_free(G);
}
Exemplo n.º 23
0
void gen_rand_los_extinction_clouds(double *const x, unsigned int N, gsl_rng *r, TLOSMCMCParams &params) {
	double mu_floor = params.img_stack->rect->min[0];
	double mu_ceil = params.img_stack->rect->max[0];
	double EBV_ceil = params.img_stack->rect->max[1] / params.subpixel_max;
	unsigned int N_clouds = N / 2;
	
	double logEBV_mean = log(1.5 * params.EBV_guess_max / params.subpixel_max / (double)N_clouds);
	double mu_mean = (mu_ceil - mu_floor) / N_clouds;
	double EBV_sum = 0.;
	double mu_sum = mu_floor;
	
	double *Delta_mu = x;
	double *logDelta_EBV = x + N_clouds;
	
	for(size_t i=0; i<N_clouds; i++) {
		logDelta_EBV[i] = logEBV_mean + gsl_ran_gaussian_ziggurat(r, 0.5);
		EBV_sum += exp(logDelta_EBV[i]);
		
		Delta_mu[i] = mu_mean * gsl_rng_uniform(r);
		mu_sum += Delta_mu[i];
	}
	Delta_mu[0] += mu_floor;
	
	// Ensure that reddening is not more than allowed
	if(EBV_sum >= 0.95 * EBV_ceil) {
		double factor = log(0.95 * EBV_ceil / EBV_sum);
		for(size_t i=0; i<N_clouds; i++) {
			logDelta_EBV[i] += factor;
		}
	}
	
	// Ensure that distance to farthest cloud is not more than allowed
	if(mu_sum >= 0.95 * mu_ceil) {
		double factor = 0.95 * mu_ceil / mu_sum;
		for(size_t i=0; i<N_clouds; i++) {
			Delta_mu[i] *= factor;
		}
	}
}
Exemplo n.º 24
0
/*===========================RANDOM SCALAR/VECTOR/MATRIX=========================*/
void
gslu_rand_gaussian_vector (const gsl_rng *r, gsl_vector *a, 
                           const gsl_vector *mu, const gsl_matrix *Sigma, const gsl_matrix *L)
{
    assert (a->size == mu->size && (Sigma || L));
    for (size_t i=0; i<a->size; i++)
        gsl_vector_set (a, i, gsl_ran_gaussian_ziggurat (r, 1.0));

    if (L) {
        assert (L->size1 == L->size2 && L->size1 == mu->size);
        gsl_blas_dtrmv (CblasLower, CblasNoTrans, CblasNonUnit, L, a);
    } else {
        assert (Sigma->size1 == Sigma->size2 && Sigma->size1 == mu->size);
        gsl_matrix *_L = gsl_matrix_alloc (Sigma->size1, Sigma->size2);
        gsl_matrix_memcpy (_L, Sigma);
        gsl_linalg_cholesky_decomp (_L);
        gsl_blas_dtrmv (CblasLower, CblasNoTrans, CblasNonUnit, _L, a);
        gsl_matrix_free (_L);
    }

    gsl_vector_add (a, mu);
}
Exemplo n.º 25
0
double
gsl_ran_gamma_mt (const gsl_rng * r, const double a, const double b)
{
  /* assume a > 0 */

  if (a < 1)
    {
      double u = gsl_rng_uniform_pos (r);
      return gsl_ran_gamma_mt (r, 1.0 + a, b) * pow (u, 1.0 / a);
    }

  {
    double x, v, u;
    double d = a - 1.0 / 3.0;
    double c = (1.0 / 3.0) / sqrt (d);

    while (1)
      {
        do
          {
            x = gsl_ran_gaussian_ziggurat (r, 1.0);
            v = 1.0 + c * x;
          }
        while (v <= 0);

        v = v * v * v;
        u = gsl_rng_uniform_pos (r);

        if (u < 1 - 0.0331 * x * x * x * x) 
          break;

        if (log (u) < 0.5 * x * x + d * (1 - v + log (v)))
          break;
      }
    
    return b * d * v;
  }
}
Exemplo n.º 26
0
/* s2 is regression model innovation variance */
double sample_gibbs_mean(gsl_vector *y, gsl_vector *x,
			 double m, double v2, double s2)
{
  size_t i,n=y->size;
  double xt,yt,x2,xy,var_beta,mean_beta;

/*   x2=0.0; */
/*   xy=0.0; */
/*   for(i=0;i<n;i++){ */
/*     xt=vget(x,i); */
/*     yt=vget(y,i); */
/*     x2 += xt*xt; */
/*     xy += xt*yt; */
/*   } */
  gsl_blas_ddot(y,x,&xy);
  gsl_blas_ddot(x,x,&x2);


  var_beta = s2*v2 / (v2*x2 + s2);
  mean_beta = var_beta * ( xy/s2 + m/v2 );

  return mean_beta + gsl_ran_gaussian_ziggurat(rng,sqrt(var_beta));
}
Exemplo n.º 27
0
void gauss_xdat(Search_settings *sett, double amplitude, double sigma, int i){

  gsl_rng * r;
  int j;
  unsigned long mySeed;
  mySeed = random_seed();

  const gsl_rng_type * T;
  
  gsl_rng_env_setup();

  T = gsl_rng_default;
  r = gsl_rng_alloc (T);
  gsl_rng_set(r, mySeed);
  // Generate normal distribution (around 0, 
  // with amplitude and sigma as parameters)
  for(j = 0; j < sett->N; j++) 
    ifo[i].sig.xDat[j] = amplitude*gsl_ran_gaussian_ziggurat(r, sigma);
 
  gsl_rng_free(r); 


}
Exemplo n.º 28
0
int main(int argc, char **argv) { 

  int i, N; 
  double *x, amp, sigma; 
  unsigned long mySeed;
  mySeed = random_seed();

  N = atoi(argv[1]); 
  amp = atof(argv[2]); 
  sigma = atof(argv[3]);  
 
  x = (double *)calloc(N, sizeof(double));   

	FILE *dataout;  
  dataout = fopen(argv[4], "wb"); 

  const gsl_rng_type * T;
  
  gsl_rng_env_setup();

  T = gsl_rng_default;
  r = gsl_rng_alloc (T);
  gsl_rng_set(r, mySeed);
  // Generate normal distribution (around 0, 
  // with amplitude amp and sigma)
  for(i=0; i<N; i++) 
    x[i] = amp*gsl_ran_gaussian_ziggurat(r, sigma);
 
  gsl_rng_free(r); 

  fwrite(x, sizeof(*x), N, dataout);  
  fclose(dataout); 
  free(x);
    
	return 0; 

}
Exemplo n.º 29
0
double Random::normal()
{
	return gsl_ran_gaussian_ziggurat(this->randomGeneratorHandle, 1.0);
}
Exemplo n.º 30
0
void integrator(function F, int D, void *params, double x[], double dxdt[], double x0[], double t[], int iters, double s_noise, double abstol, double reltol, unsigned long int noise_seed) {

    /*printf("Integration starts...\n");*/
    
    /*printf("D=%d\n",D);*/
	/*Temporary variables*/
    double *y = (double*) malloc( sizeof(double)*D ); /*state variable at time t-1 (input) and then at time t(output)*/
    /*printf("y allocated\n");*/
    double *dydt = (double*) malloc( sizeof(double)*D ); /*rate of change at time point t*/
    /*double *dydt_in = (double*) malloc( sizeof(double)*D ); /*rate of change at time point t-1*/
    /*printf("dydt_in allocated\n");*/
    /*double *dydt_out= (double*) malloc( sizeof(double)*D ); /*rate of change at time point t*/
    /*printf("dydt_out allocated\n");*/
    /*double *yerr = (double*) malloc( sizeof(double)*D );/*error*/
    /*printf("yerr allocated\n");*/
    double t0,tf,tc,dt=(t[1]-t[0])/10,noise;/*initial time point, final time point, current time point, current time step, noise*/
    /*printf("dt=%f\n",dt);*/
    int j,ii;/*State variable and iteration indexes*/
    int status;/*integrator success flag*/
    
    /*Prepare noise generator*/
    const gsl_rng_type *Q;
    gsl_rng *r;
    gsl_rng_env_setup();
    Q = gsl_rng_default;
    r = gsl_rng_alloc(Q);
    gsl_rng_set (r, noise_seed);
    /*printf("random number generator set\n");*/
      
       
            
    /*Initialize*/
    /*Calculate dx/dt for x0*/
    /*tc=t[0];
    /*printf("tc=%f\n",tc);*/
    /* initialise dydt_in from system parameters */
    /*GSL_ODEIV_FN_EVAL(&sys, t, y, dydt_in);*/
    /*printf("x0=[ ");
    for (j=0; j<D; j++) printf("%f ", x0[j]);
    /*printf("]\n");*/
    /*GSL_ODEIV_FN_EVAL(&sys, tc, x0, dydt_in);
    /*printf("dydt_in[t0]=[ ");*/
    /*for (j=0; j<D; j++) printf(" %f",dydt_in[j]);*/
    /*printf(" ]\n");*/
    /*printf("y[t0]=[ ");*/
    for (j=0;j<D;j++) {
        y[j] = x[j] = x0[j];
        /*printf(" %f",y[j]);*/
    }
    /*printf(" ]\n");*/
            
    /*Integration*/
    for (ii=1; ii<iters; ii++) {
       
        /*Call the integrator*/
        /*int gsl_odeiv_step_apply(gsl_odeiv_step * s, double t, double h, double y[], double yerr[], const double dydt_in[], double dydt_out[], const gsl_odeiv_system * dydt)*/        
        t0=t[ii-1];
        tf=t[ii];
        tc=t0;
        
        while (tc<tf) {
            
            /*Constraint time step h such as that tc+h<=tf*/
            /*if (tc+dt>tf) dt=tf-tc;
            
            /*Advance a h time step*/
            /*status=gsl_odeiv_step_apply(s, tc, dt, y, yerr, dydt_in, dydt_out, &sys);
            if (status != GSL_SUCCESS) break;
            
            /*Modify time sep*/
            /*gsl_odeiv_control_hadjust(c,s,y,yerr,dydt_in,&dt);*/

            status=System(tc, y, dydt, params); 
            
            /*Increase current time*/
            tc += dt;

            for (j=0;j<D;j++) {
             
                noise=gsl_ran_gaussian_ziggurat(r, s_noise);
            
                //dydt[j]+=noise;
                y[j] += dt*dydt[j] + sqrt(dt)*noise;      

            }
        
        }
        
        /*Unpack and store result for this time point adding some gaussian noise with sigma s_noise*/
        /*if (status != GSL_SUCCESS) break;*/
        
        for (j=0;j<D;j++) { 
            
            x[ii*D+j] = y[j];
            dxdt[(ii-1)*D+j] = dydt[j];

        }
 
    }
    /*Get dxdt for the last time point*/
    status=System(t[ii], y, dydt, params);
    for (j=0;j<D;j++) dxdt[(iters-1)*D+j] = dydt[j];
        
    /*gsl_odeiv_control_free(c);
    /*printf("c freed\n");*/
    /*gsl_odeiv_step_free(s);
    /*printf("s freed\n");*/
    gsl_rng_free(r);
    /*printf("rng freed\n");*/
    /*free(yerr);
    /*printf("yerr freed\n");*/
    /*free(dydt_out);
    /*printf("dydt_out freed\n");*/
    free(dydt);
    /*free(dydt_in);
    /*printf("dydt_in freed\n");*/
    free(y);
    /*printf("y freed\n");*/

}