-
Notifications
You must be signed in to change notification settings - Fork 83
New sample calculation with proof #986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
18b6318
New sample calculation with proof
rajeee 5e7d1f6
Python version of sampling
rajeee f12e75a
small bugfix in run_sampling.py
rajeee eaf8342
Restrict door area for small buildings
rajeee 291a433
Modify TSV properly
rajeee 177d309
Correct newline error
rajeee 00d6126
Remove Vintage dep
rajeee a7277f3
Latest results.
224b4b2
Merge branch 'develop' into faster_sampling
rajeee faa2396
Latest results.
8cfe5a0
Cleanup and remove python version
rajeee 63e0f3e
Merge branch 'develop' into faster_sampling
rajeee 6f43e12
Latest results.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -219,8 +219,10 @@ def _sample_probability_distribution(prob_dist, num_samples) | |
| end | ||
|
|
||
| return_samples = {} | ||
| return_samples_v2 = {} | ||
| prob_dist.each do |item| | ||
| return_samples[item[0]] = 0 | ||
| return_samples_v2[item[0]] = 0 | ||
| end | ||
|
|
||
| # Sort array in descending order | ||
|
|
@@ -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) | ||
|
|
@@ -268,14 +281,33 @@ def _sample_probability_distribution(prob_dist, num_samples) | |
| end | ||
| end | ||
|
|
||
| if not (return_samples == return_samples_v2) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.