Skip to content

Commit e05a3ff

Browse files
authored
use MY_EXP and MY_LOG everywhere again (#57)
1 parent a71d215 commit e05a3ff

11 files changed

+69
-64
lines changed

src/bgmCompare_logp_and_grad.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "bgmCompare_helper.h"
33
#include "bgmCompare_logp_and_grad.h"
44
#include <cmath>
5+
#include "explog_switch.h"
56

67
using namespace Rcpp;
78

@@ -174,7 +175,7 @@ double log_pseudoposterior(
174175

175176
// ---- priors ----
176177
auto log_beta_prior = [&](double x) {
177-
return x * main_alpha - std::log1p(std::exp(x)) * (main_alpha + main_beta);
178+
return x * main_alpha - std::log1p(MY_EXP(x)) * (main_alpha + main_beta);
178179
};
179180

180181
// Main effects prior
@@ -510,7 +511,7 @@ arma::vec gradient(
510511
for (int c = 0; c < num_cats; ++c) {
511512
off = main_index(base + c, 0);
512513
double value = main_effects(base + c, 0);
513-
const double p = 1.0 / (1.0 + std::exp(-value));
514+
const double p = 1.0 / (1.0 + MY_EXP(-value));
514515
grad(off) += main_alpha - (main_alpha + main_beta) * p;
515516

516517
if (inclusion_indicator(v, v) == 0) continue;
@@ -524,12 +525,12 @@ arma::vec gradient(
524525
} else {
525526
off = main_index(base, 0);
526527
double value = main_effects(base, 0);
527-
double p = 1.0 / (1.0 + std::exp(-value));
528+
double p = 1.0 / (1.0 + MY_EXP(-value));
528529
grad(off) += main_alpha - (main_alpha + main_beta) * p;
529530

530531
off = main_index(base + 1, 0);
531532
value = main_effects(base + 1, 0);
532-
p = 1.0 / (1.0 + std::exp(-value));
533+
p = 1.0 / (1.0 + MY_EXP(-value));
533534
grad(off) += main_alpha - (main_alpha + main_beta) * p;
534535

535536

@@ -733,7 +734,7 @@ double log_pseudoposterior_main_component(
733734
if (h == 0) {
734735
// overall
735736
auto log_beta_prior = [&](double x) {
736-
return x * main_alpha - std::log1p(std::exp(x)) * (main_alpha + main_beta);
737+
return x * main_alpha - std::log1p(MY_EXP(x)) * (main_alpha + main_beta);
737738
};
738739

739740
// Main effects prior

src/bgmCompare_sampler.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "print_mutex.h"
1313
#include "rng_utils.h"
1414
#include "sampler_output.h"
15+
#include "explog_switch.h"
1516
#include <string>
1617

1718
using namespace Rcpp;
@@ -125,7 +126,7 @@ void impute_missing_bgmcompare(
125126
for(int category = 1; category <= num_categories(variable, group); category++) {
126127
exponent = group_main_effects(category - 1);
127128
exponent += category * rest_score;
128-
cumsum += std::exp(exponent);
129+
cumsum += MY_EXP(exponent);
129130
category_response_probabilities[category] = cumsum;
130131
}
131132
} else {
@@ -137,7 +138,7 @@ void impute_missing_bgmcompare(
137138
(category - baseline_category[variable]) *
138139
(category - baseline_category[variable]);
139140
exponent += category * rest_score;
140-
cumsum += std::exp(exponent);
141+
cumsum += MY_EXP(exponent);
141142
category_response_probabilities[category] = cumsum;
142143
}
143144
}
@@ -985,7 +986,7 @@ void tune_proposal_sd_bgmcompare(
985986
SamplerResult result = rwm_sampler(current, prop_sd, log_post, rng);
986987
current = result.state[0];
987988
prop_sd = update_proposal_sd_with_robbins_monro(
988-
prop_sd, std::log(result.accept_prob), rm_weight, target_accept
989+
prop_sd, MY_LOG(result.accept_prob), rm_weight, target_accept
989990
);
990991
}
991992
}
@@ -1013,7 +1014,7 @@ void tune_proposal_sd_bgmcompare(
10131014
SamplerResult result = rwm_sampler(current, prop_sd, log_post, rng);
10141015
current = result.state[0];
10151016
prop_sd = update_proposal_sd_with_robbins_monro(
1016-
prop_sd, std::log(result.accept_prob), rm_weight, target_accept
1017+
prop_sd, MY_LOG(result.accept_prob), rm_weight, target_accept
10171018
);
10181019
}
10191020
}
@@ -1046,7 +1047,7 @@ void tune_proposal_sd_bgmcompare(
10461047
SamplerResult result = rwm_sampler(current, prop_sd, log_post, rng);
10471048
current = result.state[0];
10481049
prop_sd = update_proposal_sd_with_robbins_monro(
1049-
prop_sd, std::log(result.accept_prob), rm_weight, target_accept
1050+
prop_sd, MY_LOG(result.accept_prob), rm_weight, target_accept
10501051
);
10511052
}
10521053
}
@@ -1165,11 +1166,11 @@ void update_indicator_differences_metropolis_bgmcompare (
11651166
// Add prior inclusion probability contribution
11661167
double inc_prob = inclusion_probability_difference(var, var);
11671168
if(proposed_ind == 1) {
1168-
log_accept += std::log(inc_prob);
1169-
log_accept -= std::log(1 - inc_prob);
1169+
log_accept += MY_LOG(inc_prob);
1170+
log_accept -= MY_LOG(1 - inc_prob);
11701171
} else {
1171-
log_accept -= std::log(inc_prob);
1172-
log_accept += std::log(1 - inc_prob);
1172+
log_accept -= MY_LOG(inc_prob);
1173+
log_accept += MY_LOG(1 - inc_prob);
11731174
}
11741175

11751176
// Add parameter prior contribution
@@ -1201,7 +1202,7 @@ void update_indicator_differences_metropolis_bgmcompare (
12011202

12021203
// Perform Metropolis-Hastings step
12031204
double U = runif(rng);
1204-
if(std::log(U) < log_accept) {
1205+
if(MY_LOG(U) < log_accept) {
12051206
inclusion_indicator(var, var) = proposed_ind;
12061207
main_effects.rows(start, stop).cols(1, num_groups - 1) =
12071208
proposed_main_effects.rows(start, stop).cols(1, num_groups - 1);
@@ -1243,11 +1244,11 @@ void update_indicator_differences_metropolis_bgmcompare (
12431244
// Add prior inclusion probability contribution
12441245
double inc_prob = inclusion_probability_difference(var1, var2);
12451246
if(proposed_ind == 1) {
1246-
log_accept += std::log(inc_prob);
1247-
log_accept -= std::log(1 - inc_prob);
1247+
log_accept += MY_LOG(inc_prob);
1248+
log_accept -= MY_LOG(1 - inc_prob);
12481249
} else {
1249-
log_accept -= std::log(inc_prob);
1250-
log_accept += std::log(1 - inc_prob);
1250+
log_accept -= MY_LOG(inc_prob);
1251+
log_accept += MY_LOG(1 - inc_prob);
12511252
}
12521253

12531254
// Add parameter prior contribution
@@ -1280,7 +1281,7 @@ void update_indicator_differences_metropolis_bgmcompare (
12801281

12811282
// Metropolis-Hastings acceptance step
12821283
double U = runif(rng);
1283-
if (std::log(U) < log_accept) {
1284+
if (MY_LOG(U) < log_accept) {
12841285
// Update inclusion inclusion_indicator
12851286
inclusion_indicator(var1, var2) = proposed_ind;
12861287
inclusion_indicator(var2, var1) = proposed_ind;

src/bgm_logp_and_grad.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "bgm_helper.h"
33
#include "bgm_logp_and_grad.h"
44
#include "common_helpers.h"
5+
#include "explog_switch.h"
56

67
using namespace Rcpp;
78

@@ -58,7 +59,7 @@ double log_pseudoposterior_main_effects_component (
5859
double log_posterior = 0.0;
5960

6061
auto log_beta_prior = [&](double main_effect_param) {
61-
return main_effect_param * main_alpha - std::log1p (std::exp (main_effect_param)) * (main_alpha + main_beta);
62+
return main_effect_param * main_alpha - std::log1p (MY_EXP (main_effect_param)) * (main_alpha + main_beta);
6263
};
6364

6465
const int num_cats = num_categories(variable);
@@ -278,7 +279,7 @@ double log_pseudoposterior (
278279

279280
// Calculate the contribution from the data and the prior
280281
auto log_beta_prior = [&](double main_effect_param) {
281-
return main_effect_param * main_alpha - std::log1p (std::exp (main_effect_param)) * (main_alpha + main_beta);
282+
return main_effect_param * main_alpha - std::log1p (MY_EXP (main_effect_param)) * (main_alpha + main_beta);
282283
};
283284

284285
for (int variable = 0; variable < num_variables; variable++) {
@@ -531,14 +532,14 @@ arma::vec gradient_log_pseudoposterior (
531532
if (is_ordinal_variable(variable)) {
532533
const int num_cats = num_categories(variable);
533534
for (int cat = 0; cat < num_cats; cat++) {
534-
const double p = 1.0 / (1.0 + std::exp (-main_effects(variable, cat)));
535+
const double p = 1.0 / (1.0 + MY_EXP (-main_effects(variable, cat)));
535536
gradient(offset + cat) += main_alpha - (main_alpha + main_beta) * p;
536537
}
537538
offset += num_cats;
538539
} else {
539540
for (int i = 0; i < 2; i++) {
540541
const double main_effect_param = main_effects(variable, i);
541-
const double p = 1.0 / (1.0 + std::exp (-main_effect_param));
542+
const double p = 1.0 / (1.0 + MY_EXP (-main_effect_param));
542543
gradient(offset + i) += main_alpha - (main_alpha + main_beta) * p;
543544
}
544545
offset += 2;
@@ -721,8 +722,8 @@ double compute_log_likelihood_ratio_for_variable (
721722
for (int person = 0; person < num_persons; person++) {
722723
const double base = main + score * residual_scores[person] - bounds[person];
723724

724-
const double exp_current = std::exp(base + score * interaction[person] * current_state);
725-
const double exp_proposed = std::exp(base + score * interaction[person] * proposed_state);
725+
const double exp_current = MY_EXP(base + score * interaction[person] * current_state);
726+
const double exp_proposed = MY_EXP(base + score * interaction[person] * proposed_state);
726727

727728
denom_current[person] += exp_current;
728729
denom_proposed[person] += exp_proposed;

src/bgm_sampler.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ void impute_missing_bgm (
9595
for (int cat = 0; cat < num_cats; cat++) {
9696
const int score = cat + 1;
9797
const double exponent = main_effects (variable, cat) + score * residual_score;
98-
cumsum += std::exp (exponent);
98+
cumsum += MY_EXP (exponent);
9999
category_probabilities[score] = cumsum;
100100
}
101101
} else {
102102
// Compute probabilities for Blume-Capel variable
103103
const int ref = baseline_category (variable);
104104

105-
cumsum = std::exp (main_effects (variable, 1) * ref * ref);
105+
cumsum = MY_EXP (main_effects (variable, 1) * ref * ref);
106106
category_probabilities[0] = cumsum;
107107

108108
for (int cat = 0; cat < num_cats; cat++) {
@@ -112,7 +112,7 @@ void impute_missing_bgm (
112112
main_effects (variable, 0) * score +
113113
main_effects (variable, 1) * centered * centered +
114114
score * residual_score;
115-
cumsum += std::exp (exponent);
115+
cumsum += MY_EXP (exponent);
116116
category_probabilities[score] = cumsum;
117117
}
118118
}
@@ -792,7 +792,7 @@ void tune_proposal_sd_bgm(
792792
}
793793

794794
proposal_sd = update_proposal_sd_with_robbins_monro(
795-
proposal_sd, std::log(result.accept_prob), rm_weight, target_accept
795+
proposal_sd, MY_LOG(result.accept_prob), rm_weight, target_accept
796796
);
797797

798798
proposal_sd_pairwise_effects(variable1, variable2) = proposal_sd;
@@ -891,15 +891,15 @@ void update_indicator_edges_metropolis_bgm (
891891
if (proposing_addition) {
892892
log_accept += R::dcauchy(proposed_state, 0.0, pairwise_scale, true);
893893
log_accept -= R::dnorm(proposed_state, current_state, sd, true);
894-
log_accept += std::log (inclusion_probability_ij) - std::log (1.0 - inclusion_probability_ij);
894+
log_accept += MY_LOG (inclusion_probability_ij) - MY_LOG (1.0 - inclusion_probability_ij);
895895
} else {
896896
log_accept -= R::dcauchy(current_state, 0.0, pairwise_scale, true);
897897
log_accept += R::dnorm(current_state, proposed_state, sd, true);
898-
log_accept -= std::log (inclusion_probability_ij) - std::log (1.0 - inclusion_probability_ij);
898+
log_accept -= MY_LOG (inclusion_probability_ij) - MY_LOG (1.0 - inclusion_probability_ij);
899899
}
900900

901901
// Metropolis-Hastings accept step
902-
if (std::log (runif(rng)) < log_accept) {
902+
if (MY_LOG (runif(rng)) < log_accept) {
903903
const int updated_indicator = 1 - indicator(variable1, variable2);
904904
indicator(variable1, variable2) = updated_indicator;
905905
indicator(variable2, variable1) = updated_indicator;

src/mcmc_adaptation.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <vector>
88
#include "mcmc_utils.h"
99
#include "mcmc_rwm.h"
10-
10+
#include "explog_switch.h"
1111

1212
class DualAveraging {
1313
public:
@@ -21,10 +21,10 @@ class DualAveraging {
2121
int t;
2222

2323
DualAveraging(double initial_step_size)
24-
: log_step_size(std::log(initial_step_size)),
25-
log_step_size_avg(std::log(initial_step_size)),
24+
: log_step_size(MY_LOG(initial_step_size)),
25+
log_step_size_avg(MY_LOG(initial_step_size)),
2626
hbar(0.0),
27-
mu(std::log(10.0 * initial_step_size)),
27+
mu(MY_LOG(10.0 * initial_step_size)),
2828
gamma(0.05),
2929
t0(10.0),
3030
kappa(0.75),
@@ -42,15 +42,15 @@ class DualAveraging {
4242
}
4343

4444
void restart(double new_step_size) {
45-
log_step_size = std::log(new_step_size);
46-
log_step_size_avg = std::log(new_step_size);
47-
mu = std::log(10.0 * new_step_size);
45+
log_step_size = MY_LOG(new_step_size);
46+
log_step_size_avg = MY_LOG(new_step_size);
47+
mu = MY_LOG(10.0 * new_step_size);
4848
hbar = 0.0;
4949
t = 1;
5050
}
5151

52-
double current() const { return std::exp(log_step_size); }
53-
double averaged() const { return std::exp(log_step_size_avg); }
52+
double current() const { return MY_EXP(log_step_size); }
53+
double averaged() const { return MY_EXP(log_step_size_avg); }
5454
};
5555

5656

@@ -281,7 +281,7 @@ class RWMAdaptationController {
281281
for (arma::uword i = 0; i < proposal_sd.n_rows; ++i) {
282282
for (arma::uword j = 0; j < proposal_sd.n_cols; ++j) {
283283
if (index_mask(i, j) == 1) {
284-
const double accept_log = std::log(accept_prob_matrix(i, j));
284+
const double accept_log = MY_LOG(accept_prob_matrix(i, j));
285285
proposal_sd(i, j) = update_proposal_sd_with_robbins_monro(
286286
proposal_sd(i, j),
287287
accept_log,

src/mcmc_hmc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ SamplerResult hmc_sampler(
3030
double proposed_H = -log_post(theta) + kinetic_energy(r, inv_mass_diag);
3131
double log_accept_prob = current_H - proposed_H;
3232

33-
arma::vec state = (std::log(runif(rng)) < log_accept_prob) ? theta : init_theta;
33+
arma::vec state = (MY_LOG(runif(rng)) < log_accept_prob) ? theta : init_theta;
3434

35-
double accept_prob = std::min(1.0, std::exp(log_accept_prob));
35+
double accept_prob = std::min(1.0, MY_EXP(log_accept_prob));
3636

3737
return {state, accept_prob};
3838
}

src/mcmc_nuts.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ BuildTreeResult build_tree(
9292
int n_new = 1 * (log_u <= logp - kin);
9393
int s_new = 1 * (log_u <= Delta_max + logp - kin);
9494
bool divergent = (s_new == 0);
95-
double alpha = std::min(1.0, std::exp(logp - kin - logp0 + kin0));
95+
double alpha = std::min(1.0, MY_EXP(logp - kin - logp0 + kin0));
9696

9797
return {
9898
theta_new, r_new, theta_new, r_new, theta_new, n_new, s_new, alpha, 1, divergent

src/mcmc_rwm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ SamplerResult rwm_sampler(
3434
) {
3535
double proposed_state = rnorm(rng, current_state, step_size);
3636
double log_accept = log_post(proposed_state) - log_post(current_state);
37-
double accept_prob = std::min(1.0, std::exp(log_accept));
37+
double accept_prob = std::min(1.0, MY_EXP(log_accept));
3838

3939
double state = (runif(rng) < accept_prob) ? proposed_state : current_state;
4040

src/mcmc_utils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ double heuristic_initial_step_size(
7474
double H0 = logp0 - kin0;
7575
double H1 = logp1 - kin1;
7676

77-
int direction = 2 * (H1 - H0 > std::log(0.5)) - 1; // +1 or -1
77+
int direction = 2 * (H1 - H0 > MY_LOG(0.5)) - 1; // +1 or -1
7878

7979
int attempts = 0;
80-
while (direction * (H1 - H0) > -direction * std::log(2.0) && attempts < max_attempts) {
80+
while (direction * (H1 - H0) > -direction * MY_LOG(2.0) && attempts < max_attempts) {
8181
eps = (direction == 1) ? 2.0 * eps : 0.5 * eps;
8282

8383
// One leapfrog step

src/mcmc_utils.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <cmath>
66
#include <functional>
77
#include <memory>
8+
#include "explog_switch.h"
89
struct SafeRNG;
910

1011
// (only if <algorithm> didn’t already provide it under C++17)
@@ -114,7 +115,7 @@ inline void update_step_size_with_dual_averaging (
114115
arma::vec& state,
115116
const double target_acceptance
116117
) {
117-
const double target_log_step_size = std::log (10.0 * initial_step_size);
118+
const double target_log_step_size = MY_LOG (10.0 * initial_step_size);
118119
constexpr int stabilization_offset = 10;
119120

120121
double& log_step_size = state[0];
@@ -160,9 +161,9 @@ inline void update_step_size_with_robbins_monro (
160161
const double error = acceptance_probability - target_acceptance;
161162
const double decay = std::pow (static_cast<double> (iteration), -decay_rate);
162163

163-
double log_step_size = std::log (step_size);
164+
double log_step_size = MY_LOG (step_size);
164165
log_step_size += error * decay;
165-
step_size = std::exp (log_step_size);
166+
step_size = MY_EXP (log_step_size);
166167
}
167168

168169

@@ -191,7 +192,7 @@ inline double update_proposal_sd_with_robbins_monro (
191192
// Normalize the acceptance probability
192193
double observed_acceptance_probability = 1.0;
193194
if (observed_log_acceptance_probability < 0.0) {
194-
observed_acceptance_probability = std::exp (observed_log_acceptance_probability);
195+
observed_acceptance_probability = MY_EXP (observed_log_acceptance_probability);
195196
}
196197

197198
// Robbins-Monro update step

0 commit comments

Comments
 (0)