Skip to content

Commit

Permalink
Implement split_chains such that it has the same behavior as Stan for…
Browse files Browse the repository at this point in the history
… odd numbered draws
  • Loading branch information
WardBrian committed Jun 14, 2024
1 parent 7e319ff commit 2dd1875
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions gui/src/app/SamplerOutputView/stan_stats/stan_stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,24 +240,20 @@ const split_chains = (draws: number[][]) => {
num_draws = Math.min(num_draws, draws[chain].length);
}

const half = num_draws / 2;
const half_draws = Math.ceil(half);
const half = num_draws / 2.0;
// when N is odd, the (N+1)/2th draw is ignored
const end_first_half = Math.floor(half);
const start_second_half = Math.ceil(half);
const split_draws = new Array(2 * num_chains);
for (let n = 0; n < num_chains; ++n) {
split_draws[2 * n] = draws[n].slice(0, half_draws);
split_draws[2 * n + 1] = draws[n].slice(half_draws);
split_draws[2 * n] = draws[n].slice(0, end_first_half);
split_draws[2 * n + 1] = draws[n].slice(start_second_half);
}

return split_draws;
}

export const compute_split_effective_sample_size = (draws: number[][]) => {
const num_chains = draws.length;
let num_draws = draws[0].length;
for (let chain = 1; chain < num_chains; ++chain) {
num_draws = Math.min(num_draws, draws[chain].length);
}

const split_draws = split_chains(draws);

return compute_effective_sample_size(split_draws);
Expand Down Expand Up @@ -302,6 +298,7 @@ export function compute_potential_scale_reduction(draws: number[][]): number {
const chain_mean = new Array(num_chains).fill(0);
// chain_var: sample variance of each chain
const chain_var = new Array(num_chains).fill(0);

for (let chain = 0; chain < num_chains; ++chain) {
const draw = draws[chain];
chain_mean[chain] = compute_mean(draw);
Expand All @@ -315,13 +312,7 @@ export function compute_potential_scale_reduction(draws: number[][]): number {
}

export function compute_split_potential_scale_reduction(draws: number[][]): number {
const num_chains = draws.length;
let num_draws = draws[0].length;
for (let chain = 1; chain < num_chains; ++chain) {
num_draws = Math.min(num_draws, draws[chain].length);
}

const split_draws = split_chains(draws);

return compute_potential_scale_reduction(split_draws);
}
}

0 comments on commit 2dd1875

Please sign in to comment.