Commit 62958a64 authored by Reinhard Munz's avatar Reinhard Munz

added PINQueryable patch

parent 9df55ca1
diff --git a/Users/munz/Downloads/PINQ/PINQ/PINQ/PINQueryable.cs b/Users/munz/Desktop/PINQueryable.cs
old mode 100755
new mode 100644
index 87d4ea7..a1d4293
--- a/Users/munz/Downloads/PINQ/PINQ/PINQ/PINQueryable.cs
+++ b/Users/munz/Desktop/PINQueryable.cs
@@ -23,6 +23,8 @@ namespace PINQ
/// <param name="epsilon">Additive increment to differential privacy parameter. Both positive and negative values are possible. Can be both positive and negative.</param>
/// <returns>True if the change is accepted, and false if not. It is assumed that negative changes will be accepted, and return values may not always be tested in that case.</returns>
public virtual bool apply(double epsilon) { return true; }
+
+ public virtual double remaining() { return Double.PositiveInfinity; }
};
#region Useful PINQAgents
@@ -47,6 +49,12 @@ namespace PINQ
budget -= epsilon;
return true;
}
+
+ public override double remaining()
+ {
+ return budget;
+ }
+
/// <summary>
/// Constructor for a PINQAgentBudget
/// </summary>
@@ -75,6 +83,11 @@ namespace PINQ
return target.apply(epsilon * scale);
}
+ public override double remaining()
+ {
+ return target.remaining();
+ }
+
/// <summary>
/// PINQAgentUnary Constructor
/// </summary>
@@ -112,6 +125,12 @@ namespace PINQ
return false;
}
+ public override double remaining()
+ {
+ if (targetA.remaining() < targetB.remaining()) return targetA.remaining();
+ return targetB.remaining();
+ }
+
/// <summary>
/// PINQAgentBinary constructor
/// </summary>
@@ -171,6 +190,11 @@ namespace PINQ
return true;
}
+ public override double remaining()
+ {
+ return target.remaining();
+ }
+
/// <summary>
/// Constructor for PINQAgentPartition
/// </summary>
@@ -213,7 +237,18 @@ namespace PINQ
/// <summary>
/// Random number generator for all randomized query responses. Consider strengthening as appopriate.
/// </summary>
- protected static System.Random random = new System.Random();
+ internal static System.Random random = new System.Random();
+
+ public static void SetRandom(System.Random rand)
+ {
+ if (rand == null) throw new ArgumentNullException(nameof(rand));
+ random = rand;
+ }
+
+ public double remaining()
+ {
+ return agent.remaining();
+ }
#region Helpful Methods
// private helper methods that add noise, and merge lists, respectively.
@@ -270,13 +305,14 @@ namespace PINQ
.Select(x => x > +1.0 ? +1.0 : x)
.Select(x => x < -1.0 ? -1.0 : x);
- double tally = values.Sum(); // we'll want this, maybe multiple times.
double count = values.Count(); // we'll want this, maybe multiple times.
// if the count is zero, we need to select uniformly at random from [-1,+1].
if (count == 0)
return Uniform(-1.0, +1.0);
+ double tally = values.Sum(); // we'll want this, maybe multiple times.
+
double candidate = (tally + Laplace(2.0 / epsilon)) / count;
while (candidate < -1.0 || candidate > 1.0)
candidate = (tally + Laplace(2.0 / epsilon)) / count;
@@ -337,14 +373,14 @@ namespace PINQ
IQueryable<double> sorted = values.OrderBy(x => x);
// we now write a custom aggregator that reservoir samples from the sorted list
- var result = sorted.Aggregate(new { previous = 0.0, counter = 0.0, sample = 0.0, tally = 0.0 },
- (state, value) => new
- {
- previous = value,
- counter = state.counter + 1.0,
- sample = (random.NextDouble() > state.tally / (state.tally + (value - state.previous) * Math.Exp(-epsilon * Math.Abs(target - state.counter)))) ? (value - state.previous) * random.NextDouble() + state.previous : state.sample,
- tally = state.tally + (value - state.previous) * Math.Exp(-epsilon * Math.Abs(target - state.counter))
- });
+ var result = sorted.ToList().Aggregate(new { previous = 0.0, counter = 0.0, sample = 0.0, tally = 0.0 },
+ (state, value) => new
+ {
+ previous = value,
+ counter = state.counter + 1.0,
+ sample = (random.NextDouble() > state.tally / (state.tally + (value - state.previous) * Math.Exp(-epsilon * Math.Abs(target - state.counter)))) ? (value - state.previous) * random.NextDouble() + state.previous : state.sample,
+ tally = state.tally + (value - state.previous) * Math.Exp(-epsilon * Math.Abs(target - state.counter))
+ });
if (random.NextDouble() > result.tally / (result.tally + (1.0 - result.previous) * Math.Exp(-epsilon * Math.Abs(target - result.counter))))
return (1.0 - result.previous) * random.NextDouble() + result.previous;
@@ -1116,4 +1152,4 @@ namespace PINQ
#endregion
};
-}
+}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment