{"id":27613743,"url":"https://github.com/akassharjun/shapleyvaluefl","last_synced_at":"2025-04-23T02:16:59.084Z","repository":{"id":57698120,"uuid":"491218415","full_name":"akassharjun/ShapleyValueFL","owner":"akassharjun","description":"A pip library for calculating the Shapley Value for computing the marginal contribution of each client in a Federated Learning environment.","archived":false,"fork":false,"pushed_at":"2023-12-10T06:46:18.000Z","size":17,"stargazers_count":26,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-23T02:16:50.488Z","etag":null,"topics":["federated-learning","game-theory","incentive-mechanism","machine-learning","shapley-value"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/akassharjun.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-05-11T17:54:14.000Z","updated_at":"2024-12-15T06:45:45.000Z","dependencies_parsed_at":"2022-08-25T14:01:40.655Z","dependency_job_id":null,"html_url":"https://github.com/akassharjun/ShapleyValueFL","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akassharjun%2FShapleyValueFL","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akassharjun%2FShapleyValueFL/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akassharjun%2FShapleyValueFL/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akassharjun%2FShapleyValueFL/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/akassharjun","download_url":"https://codeload.github.com/akassharjun/ShapleyValueFL/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250354517,"owners_count":21416752,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["federated-learning","game-theory","incentive-mechanism","machine-learning","shapley-value"],"created_at":"2025-04-23T02:16:58.352Z","updated_at":"2025-04-23T02:16:59.072Z","avatar_url":"https://github.com/akassharjun.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ShapleyValueFL\nA pip library for computing the marginal contribution (Shapley Value) for each client in a Federated Learning environment.\n\n## Table of Content\n- [ShapleyValueFL](#shapleyvaluefl)\n  - [Table of Content](#table-of-content)\n  - [Brief](#brief)\n  - [Usage](#usage)\n  - [Future Work](#future-work)\n  - [Feedback](#feedback)\n\n## Brief\nThe Shapley Value is a game theory concept that explores how to equitably distribute rewards and costs among members of a coalition. It is extensively used in incentive mechanisms for Federated Learning to fairly distribute rewards to clients based on their contribution to the system.\n\nLet $v(S)$ where $S\\subset N$ is defined as the contribution of the model collaboratively trained by the subset $S$. $N$ is a set of all the participants in the system.\nThe i-th participant’s Shapley Value $\\phi(i)$ is defined as\n\n$$\\phi(i) = \\sum_{S\\subset N \\backslash \\{i\\}} \\frac{|S|!(N-|S|-1)!}{|N|!}(v(S\\cup \\{i\\}) - v(S))$$\n\nThe marginal contribution of the $i-th$ participant is defined as\n$(v(S \\cup \\{i\\}) - v(S))$ when they join this coalition.\n\nLet's see this equation in action, consider a Federated Learning environment with three clients, so $N = \\{0, 1, 2\\}$. We list the contribution of each subset within this coalition. Let's consider the contribution to be measured in terms of model accuracy.\n\n\n\u003cdiv align=\"center\"\u003e\n\n$v(\\emptyset) = 0$ \u0026emsp;\u0026emsp; $v(\\{0\\}) = 40$ \u0026emsp;\u0026emsp; $v(\\{1\\}) = 60$ \u0026emsp;\u0026emsp; $v(\\{2\\}) = 80$\n\n$v(\\{0,1\\}) = 70$ \u0026emsp;\u0026emsp; $v(\\{0,2\\}) = 75$ \u0026emsp;\u0026emsp; $v(\\{1,2\\}) = 85$\n\n$v(\\{0,1,2\\}) = 90$\n\n\u003cbr /\u003e\n\n| Subset  | Client #0 | Client #1 | Client #2 |\n| ------------- | ------------- | ------------- | ------------- |\n| $0 \\leftarrow 1 \\leftarrow 2$ | 40  | 30 | 20 |\n| $0 \\leftarrow 2 \\leftarrow 1$ | 40  | 15 | 35 |\n| $1 \\leftarrow 0 \\leftarrow 2$ | 10  | 60 | 20 |\n| $1 \\leftarrow 2 \\leftarrow 0$ | 5  | 60 | 25 |\n| $2 \\leftarrow 0 \\leftarrow 1$ | 0  | 10 | 80 |\n| $2 \\leftarrow 1 \\leftarrow 0$ | 5  | 5 | 80 |\n| $Sum$ | 100  | 180 | 260 |\n| $\\phi(i)$ | 16.67  | 30 | 43.33 |\n\u003c/div\u003e\n\nThe arrow signifies the order in which each client joins the coalition. Consider the\nfirst iteration $0 \\leftarrow 1 \\leftarrow 2$, we calculate the marginal contribution of each client using the\nabove equation. \n\n- Client 0's marginal contribution is given as $v(\\{0\\}) = 40$. \n- Client 1's marginal contribution is given as $v(\\{0, 1\\}) - v(\\{0\\}) = 30$. \n- Client 2's marginal contribution is given as $v(\\{0, 1, 2\\}) - v(\\{0, 1\\}) - v(\\{0\\}) = 20$. \n  \nThe marginal contribution is calculated for each permutation\nlikewise, and the Shapley Value is derived by averaging all of these marginal contributions.\n\n## Usage\n\n```python \nfrom svfl.svfl import calculate_sv\n\nmodels = {\n    \"client-id-1\" : ModelUpdate(),\n    \"client-id-2\" : ModelUpdate(),\n    \"client-id-3\" : ModelUpdate(),\n}\n\ndef evaluate_model(model):\n    # function to compute evaluation metric, ex: accuracy, precision\n    return metric\n\ndef fed_avg(models):\n    # function to merge the model updates into one model for evaluation, ex: FedAvg, FedProx\n    return model\n\n# returns a key value pair with the client identifier and it's respective Shapley Value\ncontribution_measure = calculate_sv(models, evaluate_model, fed_avg)\n```\n\n## Future Work\n\n- Built-in support for standard averaging methods like FedAvg, \u0026 FedProx.\n\n\n\n## Feedback\nAny feedback/corrections/additions are welcome:\n\nIf this was helpful, please leave a star on the [github](https://github.com/akassharjun/ShapleyValueFL) page.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakassharjun%2Fshapleyvaluefl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fakassharjun%2Fshapleyvaluefl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakassharjun%2Fshapleyvaluefl/lists"}