Skip to content
36 changes: 34 additions & 2 deletions resources/run_sampling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,10 @@ def _sample_probability_distribution(prob_dist, num_samples)
end

return_samples = {}
return_samples_v2 = {}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New version.
Once the new version is accepted to be okay, we can remove the old version. Keeping both for now to make it possible to compare.

prob_dist.each do |item|
return_samples[item[0]] = 0
return_samples_v2[item[0]] = 0
end

# Sort array in descending order
Expand All @@ -239,6 +241,17 @@ def _sample_probability_distribution(prob_dist, num_samples)
end

sum = prob_dist.transpose[1].inject(0, :+)

# New style of calculating sampling distribution
sample_dist = prob_dist.map{|v| [v[0], (v[1]*num_samples/sum)]}
assigned_samples = (sample_dist.map{|val| return_samples_v2[val[0]] += val[1].to_i}).inject(0, :+)
remaining_samples = num_samples - assigned_samples
sample_dist_hash = {}
remaining_sample_dist = sample_dist.each_with_index.map{|v, i| [v[1] - v[1].to_i, v[0]]}.sort.reverse
remaining_sample_dist.map{|v| sample_dist_hash[v[1]]=v[0]}

remaining_sample_dist[0, remaining_samples].map{|v| return_samples_v2[v[1]] += 1}

remaining_samples = num_samples
while remaining_samples > 0
# Choose highest probability item (first in array)
Expand Down Expand Up @@ -268,14 +281,33 @@ def _sample_probability_distribution(prob_dist, num_samples)
end
end

if not (return_samples == return_samples_v2)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For most cases, the new version is equal to old version; When they are not, making sure that the difference can be justified.

# If the two sample counts (new and old) aren't equal, make sure:
# 1. there is no more than 1 sample difference for any options and sum of differences (extra and deficit) is zero.
# 2. The options for which there is one sample extra, and corresponding options for which there is one sample
# deficit have exact same probability.
# The reason for this discrepancy is as follow. Suppose, we want three samples between option=Yes and option=No
# If both Yes and No has same probability (0.5), the samples could be [Yes, Yes, No] or [Yes, No, No].
diffs = return_samples_v2.map{ |key, v| [sample_dist_hash[key], key, return_samples[key] - v]}
raise "Sample count diff doesn't sum to zero" unless diffs.transpose[2].inject(0, :+) == 0
raise "There are differences of more than 1 samples" unless diffs.transpose[2].map{|v| v.abs() <= 1}.all?
negative_diffs = diffs.select{|v| v[2]<0}.sort
positive_diffs = diffs.select{|v| v[2]>0}.sort
while negative_diffs.size > 0
negative_diff = negative_diffs.pop()
positive_diff = positive_diffs.pop()
raise "Matching diff with same probability not found" unless (negative_diff[0] - positive_diff[0]).abs < 1e-9
end
end
# Remove items with no samples
return_samples_v2.delete_if { |_k, v| v == 0 }
return_samples.delete_if { |_k, v| v == 0 }

if return_samples.values.reduce(:+) != num_samples
if return_samples_v2.values.reduce(:+) != num_samples
register_error('Sampling algorithm unexpectedly failed.', nil)
end

return return_samples
return return_samples_v2
end

def distribute_samples(random_seed, results_data_param, sample_results, bldgs)
Expand Down