void Sinusoid::generate(RNG& rng) { A = exp(0.1*rng.randn()); log10_period = -rng.rand(); phi = 2.0 * M_PI * rng.rand(); calculate_mu(); for(size_t i=0; i<N; ++i) y[i] = mu[i] + sigma*rng.randn(); calculate_logl(); }
void Sinusoid::generate(RNG& rng) { A = -log(1.0 - rng.rand()); log10_period = log10(1E-3 * t_range) + 3.0 * rng.rand(); phi = 2.0 * M_PI * rng.rand(); for(double& nn : n) nn = rng.randn(); assemble(); }
double Sinusoid::perturb(RNG& rng) { double logH = 0.0; if(rng.rand() < 0.5) { // Perturb one of the three parameters int which = rng.rand_int(3); if(which == 0) { A = 1.0 - exp(-A); A += rng.randh(); wrap(A, 0.0, 1.0); A = -log(1.0 - A); } else if(which == 1) { log10_period += 3.0 * rng.randh(); wrap(log10_period, log10(1E-3 * t_range), log10(t_range)); } else { phi += 2 * M_PI * rng.randh(); wrap(phi, 0.0, 2 * M_PI); } } else { // Perturb some of the ns. if(rng.rand() <= 0.5) { // Just change one int which = rng.rand_int(N); logH -= -0.5*pow(n[which], 2); n[which] += rng.randh(); logH += -0.5*pow(n[which], 2); } else { // Potentially regenerate many int reps = pow(N, rng.rand()); for(int i=0; i<reps; ++i) n[rng.rand_int(N)] = rng.randn(); } } assemble(); return logH; }
double Sinusoid::perturb(RNG& rng) { double logH = 0.0; int proposal_type = 1;//rng.rand_int(4); if(proposal_type == 0) { // Perturb parameters, changing data along with it std::vector<double> mu_old = mu; logH += perturb_parameters(rng); calculate_mu(); double n; for(size_t i=0; i<N; ++i) { n = (y[i] - mu_old[i]) / sigma; y[i] = mu[i] + sigma * n; } calculate_logl(); } else if(proposal_type == 1) { // Perturb parameters, keeping data constant // (aka Metropolis step of the posterior!) logH -= logl; logH += perturb_parameters(rng); calculate_mu(); calculate_logl(); logH += logl; } else if(proposal_type == 2) { // Just change one datum int which = rng.rand_int(N); logH -= -0.5*pow((y[which] - mu[which])/sigma, 2); y[which] += sigma * rng.randh(); logH += -0.5*pow((y[which] - mu[which])/sigma, 2); calculate_logl(); } else { // Potentially regenerate many of the data points int reps = pow(N, rng.rand()); int which; for(int i=0; i<reps; ++i) { which = rng.rand_int(N); y[which] = mu[which] + sigma * rng.randn(); } calculate_logl(); } return logH; }