_binary From e1b3e44f0b54a9524c7fd3e87c7d439310d83fc1 Mon Sep 17 00:00:00 2001
From: Pragya Rana <pragya.rana9@gmail.com>
Date: Mon, 21 Nov 2016 12:06:44 -0800
Subject: [PATCH] Initial patch for Del 4: Adding differential privacy

* Add differential privacy to number of views of each group's threads.
---
 src/configs/Config.php                         |  2 ++
 src/controllers/Controller.php                 | 33 ++++++++++++++++++++++++++
 src/controllers/components/SocialComponent.php |  2 ++
 3 files changed, 37 insertions(+)

diff --git a/src/configs/Config.php b/src/configs/Config.php
index dbdb4d8..65b5899 100755
--- a/src/configs/Config.php
+++ b/src/configs/Config.php
@@ -225,6 +225,8 @@ nsdefine('ONE_HOUR', 3600);
 nsdefine('ONE_MINUTE', 60);
 /** Number of seconds in a second */
 nsdefine('ONE_SECOND', 1);
+/** Value of epsolon */
+nsdefine('EPSOLON', 0.01);
 if (file_exists(BASE_DIR."/configs/LocalConfig.php")) {
     /** Include any locally specified defines (could use as an alternative
         way to set work directory) */
diff --git a/src/controllers/Controller.php b/src/controllers/Controller.php
index 9b8fd28..c10b05f 100755
--- a/src/controllers/Controller.php
+++ b/src/controllers/Controller.php
@@ -924,4 +924,37 @@ abstract class Controller
             }
         }
      }
+  /**
+     * Fuzzifies the data to achieve differential privacy
+     *
+     * @param int actual value
+     * @return int fuzzified value
+     */
+    public function addDifferentialPrivacy($actual_value, $level_privacy = 1)
+    {
+
+        $sigma = 1 / C\EPSOLON;
+        $max_value = (2 * $actual_value) + 1;
+        /* Calculation by Integration
+         * f: exp(-1 * ((abs($actual_value - $x)) / $sigma))
+         * Since function consists of absolute value, break the integration into two to remove absolute from the function.
+         * First integral runs from 0 through $actual_value, and second integral runs from $actual_value through $max_value.
+         * Then after using substition rule, first and second integral range change, say [a,b] and [b,c]
+         */
+        $a = -1 * $actual_value;
+        $b = 0;
+        $c = -1 * ($max_value - $actual_value);
+        $integral_value = $sigma * (2 - exp($a / $sigma) - exp($c / $sigma));
+        $random = rand(0, $integral_value);
+        $p = 0;
+        for ($i = 0; $i < $max_value; $i++) {
+            $arg = -1 * (($actual_value - $i) / $sigma);
+            $p +=  exp($arg);
+            if ($p > $random) {
+                break;
+            }
+        }
+        $fuzzy_value = $i;
+        return $fuzzy_value;
+    }
 }
\ No newline at end of file
diff --git a/src/controllers/components/SocialComponent.php b/src/controllers/components/SocialComponent.php
index 471043a..4154d51 100644
--- a/src/controllers/components/SocialComponent.php
+++ b/src/controllers/components/SocialComponent.php
@@ -1525,6 +1525,8 @@ class SocialComponent extends Component implements CrawlConstants
             $user_id);
         foreach ($group_items as $item) {
             $page = $item;
+            // fuzzify the number of views to add privacy
+            $page['NUM_VIEWS'] = $parent->addDifferentialPrivacy($page['NUM_VIEWS']);
             $page['USER_ICON'] = $user_model->getUserIconUrl($page['USER_ID']);
             $page[self::TITLE] = $page['TITLE'];
             unset($page['TITLE']);
-- 
2.8.4 (Apple Git-73)

