Skip to content
This repository was archived by the owner on Aug 13, 2020. It is now read-only.

Experiment with differential privacy #1290

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/lib/differential_privacy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module DifferentialPrivacy
def self.flip(value, substitute)
return value if rand(2) == 0 # first flip
rand(2) == 0 ? value : substitute # second flip
end
end
18 changes: 18 additions & 0 deletions test/lib/differential_privacy_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require "test_helper"

class DifferentialPrivacyTest < ActiveSupport::TestCase
test "basic flip" do
value = 1
substitutes = (1..10).to_a
results = 1000.times.each_with_object({}) { |_, memo|
result = DifferentialPrivacy.flip(value, substitutes.sample)
memo[result] ||= 0
memo[result] += 1
}
hit_percentage = results[value] / 1000.to_f
puts results.inspect
puts "True Value: #{hit_percentage * 100}%"
puts "Fake Value: #{(1 - hit_percentage) * 100}%"
assert hit_percentage.between?(0.65, 0.85)
end
end