Package: pystatdp
PyStatDPThis is a fork ofcmla-psu/statdp Statistical Counterexample Detector for Differential Privacycreated to explore the possiblity of integrating it into the CI workfollow of projects with differentially private elements.UsageWe assume your algorithm implementation has the folllowing signature:(queries, epsilon, ...)(list of queries, privacy budget and extra arguments).Then you can simply call the detection tool with automatic database generation and event selection:frompystatdpimportpystatdppystatdp=pystatdp()#Currently, only mechanisms with the class and call structure of PyDP[https://github.com/openmined/PyDP] are supported.# All mechanisms of PyDP are supported.defyour_algorithm(queries,epsilon,...):# your algorithm implementation hereif__name__=='__main__':# algorithm privacy budget argument(`epsilon`) is needed# otherwise detector won't work properly since it will try to generate a privacy budgetresult=pystatdp.main(your_algorithm,(param_for_algorithm,[param_for_algorithm]):tuple,(epsilon,[epsilon]):tuple)The result is returned in variableresult, which is stored as[(epsilon, p, d1, d2, kwargs, event), (...)].Thedetect_counterexampleaccepts multiple extra arguments to customize the process, check the signature and notes ofdetect_counterexamplemethod to see how to use.defdetect_counterexample(algorithm,test_epsilon,default_kwargs=None,databases=None,num_input=(5,10),event_iterations=100000,detect_iterations=500000,cores=None,sensitivity=ALL_DIFFER,quiet=False,loglevel=logging.INFO):""":param algorithm: The algorithm to test for.:param test_epsilon: The privacy budget to test for, can either be a number or a tuple/list.:param default_kwargs: The default arguments the algorithm needs except the first Queries argument.:param databases: The databases to run for detection, optional.:param num_input: The length of input to generate, not used if database param is specified.:param event_iterations: The iterations for event selector to run.:param detect_iterations: The iterations for detector to run.:param cores: The number of max processes to set for multiprocessing.Pool(), os.cpu_count() is used if None.:param sensitivity: The sensitivity setting, all queries can differ by one or just one query can differ by one.:param quiet: Do not print progress bar or messages, logs are not affected.:param loglevel: The loglevel for logging package.:return: [(epsilon, p, d1, d2, kwargs, event)] The epsilon-p pairs along with databases/arguments/selected event."""InstallFor the best performance we recommend installingpystatdpin acondavirtual environment (orvenvif you prefer, the setup is similar):# we use python 3.8, but 3.6 and above should work finecondacreate-npystatdpanacondapython=3.8
condaactivatepystatdp# install dependencies from conda for best performancecondainstallnumpynumbamatplotlibsympytqdmjsonpicklepip# install icc_rt compiler for best performance with numba, this requires using intel's channelcondainstall-cintelicc_rt# install the remaining non-conda dependencies and pystatdppipinstall.Then you can runexamples/benchmark.pyto run the experiments we conducted.Visualizing the resultsA nice python librarymatplotlibis recommended for visualizing your result.There's a python code snippet within classpystatdp(plot_resultmethod) to show an example of plotting the results.Then you can generate a figure like the BoundedMean method of PyDP. (see at:https://github.com/OpenMined/PyStatDP/blob/master/examples/generic_method.pdf)Customizing the detectionOur tool is designed to be modular and components are fully decoupled. You can write your owninput generator/event selectorand apply them tohypothesis test.In general the detection process istest_epsilon --> generate_databases --((d1, d2, kwargs), ...), epsilon--> select_event --(d1, d2, kwargs, event), epsilon--> hypothesis_test --> (d1, d2, kwargs, event, p-value), epsilonYou can checkout the definition and docstrings of the functions respectively to define your own generator/selector. Basically thedetect_counterexamplefunction inpystatdp.coremodule is just shortcut function to take care of the above process for you.test_statisticsfunction inhypotestmodule can be used universally by all algorithms (this function is to calculate p-value based on the observed statistics). However, you may need to design your own generator or selector for your own algorithm, since our input generator and event selector are designed to work with numerical queries on databases.Citing this workYou are encouraged to cite the orginal StatDppaperif you use this tool for academic research:@inproceedings{ding2018detecting,title={Detecting Violations of Differential Privacy},author={Ding, Zeyu and Wang, Yuxin and Wang, Guanhong and Zhang, Danfeng and Kifer, Daniel},booktitle={Proceedings of the 2018 ACM SIGSAC Conference on Computer and Communications Security},pages={475--489},year={2018},organization={ACM}}LicenseMIT.