├── LICENSE.txt ├── README.md ├── disparate_impact ├── README.md ├── adult_data_demo │ ├── demo_constraints.py │ └── prepare_adult_data.py └── synthetic_data_demo │ ├── decision_boundary_demo.py │ ├── fairness_acc_tradeoff.py │ ├── generate_synthetic_data.py │ └── img │ ├── a_cons.png │ ├── a_cons_fine.png │ ├── data.png │ ├── f_cons.png │ └── tradeoff.png ├── disparate_mistreatment ├── README.md ├── propublica_compas_data_demo │ ├── demo_constraints.py │ └── load_compas_data.py └── synthetic_data_demo │ ├── decision_boundary_demo.py │ ├── fairness_acc_tradeoff.py │ ├── generate_synthetic_data.py │ ├── img │ ├── data.png │ ├── fairness_acc_tradeoff_cons_type_1.png │ ├── syn_cons_dtype_1_cons_type_1.png │ ├── syn_cons_dtype_1_cons_type_2.png │ └── syn_cons_dtype_1_cons_type_4.png │ └── plot_syn_boundaries.py ├── fair_classification ├── funcs_disp_mist.py ├── linear_clf_pref_fairness.py ├── loss_funcs.py ├── stats_pref_fairness.py └── utils.py └── preferential_fairness ├── README.md ├── adult_data_demo ├── demo_constraints.py └── prepare_adult_data.py └── synthetic_data_demo ├── decision_boundary_demo.py ├── generate_synthetic_data.py ├── img ├── data.png ├── parity.png ├── preferred_both.png ├── preferred_impact.png └── unconstrained.png └── plot_synthetic_boundaries.py /LICENSE.txt: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Fairness in Classification 2 | 3 | 4 | This repository provides a logistic regression implementation in python for the fair classification mechanisms introduced in our AISTATS'17, WWW'17 and NIPS'17 papers. 5 | 6 | Specifically: 7 | 8 | 1. The AISTATS'17 paper [1] proposes mechanisms to make classification outcomes free of disparate impact, that is, to ensure that similar fractions of people from different demographic groups (e.g., males, females) are accepted (or classified as positive) by the classifier. More discussion about the disparate impact notion can be found in Sections 1 and 2 of the paper. 9 | 10 | 11 | 2. The WWW'17 paper [2] focuses on making classification outcomes free of disparate mistreatment, that is, to ensure that the misclassification rates for different demographic groups are similar. We discuss this fairness notion in detail, and contrast it to the disparate impact notion, in Sections 1, 2 and 3 of the paper. 12 | 13 | 14 | 2. The NIPS'17 paper [3] focuses on making classification outcomes adhere to preferred treatment and preferred impact fairness criteria. For more details on these fairness notions, and how they compare to existing fairness notions used in ML, see Sections 1 and 2 of the paper. 15 | 16 | #### Dependencies 17 | 1. [numpy, scipy](https://www.scipy.org/scipylib/download.html) and [matplotlib](http://matplotlib.org/) if you are only using the mechanisms introduced in [1]. 18 | 2. Additionally, if you are using the mechanisms introduced in [2] and [3], then you also need to install [CVXPY](https://github.com/cvxgrp/cvxpy) and [DCCP](https://github.com/cvxgrp/dccp). 19 | 20 | #### Using the code 21 | 22 | 1. If you want to use the code related to [1], please navigate to the directory "disparate_impact". 23 | 2. If you want to use the code related to [2], please navigate to the directory "disparate_mistreatment". 24 | 2. If you want to use the code related to [3], please navigate to the directory "preferential_fairness". 25 | 26 | Please cite the corresponding paper when using the code. 27 | 28 | #### References 29 | 1. Fairness Constraints: Mechanisms for Fair Classification
30 | Muhammad Bilal Zafar, Isabel Valera, Manuel Gomez Rodriguez, Krishna P. Gummadi.
31 | 20th International Conference on Artificial Intelligence and Statistics (AISTATS), Fort Lauderdale, FL, April 2017. 32 | 33 | 34 | 2. Fairness Beyond Disparate Treatment & Disparate Impact: Learning Classification without Disparate Mistreatment
35 | Muhammad Bilal Zafar, Isabel Valera, Manuel Gomez Rodriguez, Krishna P. Gummadi.
36 | 26th International World Wide Web Conference (WWW), Perth, Australia, April 2017. 37 | 38 | 39 | 3. From Parity to Preference-based Notions of Fairness in Classification
40 | Muhammad Bilal Zafar, Isabel Valera, Manuel Gomez Rodriguez, Krishna P. Gummadi, Adrian Weller.
41 | 31st Conference on Neural Information Processing Systems (NIPS), Long Beach, CA, December 2017. -------------------------------------------------------------------------------- /disparate_impact/README.md: -------------------------------------------------------------------------------- 1 | # Classification without disparate impact 2 | 3 | ## 1. Fair classification demo 4 | 5 | Fair classification corresponds to a scenario where we are learning classifiers from a dataset that is biased towards/against a specific demographic group, yet the classifier predictions are fair and do not show the biases contained in the data. For more details, have a look at Section 2 of our [paper](http://arxiv.org/abs/1507.05259). 6 | 7 | ### 1.1. Generating a biased dataset 8 | Lets start off by generating a sample dataset where class labels are biased towards a certain group. 9 | 10 | ```shell 11 | $ cd synthetic_data_demo 12 | $ python decision_boundary_demo.py 13 | ``` 14 | 15 | The code will generate a dataset with a multivariate normal distribution. The data consists of two non-sensitive features and one sensitive feature. 16 | 17 | 18 | 19 | Green color denotes the positive class while red denotes negative. Circles represent the non-protected group while crosses represent the protected group. It can be seen that class labels (green and red) are highly correlated with the sensitive feature value (protected and non-protected), that is, most of the green (positive class) points are in the non-protected class while most of red (negative class) points are in the protected class. **Close the figure** for the code to continue. Next, the code will output the following details about the dataset: 20 | 21 | ``` 22 | Total data points: 2000 23 | # non-protected examples: 914 24 | # protected examples: 1086 25 | Non-protected in positive class: 642 (70%) 26 | Protected in positive class: 358 (33%) 27 | P-rule is: 47% 28 | ``` 29 | 30 | The p-rule is essentially the ratio of (fractions of) protected and non-protected examples in the positive class. According to the [doctrine of disparate impact](https://en.wikipedia.org/wiki/Disparate_impact), fair systems should maintain a p-rule of at least 80%. More discussion on p-rule can be found in our [paper](http://arxiv.org/pdf/1507.05259.pdf) (Section 2). 31 | 32 | ### 1.2. Training an unconstrained classifier on the biased data 33 | 34 | Next, we will train a logistic regression classifier on the data to see the correlations between the classifier decisions and sensitive feature value: 35 | 36 | ```python 37 | # all constraint flags are set to 0 since we want to train an unconstrained (original) classifier 38 | apply_fairness_constraints = 0 39 | apply_accuracy_constraint = 0 40 | sep_constraint = 0 41 | w_uncons, p_uncons, acc_uncons = train_test_classifier() 42 | ``` 43 | 44 | We are training the classifier without any constraints (more on constraints to come later). "train_test_classifier()" will in turn call the function "train_model(...)": 45 | 46 | ```python 47 | ut.train_model(x_train, y_train, x_control_train, loss_function, apply_fairness_constraints, apply_accuracy_constraint, sep_constraint, sensitive_attrs, sensitive_attrs_to_cov_thresh, gamma) 48 | ``` 49 | 50 | Following output is generated by the program: 51 | 52 | ``` 53 | == Unconstrained (original) classifier == 54 | Accuracy: 0.85 55 | Protected/non-protected in +ve class: 33% / 70% 56 | P-rule achieved: 48% 57 | Covariance between sensitive feature and decision from distance boundary : 0.809 58 | ``` 59 | 60 | We can see that the classifier decisions reflect the biases contained in the original data, and the p-rule is 48%, showing the unfairness of classifier outcomes. The reason why the classifier shows similar biases as ones contained in the data is that the classifier model tries to minimize the loss (or maximize the accuracy) on the training data by learning the patterns in the data as best as possible. One of the patterns was the unfairness w.r.t. the sensitive feature, and the classifier ended up learning that as well. 61 | 62 | ### 1.3. Optimizing classifier accuracy subject to fairness constraints 63 | 64 | Next, we will try to make these outcomes fair by still **optimizing for classifier accuracy**, but **subject it to fairness constraints**. Refer to Section 3.2 of our [paper](http://arxiv.org/pdf/1507.05259.pdf) for more details. 65 | 66 | ```python 67 | apply_fairness_constraints = 1 # set this flag to one since we want to optimize accuracy subject to fairness constraints 68 | apply_accuracy_constraint = 0 69 | sep_constraint = 0 70 | sensitive_attrs_to_cov_thresh = {"s1":0} 71 | w_f_cons, p_f_cons, acc_f_cons = train_test_classifier() 72 | ``` 73 | 74 | Notice that setting _{"s1":0}_ means that the classifier should achieve 0 covariance between the sensitive feature (s1) value and distance to the decision boundary. A 0 covariance would mean no correlation between the two variables. 75 | 76 | The results for the fair classifier look like this: 77 | 78 | ``` 79 | == Classifier with fairness constraint == 80 | Accuracy: 0.71 81 | Protected/non-protected in +ve class: 51% / 53% 82 | P-rule achieved: 97% 83 | Covariance between sensitive feature and decision from distance boundary : 0.014 84 | ``` 85 | 86 | We can see that the classifier sacrificed some accuracy to achieve similar fractions of protected/non-protected examples in the positive class. The code will also show how the classifier shifts its boundary to achieve fairness, while making sure that the smallest possible loss in accuracy is incurred. 87 | 88 | 89 | 90 | The figure shows the original decision boundary (without any constraints) and the shifted decision boundary that was learnt by the fair classifier. Notice how the boundary shifts to push more non-protected points to the negative class (and vice-versa). 91 | 92 | 93 | ###1.4. Optimizing fairness subject to accuracy constraints 94 | 95 | Now lets try to **optimize fairness** (that does not necessarily correspond to a 100% p-rule) **subject to a deterministic loss in accuracy**. The details can be found in Section 3.3 of our [paper](http://arxiv.org/pdf/1507.05259.pdf). 96 | 97 | ```python 98 | apply_fairness_constraints = 0 # flag for fairness constraint is set back to0 since we want to apply the accuracy constraint now 99 | apply_accuracy_constraint = 1 # now, we want to optimize fairness subject to accuracy constraints 100 | sep_constraint = 0 101 | gamma = 0.5 102 | w_a_cons, p_a_cons, acc_a_cons = train_test_classifier() 103 | ``` 104 | 105 | The "gamma" variable controls how much loss in accuracy we are willing to take while optimizing for fairness. A larger value of gamma will result in more fair system, but we will be getting a more loss in accuracy. 106 | 107 | The results and the decision boundary for this experiment are: 108 | 109 | ``` 110 | == Classifier with accuracy constraint == 111 | Accuracy: 0.79 112 | Protected/non-protected in +ve class: 46% / 66% 113 | P-rule achieved: 70% 114 | Covariance between sensitive feature and decision from distance boundary : 0.155 115 | ``` 116 | 117 | 118 | 119 | You can experiment with more values of gamma to see how allowing more loss in accuracy leads to more fair boundaries. 120 | 121 | 122 | ### 1.5. Constraints on misclassying positive examples 123 | 124 | Next, lets try to train a fair classifier, however, lets put an additional constraint: do not misclassify any non-protected points that were classified in positive class by the original (unconstrained) classifier! The idea here is that we only want to promote the examples from protected group to the positive class, without demoting any non-protected points from the positive class (this might be a business necessity in many scenarios). Details of this formulation can be found in Section 3.3 of our [paper](http://arxiv.org/pdf/1507.05259.pdf). The code works as follows: 125 | 126 | ```python 127 | apply_fairness_constraints = 0 # flag for fairness constraint is set back to0 since we want to apply the accuracy constraint now 128 | apply_accuracy_constraint = 1 # now, we want to optimize accuracy subject to fairness constraints 129 | sep_constraint = 1 # set the separate constraint flag to one, since in addition to accuracy constrains, we also want no misclassifications for certain points (details in demo README.md) 130 | gamma = 2000.0 131 | w_a_cons_fine, p_a_cons_fine, acc_a_cons_fine = train_test_classifier() 132 | ``` 133 | 134 | The output looks like: 135 | 136 | ``` 137 | == Classifier with accuracy constraint (no +ve misclassification) == 138 | Accuracy: 0.67 139 | Protected/non-protected in +ve class: 81% / 90% 140 | P-rule achieved: 90% 141 | Covariance between sensitive feature and decision from distance boundary : 0.075 142 | 143 | ``` 144 | 145 | 146 | 147 | Notice the movement of decision boundary: we are only moving points to the positive class to achieve fairness (and not moving any non-protected point that was classified as positive by the original classifier to the negative class). 148 | 149 | ### 1.6. Understanding trade-offs between fairness and accuracy 150 | Remember, while optimizing for accuracy subject to fairness constraints, we forced the classifier to achieve perfect fairness by setting the covariance threshold to 0. This resulted in a perfectly fair classifier but we had to incur a rather big loss in accuracy (0.71 from 0.85). Lets see if we try a range of fairness values (not necessarily 100% p-rule), what kind of accuracy we will be achieving. We will do that by trying a range of values of covariance threshold (not only 0!) for this purpose. Execute the following command: 151 | 152 | ```shell 153 | $ cd synthetic_data_demo 154 | $ python fairness_acc_tradeoff.py 155 | ``` 156 | 157 | The code will generate the synthetic data as before, and call the following function: 158 | 159 | ```python 160 | ut.plot_cov_thresh_vs_acc_pos_ratio(X, y, x_control, NUM_FOLDS, loss_function, apply_fairness_constraints, apply_accuracy_constraint, sep_constraint, ['s1']) 161 | ``` 162 | 163 | The following output is generated: 164 | 165 | 166 | 167 | We can see that decreasing the covariance threshold value gives a continuous trade-off between fairness and accuracy. Specifically, we see that the fractions of protected and non-protected examples in positive class starts to converge (resulting in a greater p-rule), however, we get an increasing drop in accuracy. 168 | 169 | ###1.7. Adult data 170 | 171 | We also provide a demo of our code on [Adult dataset](http://archive.ics.uci.edu/ml/datasets/Adult). For applying the fairness constraints on the adult dataset, execute the following commands: 172 | 173 | ```shell 174 | $ cd adult_data_demo 175 | $ python demo_constraints.py 176 | ``` 177 | 178 | ## 2. Using the code 179 | 180 | ### 2.1. Training a(n) (un)fair classifier 181 | 182 | For training a fair classifier, set the values for constraints that you want to apply, and call the following function: 183 | 184 | ```python 185 | import utils as ut 186 | 187 | apply_fairness_constraints = 0 188 | apply_accuracy_constraint = 0 189 | sep_constraint = 0 190 | gamma = 0 191 | w = ut.train_model(x_train, y_train, x_control_train, loss_function, apply_fairness_constraints, apply_accuracy_constraint, sep_constraint, sensitive_attrs, sensitive_attrs_to_cov_thresh, gamma) 192 | ``` 193 | 194 | The function resides in file "fair_classification/utils.py". **Documentation about the type/format of the variables can be found at the beginning of the function**. 195 | 196 | Setting all the constraint values to 0 will train an unconstrained (original) logistic regression classifier. You can choose the constraint values selectively depending on which fairness formulation you want to use (**examples for each case provided in the demo above**). 197 | 198 | ### 2.2. Making predictions 199 | 200 | The function will return the weight vector learned by the classifier. Given an _(n)_ x _(d+1)_ array _X_ consisting of data _n_ points (and _d_ features -- first column in the weight array is for the intercept, and should be set to 1 for all data points), you can make the classifier predictions using the weight vector as follows: 201 | 202 | ```python 203 | distance_boundary = numpy.dot(w, X) # will give the distance from the decision boundary 204 | predicted_labels = np.sign(class_probabilities) # sign of the class probability is the class label 205 | ``` 206 | 207 | For using k-fold cross validation, call the function "ut.compute_cross_validation_error(...)". This function will automatically split the data into k (specified by user) train/test folds and return the accuracy and fairness numbers. 208 | -------------------------------------------------------------------------------- /disparate_impact/adult_data_demo/demo_constraints.py: -------------------------------------------------------------------------------- 1 | import os,sys 2 | import numpy as np 3 | from prepare_adult_data import * 4 | sys.path.insert(0, '../../fair_classification/') # the code for fair classification is in this directory 5 | import utils as ut 6 | import loss_funcs as lf # loss funcs that can be optimized subject to various constraints 7 | 8 | 9 | 10 | def test_adult_data(): 11 | 12 | 13 | """ Load the adult data """ 14 | X, y, x_control = load_adult_data(load_data_size=10000) # set the argument to none, or no arguments if you want to test with the whole data -- we are subsampling for performance speedup 15 | ut.compute_p_rule(x_control["sex"], y) # compute the p-rule in the original data 16 | 17 | 18 | 19 | """ Split the data into train and test """ 20 | X = ut.add_intercept(X) # add intercept to X before applying the linear classifier 21 | train_fold_size = 0.7 22 | x_train, y_train, x_control_train, x_test, y_test, x_control_test = ut.split_into_train_test(X, y, x_control, train_fold_size) 23 | 24 | 25 | 26 | 27 | apply_fairness_constraints = None 28 | apply_accuracy_constraint = None 29 | sep_constraint = None 30 | 31 | loss_function = lf._logistic_loss 32 | sensitive_attrs = ["sex"] 33 | sensitive_attrs_to_cov_thresh = {} 34 | gamma = None 35 | 36 | def train_test_classifier(): 37 | w = ut.train_model(x_train, y_train, x_control_train, loss_function, apply_fairness_constraints, apply_accuracy_constraint, sep_constraint, sensitive_attrs, sensitive_attrs_to_cov_thresh, gamma) 38 | train_score, test_score, correct_answers_train, correct_answers_test = ut.check_accuracy(w, x_train, y_train, x_test, y_test, None, None) 39 | distances_boundary_test = (np.dot(x_test, w)).tolist() 40 | all_class_labels_assigned_test = np.sign(distances_boundary_test) 41 | correlation_dict_test = ut.get_correlations(None, None, all_class_labels_assigned_test, x_control_test, sensitive_attrs) 42 | cov_dict_test = ut.print_covariance_sensitive_attrs(None, x_test, distances_boundary_test, x_control_test, sensitive_attrs) 43 | p_rule = ut.print_classifier_fairness_stats([test_score], [correlation_dict_test], [cov_dict_test], sensitive_attrs[0]) 44 | return w, p_rule, test_score 45 | 46 | 47 | """ Classify the data while optimizing for accuracy """ 48 | print 49 | print "== Unconstrained (original) classifier ==" 50 | # all constraint flags are set to 0 since we want to train an unconstrained (original) classifier 51 | apply_fairness_constraints = 0 52 | apply_accuracy_constraint = 0 53 | sep_constraint = 0 54 | w_uncons, p_uncons, acc_uncons = train_test_classifier() 55 | 56 | """ Now classify such that we optimize for accuracy while achieving perfect fairness """ 57 | apply_fairness_constraints = 1 # set this flag to one since we want to optimize accuracy subject to fairness constraints 58 | apply_accuracy_constraint = 0 59 | sep_constraint = 0 60 | sensitive_attrs_to_cov_thresh = {"sex":0} 61 | print 62 | print "== Classifier with fairness constraint ==" 63 | w_f_cons, p_f_cons, acc_f_cons = train_test_classifier() 64 | 65 | 66 | 67 | """ Classify such that we optimize for fairness subject to a certain loss in accuracy """ 68 | apply_fairness_constraints = 0 # flag for fairness constraint is set back to0 since we want to apply the accuracy constraint now 69 | apply_accuracy_constraint = 1 # now, we want to optimize fairness subject to accuracy constraints 70 | sep_constraint = 0 71 | gamma = 0.5 # gamma controls how much loss in accuracy we are willing to incur to achieve fairness -- increase gamme to allow more loss in accuracy 72 | print "== Classifier with accuracy constraint ==" 73 | w_a_cons, p_a_cons, acc_a_cons = train_test_classifier() 74 | 75 | """ 76 | Classify such that we optimize for fairness subject to a certain loss in accuracy 77 | In addition, make sure that no points classified as positive by the unconstrained (original) classifier are misclassified. 78 | 79 | """ 80 | apply_fairness_constraints = 0 # flag for fairness constraint is set back to0 since we want to apply the accuracy constraint now 81 | apply_accuracy_constraint = 1 # now, we want to optimize accuracy subject to fairness constraints 82 | sep_constraint = 1 # set the separate constraint flag to one, since in addition to accuracy constrains, we also want no misclassifications for certain points (details in demo README.md) 83 | gamma = 1000.0 84 | print "== Classifier with accuracy constraint (no +ve misclassification) ==" 85 | w_a_cons_fine, p_a_cons_fine, acc_a_cons_fine = train_test_classifier() 86 | 87 | return 88 | 89 | def main(): 90 | test_adult_data() 91 | 92 | 93 | if __name__ == '__main__': 94 | main() -------------------------------------------------------------------------------- /disparate_impact/adult_data_demo/prepare_adult_data.py: -------------------------------------------------------------------------------- 1 | import os,sys 2 | import urllib2 3 | sys.path.insert(0, '../../fair_classification/') # the code for fair classification is in this directory 4 | import utils as ut 5 | import numpy as np 6 | from random import seed, shuffle 7 | SEED = 1122334455 8 | seed(SEED) # set the random seed so that the random permutations can be reproduced again 9 | np.random.seed(SEED) 10 | 11 | """ 12 | The adult dataset can be obtained from: http://archive.ics.uci.edu/ml/datasets/Adult 13 | The code will look for the data files (adult.data, adult.test) in the present directory, if they are not found, it will download them from UCI archive. 14 | """ 15 | 16 | def check_data_file(fname): 17 | files = os.listdir(".") # get the current directory listing 18 | print "Looking for file '%s' in the current directory..." % fname 19 | 20 | if fname not in files: 21 | print "'%s' not found! Downloading from UCI Archive..." % fname 22 | addr = "http://archive.ics.uci.edu/ml/machine-learning-databases/adult/%s" % fname 23 | response = urllib2.urlopen(addr) 24 | data = response.read() 25 | fileOut = open(fname, "w") 26 | fileOut.write(data) 27 | fileOut.close() 28 | print "'%s' download and saved locally.." % fname 29 | else: 30 | print "File found in current directory.." 31 | 32 | print 33 | return 34 | 35 | 36 | def load_adult_data(load_data_size=None): 37 | 38 | """ 39 | if load_data_size is set to None (or if no argument is provided), then we load and return the whole data 40 | if it is a number, say 10000, then we will return randomly selected 10K examples 41 | """ 42 | 43 | attrs = ['age', 'workclass', 'fnlwgt', 'education', 'education_num', 'marital_status', 'occupation', 'relationship', 'race', 'sex', 'capital_gain', 'capital_loss', 'hours_per_week', 'native_country'] # all attributes 44 | int_attrs = ['age', 'fnlwgt', 'education_num', 'capital_gain', 'capital_loss', 'hours_per_week'] # attributes with integer values -- the rest are categorical 45 | sensitive_attrs = ['sex'] # the fairness constraints will be used for this feature 46 | attrs_to_ignore = ['sex', 'race' ,'fnlwgt'] # sex and race are sensitive feature so we will not use them in classification, we will not consider fnlwght for classification since its computed externally and it highly predictive for the class (for details, see documentation of the adult data) 47 | attrs_for_classification = set(attrs) - set(attrs_to_ignore) 48 | 49 | # adult data comes in two different files, one for training and one for testing, however, we will combine data from both the files 50 | data_files = ["adult.data", "adult.test"] 51 | 52 | 53 | 54 | X = [] 55 | y = [] 56 | x_control = {} 57 | 58 | attrs_to_vals = {} # will store the values for each attribute for all users 59 | for k in attrs: 60 | if k in sensitive_attrs: 61 | x_control[k] = [] 62 | elif k in attrs_to_ignore: 63 | pass 64 | else: 65 | attrs_to_vals[k] = [] 66 | 67 | for f in data_files: 68 | check_data_file(f) 69 | 70 | for line in open(f): 71 | line = line.strip() 72 | if line == "": continue # skip empty lines 73 | line = line.split(", ") 74 | if len(line) != 15 or "?" in line: # if a line has missing attributes, ignore it 75 | continue 76 | 77 | class_label = line[-1] 78 | if class_label in ["<=50K.", "<=50K"]: 79 | class_label = -1 80 | elif class_label in [">50K.", ">50K"]: 81 | class_label = +1 82 | else: 83 | raise Exception("Invalid class label value") 84 | 85 | y.append(class_label) 86 | 87 | 88 | for i in range(0,len(line)-1): 89 | attr_name = attrs[i] 90 | attr_val = line[i] 91 | # reducing dimensionality of some very sparse features 92 | if attr_name == "native_country": 93 | if attr_val!="United-States": 94 | attr_val = "Non-United-Stated" 95 | elif attr_name == "education": 96 | if attr_val in ["Preschool", "1st-4th", "5th-6th", "7th-8th"]: 97 | attr_val = "prim-middle-school" 98 | elif attr_val in ["9th", "10th", "11th", "12th"]: 99 | attr_val = "high-school" 100 | 101 | if attr_name in sensitive_attrs: 102 | x_control[attr_name].append(attr_val) 103 | elif attr_name in attrs_to_ignore: 104 | pass 105 | else: 106 | attrs_to_vals[attr_name].append(attr_val) 107 | 108 | def convert_attrs_to_ints(d): # discretize the string attributes 109 | for attr_name, attr_vals in d.items(): 110 | if attr_name in int_attrs: continue 111 | uniq_vals = sorted(list(set(attr_vals))) # get unique values 112 | 113 | # compute integer codes for the unique values 114 | val_dict = {} 115 | for i in range(0,len(uniq_vals)): 116 | val_dict[uniq_vals[i]] = i 117 | 118 | # replace the values with their integer encoding 119 | for i in range(0,len(attr_vals)): 120 | attr_vals[i] = val_dict[attr_vals[i]] 121 | d[attr_name] = attr_vals 122 | 123 | 124 | # convert the discrete values to their integer representations 125 | convert_attrs_to_ints(x_control) 126 | convert_attrs_to_ints(attrs_to_vals) 127 | 128 | 129 | # if the integer vals are not binary, we need to get one-hot encoding for them 130 | for attr_name in attrs_for_classification: 131 | attr_vals = attrs_to_vals[attr_name] 132 | if attr_name in int_attrs or attr_name == "native_country": # the way we encoded native country, its binary now so no need to apply one hot encoding on it 133 | X.append(attr_vals) 134 | 135 | else: 136 | attr_vals, index_dict = ut.get_one_hot_encoding(attr_vals) 137 | for inner_col in attr_vals.T: 138 | X.append(inner_col) 139 | 140 | 141 | # convert to numpy arrays for easy handline 142 | X = np.array(X, dtype=float).T 143 | y = np.array(y, dtype = float) 144 | for k, v in x_control.items(): x_control[k] = np.array(v, dtype=float) 145 | 146 | # shuffle the data 147 | perm = range(0,len(y)) # shuffle the data before creating each fold 148 | shuffle(perm) 149 | X = X[perm] 150 | y = y[perm] 151 | for k in x_control.keys(): 152 | x_control[k] = x_control[k][perm] 153 | 154 | # see if we need to subsample the data 155 | if load_data_size is not None: 156 | print "Loading only %d examples from the data" % load_data_size 157 | X = X[:load_data_size] 158 | y = y[:load_data_size] 159 | for k in x_control.keys(): 160 | x_control[k] = x_control[k][:load_data_size] 161 | 162 | return X, y, x_control -------------------------------------------------------------------------------- /disparate_impact/synthetic_data_demo/decision_boundary_demo.py: -------------------------------------------------------------------------------- 1 | import os,sys 2 | import numpy as np 3 | from generate_synthetic_data import * 4 | sys.path.insert(0, '../../fair_classification/') # the code for fair classification is in this directory 5 | import utils as ut 6 | import loss_funcs as lf # loss funcs that can be optimized subject to various constraints 7 | 8 | 9 | 10 | def test_synthetic_data(): 11 | 12 | """ Generate the synthetic data """ 13 | X, y, x_control = generate_synthetic_data(plot_data=True) # set plot_data to False to skip the data plot 14 | ut.compute_p_rule(x_control["s1"], y) # compute the p-rule in the original data 15 | 16 | 17 | """ Split the data into train and test """ 18 | X = ut.add_intercept(X) # add intercept to X before applying the linear classifier 19 | train_fold_size = 0.7 20 | x_train, y_train, x_control_train, x_test, y_test, x_control_test = ut.split_into_train_test(X, y, x_control, train_fold_size) 21 | 22 | 23 | 24 | apply_fairness_constraints = None 25 | apply_accuracy_constraint = None 26 | sep_constraint = None 27 | 28 | loss_function = lf._logistic_loss 29 | sensitive_attrs = ["s1"] 30 | sensitive_attrs_to_cov_thresh = {} 31 | gamma = None 32 | 33 | def train_test_classifier(): 34 | w = ut.train_model(x_train, y_train, x_control_train, loss_function, apply_fairness_constraints, apply_accuracy_constraint, sep_constraint, sensitive_attrs, sensitive_attrs_to_cov_thresh, gamma) 35 | train_score, test_score, correct_answers_train, correct_answers_test = ut.check_accuracy(w, x_train, y_train, x_test, y_test, None, None) 36 | distances_boundary_test = (np.dot(x_test, w)).tolist() 37 | all_class_labels_assigned_test = np.sign(distances_boundary_test) 38 | correlation_dict_test = ut.get_correlations(None, None, all_class_labels_assigned_test, x_control_test, sensitive_attrs) 39 | cov_dict_test = ut.print_covariance_sensitive_attrs(None, x_test, distances_boundary_test, x_control_test, sensitive_attrs) 40 | p_rule = ut.print_classifier_fairness_stats([test_score], [correlation_dict_test], [cov_dict_test], sensitive_attrs[0]) 41 | return w, p_rule, test_score 42 | 43 | 44 | def plot_boundaries(w1, w2, p1, p2, acc1, acc2, fname): 45 | 46 | num_to_draw = 200 # we will only draw a small number of points to avoid clutter 47 | x_draw = X[:num_to_draw] 48 | y_draw = y[:num_to_draw] 49 | x_control_draw = x_control["s1"][:num_to_draw] 50 | 51 | X_s_0 = x_draw[x_control_draw == 0.0] 52 | X_s_1 = x_draw[x_control_draw == 1.0] 53 | y_s_0 = y_draw[x_control_draw == 0.0] 54 | y_s_1 = y_draw[x_control_draw == 1.0] 55 | plt.scatter(X_s_0[y_s_0==1.0][:, 1], X_s_0[y_s_0==1.0][:, 2], color='green', marker='x', s=30, linewidth=1.5) 56 | plt.scatter(X_s_0[y_s_0==-1.0][:, 1], X_s_0[y_s_0==-1.0][:, 2], color='red', marker='x', s=30, linewidth=1.5) 57 | plt.scatter(X_s_1[y_s_1==1.0][:, 1], X_s_1[y_s_1==1.0][:, 2], color='green', marker='o', facecolors='none', s=30) 58 | plt.scatter(X_s_1[y_s_1==-1.0][:, 1], X_s_1[y_s_1==-1.0][:, 2], color='red', marker='o', facecolors='none', s=30) 59 | 60 | 61 | x1,x2 = max(x_draw[:,1]), min(x_draw[:,1]) 62 | y1,y2 = ut.get_line_coordinates(w1, x1, x2) 63 | plt.plot([x1,x2], [y1,y2], 'c-', linewidth=3, label = "Acc=%0.2f; p%% rule=%0.0f%% - Original"%(acc1, p1)) 64 | y1,y2 = ut.get_line_coordinates(w2, x1, x2) 65 | plt.plot([x1,x2], [y1,y2], 'b--', linewidth=3, label = "Acc=%0.2f; p%% rule=%0.0f%% - Constrained"%(acc2, p2)) 66 | 67 | 68 | 69 | plt.tick_params(axis='x', which='both', bottom='off', top='off', labelbottom='off') # dont need the ticks to see the data distribution 70 | plt.tick_params(axis='y', which='both', left='off', right='off', labelleft='off') 71 | plt.legend(loc=2, fontsize=15) 72 | plt.xlim((-15,10)) 73 | plt.ylim((-10,15)) 74 | plt.savefig(fname) 75 | plt.show() 76 | 77 | 78 | """ Classify the data while optimizing for accuracy """ 79 | print 80 | print "== Unconstrained (original) classifier ==" 81 | # all constraint flags are set to 0 since we want to train an unconstrained (original) classifier 82 | apply_fairness_constraints = 0 83 | apply_accuracy_constraint = 0 84 | sep_constraint = 0 85 | w_uncons, p_uncons, acc_uncons = train_test_classifier() 86 | 87 | """ Now classify such that we optimize for accuracy while achieving perfect fairness """ 88 | apply_fairness_constraints = 1 # set this flag to one since we want to optimize accuracy subject to fairness constraints 89 | apply_accuracy_constraint = 0 90 | sep_constraint = 0 91 | sensitive_attrs_to_cov_thresh = {"s1":0} 92 | print 93 | print "== Classifier with fairness constraint ==" 94 | w_f_cons, p_f_cons, acc_f_cons = train_test_classifier() 95 | plot_boundaries(w_uncons, w_f_cons, p_uncons, p_f_cons, acc_uncons, acc_f_cons, "img/f_cons.png") 96 | 97 | 98 | """ Classify such that we optimize for fairness subject to a certain loss in accuracy """ 99 | apply_fairness_constraints = 0 # flag for fairness constraint is set back to0 since we want to apply the accuracy constraint now 100 | apply_accuracy_constraint = 1 # now, we want to optimize fairness subject to accuracy constraints 101 | sep_constraint = 0 102 | gamma = 0.5 # gamma controls how much loss in accuracy we are willing to incur to achieve fairness -- increase gamme to allow more loss in accuracy 103 | print "== Classifier with accuracy constraint ==" 104 | w_a_cons, p_a_cons, acc_a_cons = train_test_classifier() 105 | plot_boundaries(w_uncons, w_a_cons, p_uncons, p_a_cons, acc_uncons, acc_a_cons, "img/a_cons.png") 106 | 107 | """ 108 | Classify such that we optimize for fairness subject to a certain loss in accuracy 109 | In addition, make sure that no points classified as positive by the unconstrained (original) classifier are misclassified. 110 | 111 | """ 112 | apply_fairness_constraints = 0 # flag for fairness constraint is set back to0 since we want to apply the accuracy constraint now 113 | apply_accuracy_constraint = 1 # now, we want to optimize accuracy subject to fairness constraints 114 | sep_constraint = 1 # set the separate constraint flag to one, since in addition to accuracy constrains, we also want no misclassifications for certain points (details in demo README.md) 115 | gamma = 2000.0 116 | print "== Classifier with accuracy constraint (no +ve misclassification) ==" 117 | w_a_cons_fine, p_a_cons_fine, acc_a_cons_fine = train_test_classifier() 118 | plot_boundaries(w_uncons, w_a_cons_fine, p_uncons, p_a_cons_fine, acc_uncons, acc_a_cons_fine, "img/a_cons_fine.png") 119 | 120 | return 121 | 122 | 123 | def main(): 124 | test_synthetic_data() 125 | 126 | 127 | if __name__ == '__main__': 128 | main() -------------------------------------------------------------------------------- /disparate_impact/synthetic_data_demo/fairness_acc_tradeoff.py: -------------------------------------------------------------------------------- 1 | import os,sys 2 | import numpy as np 3 | from generate_synthetic_data import * 4 | sys.path.insert(0, '../../fair_classification/') # the code for fair classification is in this directory 5 | import utils as ut 6 | import loss_funcs as lf # loss funcs that can be optimized subject to various constraints 7 | 8 | NUM_FOLDS = 10 # we will show 10-fold cross validation accuracy as a performance measure 9 | 10 | def test_synthetic_data(): 11 | 12 | """ Generate the synthetic data """ 13 | X, y, x_control = generate_synthetic_data(plot_data=False) 14 | ut.compute_p_rule(x_control["s1"], y) # compute the p-rule in the original data 15 | 16 | """ Classify the data without any constraints """ 17 | apply_fairness_constraints = 0 18 | apply_accuracy_constraint = 0 19 | sep_constraint = 0 20 | 21 | loss_function = lf._logistic_loss 22 | X = ut.add_intercept(X) # add intercept to X before applying the linear classifier 23 | test_acc_arr, train_acc_arr, correlation_dict_test_arr, correlation_dict_train_arr, cov_dict_test_arr, cov_dict_train_arr = ut.compute_cross_validation_error(X, y, x_control, NUM_FOLDS, loss_function, apply_fairness_constraints, apply_accuracy_constraint, sep_constraint, ['s1'], [{} for i in range(0,NUM_FOLDS)]) 24 | print 25 | print "== Unconstrained (original) classifier ==" 26 | ut.print_classifier_fairness_stats(test_acc_arr, correlation_dict_test_arr, cov_dict_test_arr, "s1") 27 | 28 | 29 | """ Now classify such that we achieve perfect fairness """ 30 | apply_fairness_constraints = 1 31 | cov_factor = 0 32 | test_acc_arr, train_acc_arr, correlation_dict_test_arr, correlation_dict_train_arr, cov_dict_test_arr, cov_dict_train_arr = ut.compute_cross_validation_error(X, y, x_control, NUM_FOLDS, loss_function, apply_fairness_constraints, apply_accuracy_constraint, sep_constraint, ['s1'], [{'s1':cov_factor} for i in range(0,NUM_FOLDS)]) 33 | print 34 | print "== Constrained (fair) classifier ==" 35 | ut.print_classifier_fairness_stats(test_acc_arr, correlation_dict_test_arr, cov_dict_test_arr, "s1") 36 | 37 | """ Now plot a tradeoff between the fairness and accuracy """ 38 | ut.plot_cov_thresh_vs_acc_pos_ratio(X, y, x_control, NUM_FOLDS, loss_function, apply_fairness_constraints, apply_accuracy_constraint, sep_constraint, ['s1']) 39 | 40 | 41 | 42 | def main(): 43 | test_synthetic_data() 44 | 45 | 46 | if __name__ == '__main__': 47 | main() -------------------------------------------------------------------------------- /disparate_impact/synthetic_data_demo/generate_synthetic_data.py: -------------------------------------------------------------------------------- 1 | import math 2 | import numpy as np 3 | import matplotlib.pyplot as plt # for plotting stuff 4 | from random import seed, shuffle 5 | from scipy.stats import multivariate_normal # generating synthetic data 6 | SEED = 1122334455 7 | seed(SEED) # set the random seed so that the random permutations can be reproduced again 8 | np.random.seed(SEED) 9 | 10 | def generate_synthetic_data(plot_data=False): 11 | 12 | """ 13 | Code for generating the synthetic data. 14 | We will have two non-sensitive features and one sensitive feature. 15 | A sensitive feature value of 0.0 means the example is considered to be in protected group (e.g., female) and 1.0 means it's in non-protected group (e.g., male). 16 | """ 17 | 18 | n_samples = 1000 # generate these many data points per class 19 | disc_factor = math.pi / 4.0 # this variable determines the initial discrimination in the data -- decraese it to generate more discrimination 20 | 21 | def gen_gaussian(mean_in, cov_in, class_label): 22 | nv = multivariate_normal(mean = mean_in, cov = cov_in) 23 | X = nv.rvs(n_samples) 24 | y = np.ones(n_samples, dtype=float) * class_label 25 | return nv,X,y 26 | 27 | """ Generate the non-sensitive features randomly """ 28 | # We will generate one gaussian cluster for each class 29 | mu1, sigma1 = [2, 2], [[5, 1], [1, 5]] 30 | mu2, sigma2 = [-2,-2], [[10, 1], [1, 3]] 31 | nv1, X1, y1 = gen_gaussian(mu1, sigma1, 1) # positive class 32 | nv2, X2, y2 = gen_gaussian(mu2, sigma2, -1) # negative class 33 | 34 | # join the posisitve and negative class clusters 35 | X = np.vstack((X1, X2)) 36 | y = np.hstack((y1, y2)) 37 | 38 | # shuffle the data 39 | perm = range(0,n_samples*2) 40 | shuffle(perm) 41 | X = X[perm] 42 | y = y[perm] 43 | 44 | rotation_mult = np.array([[math.cos(disc_factor), -math.sin(disc_factor)], [math.sin(disc_factor), math.cos(disc_factor)]]) 45 | X_aux = np.dot(X, rotation_mult) 46 | 47 | 48 | """ Generate the sensitive feature here """ 49 | x_control = [] # this array holds the sensitive feature value 50 | for i in range (0, len(X)): 51 | x = X_aux[i] 52 | 53 | # probability for each cluster that the point belongs to it 54 | p1 = nv1.pdf(x) 55 | p2 = nv2.pdf(x) 56 | 57 | # normalize the probabilities from 0 to 1 58 | s = p1+p2 59 | p1 = p1/s 60 | p2 = p2/s 61 | 62 | r = np.random.uniform() # generate a random number from 0 to 1 63 | 64 | if r < p1: # the first cluster is the positive class 65 | x_control.append(1.0) # 1.0 means its male 66 | else: 67 | x_control.append(0.0) # 0.0 -> female 68 | 69 | x_control = np.array(x_control) 70 | 71 | """ Show the data """ 72 | if plot_data: 73 | num_to_draw = 200 # we will only draw a small number of points to avoid clutter 74 | x_draw = X[:num_to_draw] 75 | y_draw = y[:num_to_draw] 76 | x_control_draw = x_control[:num_to_draw] 77 | 78 | X_s_0 = x_draw[x_control_draw == 0.0] 79 | X_s_1 = x_draw[x_control_draw == 1.0] 80 | y_s_0 = y_draw[x_control_draw == 0.0] 81 | y_s_1 = y_draw[x_control_draw == 1.0] 82 | plt.scatter(X_s_0[y_s_0==1.0][:, 0], X_s_0[y_s_0==1.0][:, 1], color='green', marker='x', s=30, linewidth=1.5, label= "Prot. +ve") 83 | plt.scatter(X_s_0[y_s_0==-1.0][:, 0], X_s_0[y_s_0==-1.0][:, 1], color='red', marker='x', s=30, linewidth=1.5, label = "Prot. -ve") 84 | plt.scatter(X_s_1[y_s_1==1.0][:, 0], X_s_1[y_s_1==1.0][:, 1], color='green', marker='o', facecolors='none', s=30, label = "Non-prot. +ve") 85 | plt.scatter(X_s_1[y_s_1==-1.0][:, 0], X_s_1[y_s_1==-1.0][:, 1], color='red', marker='o', facecolors='none', s=30, label = "Non-prot. -ve") 86 | 87 | 88 | plt.tick_params(axis='x', which='both', bottom='off', top='off', labelbottom='off') # dont need the ticks to see the data distribution 89 | plt.tick_params(axis='y', which='both', left='off', right='off', labelleft='off') 90 | plt.legend(loc=2, fontsize=15) 91 | plt.xlim((-15,10)) 92 | plt.ylim((-10,15)) 93 | plt.savefig("img/data.png") 94 | plt.show() 95 | 96 | x_control = {"s1": x_control} # all the sensitive features are stored in a dictionary 97 | return X,y,x_control 98 | -------------------------------------------------------------------------------- /disparate_impact/synthetic_data_demo/img/a_cons.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbilalzafar/fair-classification/45e85baad209134bbe2697c1a884dd1e2c2d9786/disparate_impact/synthetic_data_demo/img/a_cons.png -------------------------------------------------------------------------------- /disparate_impact/synthetic_data_demo/img/a_cons_fine.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbilalzafar/fair-classification/45e85baad209134bbe2697c1a884dd1e2c2d9786/disparate_impact/synthetic_data_demo/img/a_cons_fine.png -------------------------------------------------------------------------------- /disparate_impact/synthetic_data_demo/img/data.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbilalzafar/fair-classification/45e85baad209134bbe2697c1a884dd1e2c2d9786/disparate_impact/synthetic_data_demo/img/data.png -------------------------------------------------------------------------------- /disparate_impact/synthetic_data_demo/img/f_cons.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbilalzafar/fair-classification/45e85baad209134bbe2697c1a884dd1e2c2d9786/disparate_impact/synthetic_data_demo/img/f_cons.png -------------------------------------------------------------------------------- /disparate_impact/synthetic_data_demo/img/tradeoff.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbilalzafar/fair-classification/45e85baad209134bbe2697c1a884dd1e2c2d9786/disparate_impact/synthetic_data_demo/img/tradeoff.png -------------------------------------------------------------------------------- /disparate_mistreatment/README.md: -------------------------------------------------------------------------------- 1 | # Classification without disparate mistreatment 2 | 3 | * [1. Fair classification demo](#1-fair-classification-demo) 4 | * [1.1. Generating a sample dataset](#11-generating-a-sample-dataset) 5 | * [1.2. Training an unconstrained classifier](#12-training-an-unconstrained-classifier) 6 | * [1.3. Removing unfairness w.r.t. false positive rate](#13-removing-unfairness-wrt-false-positive-rate) 7 | * [1.4. Removing unfairness w.r.t. false negative rate](#14-removing-unfairness-wrt-false-negative-rate) 8 | * [1.5. Removing unfairness w.r.t. both false positive and negative rate](#15-removing-unfairness-wrt-both-false-positive-and-negative-rate) 9 | * [1.6. However...](#16-however) 10 | * [1.7. Understanding trade-offs between fairness and accuracy](#17-understanding-trade-offs-between-fairness-and-accuracy) 11 | * [1.8. ProPublica COMPAS dataset](#18-propublica-compas-dataset) 12 | * [2. Using the code](#2-using-the-code) 13 | * [2.1. Training a(n) (un)fair classifier](#21-training-an-unfair-classifier) 14 | * [2.2. Making predictions](#22-making-predictions) 15 | * [3. Code update](#3-code-update) 16 | 17 | ## 1. Fair classification demo 18 | 19 | Imagine we are training classifiers on a given dataset and the learned classifier results in different misclassification rates for different demographic groups. Fair classification here corresponds to removing such disparities in misclassification rates from classification outcomes. 20 | 21 | For more details, have a look at our paper. 22 | 23 | ### 1.1. Generating a sample dataset 24 | Lets start off by generating a sample dataset. 25 | 26 | ```shell 27 | $ cd synthetic_data_demo 28 | $ python decision_boundary_demo.py 29 | ``` 30 | 31 | The code will generate a dataset with a multivariate normal distribution. The data consists of two non-sensitive features and one sensitive feature. Sensitive feature encodes the demographic group (e.g., gender, race) of each data point. 32 | 33 | 34 | 35 | 36 | Green color denotes the positive class while red denotes the negative class. The sensitive feature here takes two values: 0 and 1. So the dataset tries to simulate two different demographic groups. You can notice that the positive and negative classes for group-0 (crosses) have a different distribution as compared to the positive and negative classes for group-1 (circles). As a result, a classifier trained on this data might have different accuracy (or misclassification rates) for different groups. Let us confirm if this actually happens. 37 | 38 | _Close the figure to continue the code._ 39 | 40 | ### 1.2. Training an unconstrained classifier 41 | 42 | Next, the code will train a logistic regression classifier on the data: 43 | 44 | ```python 45 | w_uncons, acc_uncons, s_attr_to_fp_fn_test_uncons = train_test_classifier() 46 | ``` 47 | 48 | We are training the classifier without any constraints (more on constraints to come later). 49 | The following output is generated by the program: 50 | 51 | ``` 52 | == Unconstrained (original) classifier == 53 | 54 | Accuracy: 0.821 55 | || s || FPR. || FNR. || 56 | || 0 || 0.26 || 0.24 || 57 | || 1 || 0.08 || 0.14 || 58 | ``` 59 | 60 | We see that the classifier has very different misclassification rates for the two groups. Specifically, both false positive as well as false negative rates for group-0 are higher than those for group-1. That is, a classifier optimizing for misclassification rates (or, accuracy) on the whole dataset leads to different misclassification rates for the two demographic groups. 61 | This may be unfair in certain scenarios. For example this could be a classifier that is making decisions to grant loans to the applicants, and having different misclassification rates for the two groups can be considered unfair. 62 | Let us try to fix this unfairness. 63 | 64 | We will first try to fix disparate false positive rates for both groups: 65 | 66 | ### 1.3. Removing unfairness w.r.t. false positive rate 67 | 68 | 69 | ```python 70 | cons_type = 1 # FPR constraint -- just change the cons_type, the rest of parameters should stay the same 71 | tau = 5.0 72 | mu = 1.2 73 | sensitive_attrs_to_cov_thresh = {"s1": {0:{0:0, 1:0}, 1:{0:0, 1:0}, 2:{0:0, 1:0}}} # zero covariance threshold, means try to get the fairest solution 74 | cons_params = {"cons_type": cons_type, 75 | "tau": tau, 76 | "mu": mu, 77 | "sensitive_attrs_to_cov_thresh": sensitive_attrs_to_cov_thresh} 78 | 79 | w_cons, acc_cons, s_attr_to_fp_fn_test_cons = train_test_classifier() 80 | ``` 81 | 82 | Notice that setting all values to 0 in _sensitive_attrs_to_cov_thresh_ means that the classifier should achieve 0 covariance between the sensitive feature (s1) value and distance to the decision boundary for the points misclassified as false positives. Refer to Section 4 of our paper for more details. 83 | 84 | The results for the fair classifier look like this: 85 | 86 | ``` 87 | === Constraints on FPR === 88 | 89 | Accuracy: 0.774 90 | || s || FPR. || FNR. || 91 | || 0 || 0.16 || 0.37 || 92 | || 1 || 0.16 || 0.21 || 93 | ``` 94 | 95 | So, the classifier sacrificed around 5% of (overall) accuracy to remove disparity in false positive rates for the two groups. Specifically, it decreased the FPR (as compared to the original classifier) for group-0 and increased the FPR for group-1. The code will also show how the classifier shifts its boundary to achieve fairness, while making sure that a minimal loss in overall accuracy is incurred. 96 | 97 | 98 | 99 | The figure shows the original decision boundary (without any constraints, solid line) and the fair decision boundary (dotted line) resulting from the constraints on false positive rates. Notice how the boundary shifts to push some previously misclassified points from group-0 into the negative class (_decreasing_ its false positive rate) and some correctly classified points from group-1 into the positive class (_increasing_ its false positive rate). Also, the false negative rates for both groups, while different from that of the original classifier, still show some disparity. 100 | 101 | 102 | ### 1.4. Removing unfairness w.r.t. false negative rate 103 | 104 | Now, lets try to remove disparity in false negative rates. 105 | 106 | ```python 107 | cons_type = 2 108 | cons_params["cons_type"] = cons_type # FNR constraint -- just change the cons_type, the rest of parameters should stay the same 109 | w_cons, acc_cons, s_attr_to_fp_fn_test_cons = train_test_classifier() 110 | ``` 111 | 112 | The results and the decision boundary for this experiment are: 113 | 114 | ``` 115 | === Constraints on FNR === 116 | 117 | Accuracy: 0.768 118 | || s || FPR. || FNR. || 119 | || 0 || 0.58 || 0.16 || 120 | || 1 || 0.04 || 0.14 || 121 | ``` 122 | 123 | 124 | 125 | Notice that: (i) the rotation of boundary to fix false negative rate is different as compared to the previous case and (ii) the FNR constraints fixed disparity on false negative rate, but the disparity on false positive rates got even worse! 126 | 127 | Next, lets try to fix disparity with respect to both false positive as well as false negative rates. 128 | 129 | 130 | ### 1.5. Removing unfairness w.r.t. _both_ false positive and negative rate 131 | 132 | ```python 133 | cons_type = 4 134 | cons_params["cons_type"] = cons_type # both FPR and FNR constraints 135 | w_cons, acc_cons, s_attr_to_fp_fn_test_cons = train_test_classifier() 136 | ``` 137 | 138 | The output looks like: 139 | 140 | ``` 141 | === Constraints on both FPR and FNR === 142 | 143 | Accuracy: 0.637 144 | || s || FPR. || FNR. || 145 | || 0 || 0.68 || 0.02 || 146 | || 1 || 0.76 || 0.00 || 147 | ``` 148 | 149 | 150 | 151 | We see that: (i) disparity on both false positive as well as false negative rates is fixed by moving the boundary by a larger margin as compared to the previous two cases and (ii) we have to incur a much bigger loss in accuracy to achieve fairness (with respect to both rates). 152 | 153 | ### 1.6. However... 154 | 155 | These shifts in boundary can be very different in different settings. For example, in certain cases (depending on the underlying data distribution), fixing unfairness w.r.t. false positive rates can also fix disparity on false negative rates (as we will see a little later). Similarly, the precise amount of accuracy that we have to trade-off to achieve fairness will also vary depending on the specific dataset. We experiment with some of these scenarios in Section 5 of our paper. 156 | 157 | Also, one might wish for overall misclassification (irrespective of whether these misclassifications are false positives or false negative) to be the same. 158 | Such scenarios can be simulated by passing ```cons_type=0``` to ```cons_params``` variable. 159 | 160 | ### 1.7. Understanding trade-offs between fairness and accuracy 161 | Remember, until now, we forced the classifier to achieve perfect fairness by setting the covariance threshold to 0. In certain scenarios, a perfectly fair classifier might come at a large cost in terms of accuracy. Lets see if we try a range of fairness values (not necessarily equal false positive and/or false negative rates), what kind of accuracy we will be achieving. We will do that by trying a range of values of covariance threshold (not only 0!) for this purpose. Execute the following command: 162 | 163 | ```shell 164 | $ python fairness_acc_tradeoff.py 165 | ``` 166 | 167 | The code will generate the synthetic data as before, apply the constraints to remove disparity in false positive rates and generate the following output: 168 | 169 | 170 | 171 | We can see that decreasing the covariance threshold value gives a continuous trade-off between fairness and accuracy. Specifically, we see a continuous range of (decreasing) disparity in false positive rates for both groups, and a corresponding drop in accuracy. You can change the variable ```cons_type``` to experiment with other constraints as well. 172 | 173 | ### 1.8. ProPublica COMPAS dataset 174 | 175 | We also provide a demo of our scheme with ProPublica COMPAS dataset. For seeing the effect of fairness constraints on this dataset, execute the following commands: 176 | 177 | ```shell 178 | $ cd propublica_compas_data_demo/ 179 | $ python demo_constraints.py 180 | ``` 181 | 182 | The code will show the stats for an unconstrained (potentially unfair) classifier, and a classifier constrained to have equal false positive rates for two demographic groups, African-Americans (encoded as 0) and Caucasians (encoded as 1). 183 | 184 | ```shell 185 | == Unconstrained (original) classifier == 186 | 187 | Accuracy: 0.678 188 | || s || FPR. || FNR. || 189 | || 0 || 0.36 || 0.27 || 190 | || 1 || 0.18 || 0.59 || 191 | ----------------------------------------------------------------------------------- 192 | == Constraints on FPR == 193 | 194 | Accuracy: 0.659 195 | || s || FPR. || FNR. || 196 | || 0 || 0.24 || 0.43 || 197 | || 1 || 0.25 || 0.50 || 198 | ``` 199 | 200 | You will notice that in this dataset, controlling for unfairness w.r.t. false positive rates also helps control unfairness on false negative rates (rather than making it worse, or not affecting it at all). For more discussion, please see Section 5 of our paper. 201 | 202 | ## 2. Using the code 203 | 204 | ### 2.1. Training a(n) (un)fair classifier 205 | 206 | For training a fair classifier, set the values for constraints that you want to apply, and call the following function: 207 | 208 | ```python 209 | import utils as ut 210 | import funcs_disp_mist as fdm 211 | 212 | w = fdm.train_model_disp_mist(x_train, y_train, x_control_train, loss_function, EPS, cons_params) 213 | ``` 214 | 215 | The function resides in file "fair_classification/funcs_disp_mist.py". **Documentation about the type/format of the variables can be found at the beginning of the function**. 216 | 217 | Passing ```cons_params = None``` will train an unconstrained (original) logistic regression classifier. For details on how to apply fairness constraints w.r.t. all misclassifications, false positive rates, false negative rates, or both, see the function documentation and the demo files above. 218 | 219 | Finally, since the constrained classifier needs access to the misclassification covariance of the unconstrained (original) classifier, this information has to be provided as a part of the ```cons_params``` variable. This covariance is computed by the ```get_sensitive_attr_constraint_fpr_fnr_cov(...)``` function in "fair_classification/funcs_disp_mist.py". Detailed example of how to compute the covariance and how to construct the ```cons_params``` variable can be found in "disparate_mistreatment/synthetic_data_demo/decision_boundary_demo.py". 220 | 221 | ### 2.2. Making predictions 222 | 223 | The function will return the weight vector learned by the classifier. Given an _(n)_ x _(d+1)_ array _X_ consisting of data _n_ points (and _d_ features -- first column in the weight array is for the intercept, and should be set to 1 for all data points), you can make the classifier predictions using the weight vector as follows: 224 | 225 | ```python 226 | distance_boundary = numpy.dot(w, X) # will give the distance from the decision boundary 227 | predicted_labels = np.sign(distance_boundary) # sign of the distance from boundary is the class label 228 | ``` 229 | 230 | 231 | ## 3. Code update 232 | 233 | We have made changes to constraints for false positive and false negative rates. In the paper(in Section 4), we compute the misclassification covariance over the whole dataset for _all_ constraint types. However, if the base rates (fraction in positive class in the training dataset) are different for various groups, computing the covariance over the whole dataset could result in under- or over-estimating the false positive / negative rates (the constraints for overall misclassification rates do not get affected by different base rates). To overcome this issue, we have changed the constraints in the following way: for false positive rate constraints, the covariance is computed over the ground truth negative dataset, and for false negative rate constraints, the covariance is computed over the ground truth positive dataset. This way, the constraints do not under- or over-estimate FPR or FNR. 234 | -------------------------------------------------------------------------------- /disparate_mistreatment/propublica_compas_data_demo/demo_constraints.py: -------------------------------------------------------------------------------- 1 | import os,sys 2 | import numpy as np 3 | from load_compas_data import * 4 | sys.path.insert(0, '../../fair_classification/') # the code for fair classification is in this directory 5 | import utils as ut 6 | import funcs_disp_mist as fdm 7 | 8 | 9 | 10 | 11 | def test_compas_data(): 12 | 13 | """ Generate the synthetic data """ 14 | data_type = 1 15 | X, y, x_control = load_compas_data() 16 | sensitive_attrs = x_control.keys() 17 | 18 | 19 | """ Split the data into train and test """ 20 | train_fold_size = 0.5 21 | x_train, y_train, x_control_train, x_test, y_test, x_control_test = ut.split_into_train_test(X, y, x_control, train_fold_size) 22 | 23 | cons_params = None # constraint parameters, will use them later 24 | loss_function = "logreg" # perform the experiments with logistic regression 25 | EPS = 1e-6 26 | 27 | def train_test_classifier(): 28 | w = fdm.train_model_disp_mist(x_train, y_train, x_control_train, loss_function, EPS, cons_params) 29 | 30 | train_score, test_score, cov_all_train, cov_all_test, s_attr_to_fp_fn_train, s_attr_to_fp_fn_test = fdm.get_clf_stats(w, x_train, y_train, x_control_train, x_test, y_test, x_control_test, sensitive_attrs) 31 | 32 | 33 | # accuracy and FPR are for the test because we need of for plotting 34 | return w, test_score, s_attr_to_fp_fn_test 35 | 36 | 37 | """ Classify the data while optimizing for accuracy """ 38 | print 39 | print "== Unconstrained (original) classifier ==" 40 | w_uncons, acc_uncons, s_attr_to_fp_fn_test_uncons = train_test_classifier() 41 | print "\n-----------------------------------------------------------------------------------\n" 42 | 43 | """ Now classify such that we optimize for accuracy while achieving perfect fairness """ 44 | 45 | print 46 | 47 | print "\n\n== Constraints on FPR ==" # setting parameter for constraints 48 | cons_type = 1 # FPR constraint -- just change the cons_type, the rest of parameters should stay the same 49 | tau = 5.0 50 | mu = 1.2 51 | sensitive_attrs_to_cov_thresh = {"race": {0:{0:0, 1:0}, 1:{0:0, 1:0}, 2:{0:0, 1:0}}} # zero covariance threshold, means try to get the fairest solution 52 | cons_params = {"cons_type": cons_type, 53 | "tau": tau, 54 | "mu": mu, 55 | "sensitive_attrs_to_cov_thresh": sensitive_attrs_to_cov_thresh} 56 | 57 | w_cons, acc_cons, s_attr_to_fp_fn_test_cons = train_test_classifier() 58 | print "\n-----------------------------------------------------------------------------------\n" 59 | 60 | return 61 | 62 | 63 | def main(): 64 | test_compas_data() 65 | 66 | 67 | if __name__ == '__main__': 68 | main() -------------------------------------------------------------------------------- /disparate_mistreatment/propublica_compas_data_demo/load_compas_data.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | import urllib2 3 | import os,sys 4 | import numpy as np 5 | import pandas as pd 6 | from collections import defaultdict 7 | from sklearn import feature_extraction 8 | from sklearn import preprocessing 9 | from random import seed, shuffle 10 | 11 | sys.path.insert(0, '../../fair_classification/') # the code for fair classification is in this directory 12 | import utils as ut 13 | 14 | SEED = 1234 15 | seed(SEED) 16 | np.random.seed(SEED) 17 | 18 | """ 19 | The adult dataset can be obtained from: https://raw.githubusercontent.com/propublica/compas-analysis/master/compas-scores-two-years.csv 20 | The code will look for the data file in the present directory, if it is not found, it will download them from GitHub. 21 | """ 22 | 23 | def check_data_file(fname): 24 | files = os.listdir(".") # get the current directory listing 25 | print "Looking for file '%s' in the current directory..." % fname 26 | 27 | if fname not in files: 28 | print "'%s' not found! Downloading from GitHub..." % fname 29 | addr = "https://raw.githubusercontent.com/propublica/compas-analysis/master/compas-scores-two-years.csv" 30 | response = urllib2.urlopen(addr) 31 | data = response.read() 32 | fileOut = open(fname, "w") 33 | fileOut.write(data) 34 | fileOut.close() 35 | print "'%s' download and saved locally.." % fname 36 | else: 37 | print "File found in current directory.." 38 | 39 | 40 | def load_compas_data(): 41 | 42 | FEATURES_CLASSIFICATION = ["age_cat", "race", "sex", "priors_count", "c_charge_degree"] #features to be used for classification 43 | CONT_VARIABLES = ["priors_count"] # continuous features, will need to be handled separately from categorical features, categorical features will be encoded using one-hot 44 | CLASS_FEATURE = "two_year_recid" # the decision variable 45 | SENSITIVE_ATTRS = ["race"] 46 | 47 | 48 | COMPAS_INPUT_FILE = "compas-scores-two-years.csv" 49 | check_data_file(COMPAS_INPUT_FILE) 50 | 51 | # load the data and get some stats 52 | df = pd.read_csv(COMPAS_INPUT_FILE) 53 | df = df.dropna(subset=["days_b_screening_arrest"]) # dropping missing vals 54 | 55 | # convert to np array 56 | data = df.to_dict('list') 57 | for k in data.keys(): 58 | data[k] = np.array(data[k]) 59 | 60 | 61 | """ Filtering the data """ 62 | 63 | # These filters are the same as propublica (refer to https://github.com/propublica/compas-analysis) 64 | # If the charge date of a defendants Compas scored crime was not within 30 days from when the person was arrested, we assume that because of data quality reasons, that we do not have the right offense. 65 | idx = np.logical_and(data["days_b_screening_arrest"]<=30, data["days_b_screening_arrest"]>=-30) 66 | 67 | 68 | # We coded the recidivist flag -- is_recid -- to be -1 if we could not find a compas case at all. 69 | idx = np.logical_and(idx, data["is_recid"] != -1) 70 | 71 | # In a similar vein, ordinary traffic offenses -- those with a c_charge_degree of 'O' -- will not result in Jail time are removed (only two of them). 72 | idx = np.logical_and(idx, data["c_charge_degree"] != "O") # F: felony, M: misconduct 73 | 74 | # We filtered the underlying data from Broward county to include only those rows representing people who had either recidivated in two years, or had at least two years outside of a correctional facility. 75 | idx = np.logical_and(idx, data["score_text"] != "NA") 76 | 77 | # we will only consider blacks and whites for this analysis 78 | idx = np.logical_and(idx, np.logical_or(data["race"] == "African-American", data["race"] == "Caucasian")) 79 | 80 | # select the examples that satisfy this criteria 81 | for k in data.keys(): 82 | data[k] = data[k][idx] 83 | 84 | 85 | 86 | """ Feature normalization and one hot encoding """ 87 | 88 | # convert class label 0 to -1 89 | y = data[CLASS_FEATURE] 90 | y[y==0] = -1 91 | 92 | 93 | 94 | print "\nNumber of people recidivating within two years" 95 | print pd.Series(y).value_counts() 96 | print "\n" 97 | 98 | 99 | X = np.array([]).reshape(len(y), 0) # empty array with num rows same as num examples, will hstack the features to it 100 | x_control = defaultdict(list) 101 | 102 | feature_names = [] 103 | for attr in FEATURES_CLASSIFICATION: 104 | vals = data[attr] 105 | if attr in CONT_VARIABLES: 106 | vals = [float(v) for v in vals] 107 | vals = preprocessing.scale(vals) # 0 mean and 1 variance 108 | vals = np.reshape(vals, (len(y), -1)) # convert from 1-d arr to a 2-d arr with one col 109 | 110 | else: # for binary categorical variables, the label binarizer uses just one var instead of two 111 | lb = preprocessing.LabelBinarizer() 112 | lb.fit(vals) 113 | vals = lb.transform(vals) 114 | 115 | # add to sensitive features dict 116 | if attr in SENSITIVE_ATTRS: 117 | x_control[attr] = vals 118 | 119 | 120 | # add to learnable features 121 | X = np.hstack((X, vals)) 122 | 123 | if attr in CONT_VARIABLES: # continuous feature, just append the name 124 | feature_names.append(attr) 125 | else: # categorical features 126 | if vals.shape[1] == 1: # binary features that passed through lib binarizer 127 | feature_names.append(attr) 128 | else: 129 | for k in lb.classes_: # non-binary categorical features, need to add the names for each cat 130 | feature_names.append(attr + "_" + str(k)) 131 | 132 | 133 | # convert the sensitive feature to 1-d array 134 | x_control = dict(x_control) 135 | for k in x_control.keys(): 136 | assert(x_control[k].shape[1] == 1) # make sure that the sensitive feature is binary after one hot encoding 137 | x_control[k] = np.array(x_control[k]).flatten() 138 | 139 | # sys.exit(1) 140 | 141 | """permute the date randomly""" 142 | perm = range(0,X.shape[0]) 143 | shuffle(perm) 144 | X = X[perm] 145 | y = y[perm] 146 | for k in x_control.keys(): 147 | x_control[k] = x_control[k][perm] 148 | 149 | 150 | X = ut.add_intercept(X) 151 | 152 | feature_names = ["intercept"] + feature_names 153 | assert(len(feature_names) == X.shape[1]) 154 | print "Features we will be using for classification are:", feature_names, "\n" 155 | 156 | 157 | return X, y, x_control 158 | -------------------------------------------------------------------------------- /disparate_mistreatment/synthetic_data_demo/decision_boundary_demo.py: -------------------------------------------------------------------------------- 1 | import os,sys 2 | import numpy as np 3 | from generate_synthetic_data import * 4 | sys.path.insert(0, '../../fair_classification/') # the code for fair classification is in this directory 5 | import utils as ut 6 | import funcs_disp_mist as fdm 7 | import plot_syn_boundaries as psb 8 | 9 | 10 | 11 | def test_synthetic_data(): 12 | 13 | """ Generate the synthetic data """ 14 | data_type = 1 15 | X, y, x_control = generate_synthetic_data(data_type=data_type, plot_data=True) # set plot_data to False to skip the data plot 16 | sensitive_attrs = x_control.keys() 17 | 18 | """ Split the data into train and test """ 19 | train_fold_size = 0.5 20 | x_train, y_train, x_control_train, x_test, y_test, x_control_test = ut.split_into_train_test(X, y, x_control, train_fold_size) 21 | 22 | cons_params = None # constraint parameters, will use them later 23 | loss_function = "logreg" # perform the experiments with logistic regression 24 | EPS = 1e-4 25 | 26 | def train_test_classifier(): 27 | w = fdm.train_model_disp_mist(x_train, y_train, x_control_train, loss_function, EPS, cons_params) 28 | 29 | train_score, test_score, cov_all_train, cov_all_test, s_attr_to_fp_fn_train, s_attr_to_fp_fn_test = fdm.get_clf_stats(w, x_train, y_train, x_control_train, x_test, y_test, x_control_test, sensitive_attrs) 30 | 31 | 32 | # accuracy and FPR are for the test because we need of for plotting 33 | return w, test_score, s_attr_to_fp_fn_test 34 | 35 | 36 | """ Classify the data while optimizing for accuracy """ 37 | print 38 | print "== Unconstrained (original) classifier ==" 39 | w_uncons, acc_uncons, s_attr_to_fp_fn_test_uncons = train_test_classifier() 40 | print "\n-----------------------------------------------------------------------------------\n" 41 | 42 | """ Now classify such that we optimize for accuracy while achieving perfect fairness """ 43 | 44 | print 45 | print "== Classifier with fairness constraint ==" 46 | 47 | 48 | print "\n\n=== Constraints on FPR ===" # setting parameter for constraints 49 | cons_type = 1 # FPR constraint -- just change the cons_type, the rest of parameters should stay the same 50 | tau = 5.0 51 | mu = 1.2 52 | sensitive_attrs_to_cov_thresh = {"s1": {0:{0:0, 1:0}, 1:{0:0, 1:0}, 2:{0:0, 1:0}}} # zero covariance threshold, means try to get the fairest solution 53 | cons_params = {"cons_type": cons_type, 54 | "tau": tau, 55 | "mu": mu, 56 | "sensitive_attrs_to_cov_thresh": sensitive_attrs_to_cov_thresh} 57 | 58 | w_cons, acc_cons, s_attr_to_fp_fn_test_cons = train_test_classifier() 59 | psb.plot_boundaries(X, y, x_control, [w_uncons, w_cons], [acc_uncons, acc_cons], [s_attr_to_fp_fn_test_uncons["s1"], s_attr_to_fp_fn_test_cons["s1"]], "img/syn_cons_dtype_%d_cons_type_%d.png"%(data_type, cons_type) ) 60 | print "\n-----------------------------------------------------------------------------------\n" 61 | 62 | print "\n\n=== Constraints on FNR ===" 63 | cons_type = 2 64 | cons_params["cons_type"] = cons_type # FNR constraint -- just change the cons_type, the rest of parameters should stay the same 65 | w_cons, acc_cons, s_attr_to_fp_fn_test_cons = train_test_classifier() 66 | psb.plot_boundaries(X, y, x_control, [w_uncons, w_cons], [acc_uncons, acc_cons], [s_attr_to_fp_fn_test_uncons["s1"], s_attr_to_fp_fn_test_cons["s1"]], "img/syn_cons_dtype_%d_cons_type_%d.png"%(data_type, cons_type) ) 67 | print "\n-----------------------------------------------------------------------------------\n" 68 | 69 | 70 | 71 | print "\n\n=== Constraints on both FPR and FNR ===" 72 | cons_type = 4 73 | cons_params["cons_type"] = cons_type # both FPR and FNR constraints 74 | w_cons, acc_cons, s_attr_to_fp_fn_test_cons = train_test_classifier() 75 | psb.plot_boundaries(X, y, x_control, [w_uncons, w_cons], [acc_uncons, acc_cons], [s_attr_to_fp_fn_test_uncons["s1"], s_attr_to_fp_fn_test_cons["s1"]], "img/syn_cons_dtype_%d_cons_type_%d.png"%(data_type, cons_type) ) 76 | print "\n-----------------------------------------------------------------------------------\n" 77 | 78 | 79 | return 80 | 81 | 82 | def main(): 83 | test_synthetic_data() 84 | 85 | 86 | if __name__ == '__main__': 87 | main() -------------------------------------------------------------------------------- /disparate_mistreatment/synthetic_data_demo/fairness_acc_tradeoff.py: -------------------------------------------------------------------------------- 1 | import os,sys 2 | import numpy as np 3 | from generate_synthetic_data import * 4 | sys.path.insert(0, '../../fair_classification/') # the code for fair classification is in this directory 5 | import utils as ut 6 | import funcs_disp_mist as fdm 7 | import loss_funcs as lf # loss funcs that can be optimized subject to various constraints 8 | import plot_syn_boundaries as psb 9 | from copy import deepcopy 10 | import matplotlib.pyplot as plt # for plotting stuff 11 | 12 | def test_synthetic_data(): 13 | 14 | """ Generate the synthetic data """ 15 | data_type = 1 16 | X, y, x_control = generate_synthetic_data(data_type=data_type, plot_data=False) # set plot_data to False to skip the data plot 17 | sensitive_attrs = x_control.keys() 18 | 19 | """ Split the data into train and test """ 20 | train_fold_size = 0.5 21 | x_train, y_train, x_control_train, x_test, y_test, x_control_test = ut.split_into_train_test(X, y, x_control, train_fold_size) 22 | 23 | cons_params = None # constraint parameters, will use them later 24 | loss_function = "logreg" # perform the experiments with logistic regression 25 | EPS = 1e-4 26 | 27 | def train_test_classifier(): 28 | w = fdm.train_model_disp_mist(x_train, y_train, x_control_train, loss_function, EPS, cons_params) 29 | 30 | train_score, test_score, cov_all_train, cov_all_test, s_attr_to_fp_fn_train, s_attr_to_fp_fn_test = fdm.get_clf_stats(w, x_train, y_train, x_control_train, x_test, y_test, x_control_test, sensitive_attrs) 31 | 32 | 33 | # accuracy and FPR are for the test because we need of for plotting 34 | # the covariance is for train, because we need it for setting the thresholds 35 | return w, test_score, s_attr_to_fp_fn_test, cov_all_train 36 | 37 | 38 | """ Classify the data while optimizing for accuracy """ 39 | print 40 | print "== Unconstrained (original) classifier ==" 41 | w_uncons, acc_uncons, s_attr_to_fp_fn_test_uncons, cov_all_train_uncons = train_test_classifier() 42 | print "\n-----------------------------------------------------------------------------------\n" 43 | 44 | """ Now classify such that we optimize for accuracy while achieving perfect fairness """ 45 | 46 | print 47 | print "== Classifier with fairness constraint ==" 48 | 49 | it = 0.05 50 | mult_range = np.arange(1.0, 0.0-it, -it).tolist() 51 | 52 | 53 | acc_arr = [] 54 | fpr_per_group = {0:[], 1:[]} 55 | fnr_per_group = {0:[], 1:[]} 56 | 57 | cons_type = 1 # FPR constraint -- just change the cons_type, the rest of parameters should stay the same 58 | tau = 5.0 59 | mu = 1.2 60 | 61 | for m in mult_range: 62 | sensitive_attrs_to_cov_thresh = deepcopy(cov_all_train_uncons) 63 | for s_attr in sensitive_attrs_to_cov_thresh.keys(): 64 | for cov_type in sensitive_attrs_to_cov_thresh[s_attr].keys(): 65 | for s_val in sensitive_attrs_to_cov_thresh[s_attr][cov_type]: 66 | sensitive_attrs_to_cov_thresh[s_attr][cov_type][s_val] *= m 67 | 68 | 69 | cons_params = {"cons_type": cons_type, 70 | "tau": tau, 71 | "mu": mu, 72 | "sensitive_attrs_to_cov_thresh": sensitive_attrs_to_cov_thresh} 73 | 74 | w_cons, acc_cons, s_attr_to_fp_fn_test_cons, cov_all_train_cons = train_test_classifier() 75 | 76 | fpr_per_group[0].append(s_attr_to_fp_fn_test_cons["s1"][0.0]["fpr"]) 77 | fpr_per_group[1].append(s_attr_to_fp_fn_test_cons["s1"][1.0]["fpr"]) 78 | fnr_per_group[0].append(s_attr_to_fp_fn_test_cons["s1"][0.0]["fnr"]) 79 | fnr_per_group[1].append(s_attr_to_fp_fn_test_cons["s1"][1.0]["fnr"]) 80 | 81 | 82 | acc_arr.append(acc_cons) 83 | 84 | 85 | fs = 15 86 | 87 | ax = plt.subplot(2,1,1) 88 | plt.plot(mult_range, fpr_per_group[0], "-o" , color="green", label = "Group-0") 89 | plt.plot(mult_range, fpr_per_group[1], "-o", color="blue", label = "Group-1") 90 | ax.set_xlim([max(mult_range), min(mult_range) ]) 91 | plt.ylabel('False positive rate', fontsize=fs) 92 | ax.legend(fontsize=fs) 93 | 94 | 95 | ax = plt.subplot(2,1,2) 96 | plt.plot(mult_range, acc_arr, "-o" , color="green", label = "") 97 | ax.set_xlim([max(mult_range), min(mult_range) ]) 98 | plt.xlabel('Covariance multiplicative factor (m)', fontsize=fs) 99 | plt.ylabel('Accuracy', fontsize=fs) 100 | 101 | plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=0.5) 102 | plt.savefig("img/fairness_acc_tradeoff_cons_type_%d.png" % cons_type) 103 | plt.show() 104 | 105 | 106 | return 107 | 108 | 109 | def main(): 110 | test_synthetic_data() 111 | 112 | 113 | if __name__ == '__main__': 114 | main() -------------------------------------------------------------------------------- /disparate_mistreatment/synthetic_data_demo/generate_synthetic_data.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | import os,sys 3 | import math 4 | import numpy as np 5 | import matplotlib.pyplot as plt # for plotting stuff 6 | from random import seed, shuffle 7 | from scipy.stats import multivariate_normal # generating synthetic data 8 | from sklearn.linear_model import LogisticRegression 9 | SEED = 1122334455 10 | seed(SEED) # set the random seed so that the random permutations can be reproduced again 11 | np.random.seed(SEED) 12 | sys.path.insert(0, '../../fair_classification/') 13 | import utils as ut 14 | 15 | 16 | def generate_synthetic_data(data_type, plot_data=False): 17 | 18 | """ 19 | Code for generating the synthetic data. 20 | We will have two non-sensitive features and one sensitive feature. 21 | Non sensitive features will be drawn from a 2D gaussian distribution. 22 | Sensitive feature specifies the demographic group of the data point and can take values 0 and 1. 23 | 24 | The code will generate data such that a classifier optimizing for accuracy will lead to disparate misclassification rates for the two demographic groups. 25 | You can generate different data configurations using different values for the "data_type" parameter. 26 | """ 27 | 28 | n_samples = 1000 # generate these many data points per cluster 29 | 30 | def gen_gaussian_diff_size(mean_in, cov_in, z_val, class_label, n): 31 | """ 32 | mean_in: mean of the gaussian cluster 33 | cov_in: covariance matrix 34 | z_val: sensitive feature value 35 | class_label: +1 or -1 36 | n: number of points 37 | """ 38 | 39 | nv = multivariate_normal(mean = mean_in, cov = cov_in) 40 | X = nv.rvs(n) 41 | y = np.ones(n, dtype=float) * class_label 42 | z = np.ones(n, dtype=float) * z_val # all the points in this cluster get this value of the sensitive attribute 43 | 44 | return nv, X, y, z 45 | 46 | 47 | if data_type == 1: 48 | 49 | """ 50 | Generate data such that a classifier optimizing for accuracy will have disparate false positive rates as well as disparate false negative rates for both groups. 51 | """ 52 | 53 | 54 | cc = [[10,1], [1,4]] 55 | mu1, sigma1 = [2, 3], cc # z=1, + 56 | cc = [[5,2], [2,5]] 57 | mu2, sigma2 = [1, 2], cc # z=0, + 58 | 59 | cc = [[5, 1], [1, 5]] 60 | mu3, sigma3 = [-5,0], cc # z=1, - 61 | cc = [[7, 1], [1, 7]] 62 | mu4, sigma4 = [0,-1], cc # z=0, - 63 | 64 | nv1, X1, y1, z1 = gen_gaussian_diff_size(mu1, sigma1, 1, +1, int(n_samples * 1) ) # z=1, + 65 | nv2, X2, y2, z2 = gen_gaussian_diff_size(mu2, sigma2, 0, +1, int(n_samples * 1) ) # z=0, + 66 | nv3, X3, y3, z3 = gen_gaussian_diff_size(mu3, sigma3, 1, -1, int(n_samples * 1) ) # z=1, - 67 | nv4, X4, y4, z4 = gen_gaussian_diff_size(mu4, sigma4, 0, -1, int(n_samples * 1) ) # z=0, - 68 | 69 | elif data_type == 2: 70 | 71 | """ 72 | Generate data such that a classifier optimizing for accuracy will have disparate false positive rates for both groups but will have equal false negative rates. 73 | """ 74 | 75 | 76 | cc = [[3,1], [1,3]] 77 | mu1, sigma1 = [2, 2], cc # z=1, + 78 | mu2, sigma2 = [2, 2], cc # z=0, + 79 | 80 | mu3, sigma3 = [-2,-2], cc # z=1, - 81 | cc = [[3,3], [1,3]] 82 | mu4, sigma4 = [-1,0], cc # z=0, - 83 | 84 | nv1, X1, y1, z1 = gen_gaussian_diff_size(mu1, sigma1, 1, +1, int(n_samples * 1) ) # z=1, + 85 | nv2, X2, y2, z2 = gen_gaussian_diff_size(mu2, sigma2, 0, +1, int(n_samples * 1) ) # z=0, + 86 | nv3, X3, y3, z3 = gen_gaussian_diff_size(mu3, sigma3, 1, -1, int(n_samples * 1) ) # z=1, - 87 | nv4, X4, y4, z4 = gen_gaussian_diff_size(mu4, sigma4, 0, -1, int(n_samples * 1) ) # z=0, - 88 | 89 | 90 | 91 | # merge the clusters 92 | X = np.vstack((X1, X2, X3, X4)) 93 | y = np.hstack((y1, y2, y3, y4)) 94 | x_control = np.hstack((z1, z2, z3, z4)) 95 | 96 | # shuffle the data 97 | perm = range(len(X)) 98 | shuffle(perm) 99 | X = X[perm] 100 | y = y[perm] 101 | x_control = x_control[perm] 102 | 103 | 104 | """ Plot the data """ 105 | if plot_data: 106 | plt.figure() 107 | num_to_draw = 200 # we will only draw a small number of points to avoid clutter 108 | x_draw = X[:num_to_draw] 109 | y_draw = y[:num_to_draw] 110 | x_control_draw = x_control[:num_to_draw] 111 | 112 | X_s_0 = x_draw[x_control_draw == 0.0] 113 | X_s_1 = x_draw[x_control_draw == 1.0] 114 | y_s_0 = y_draw[x_control_draw == 0.0] 115 | y_s_1 = y_draw[x_control_draw == 1.0] 116 | 117 | plt.scatter(X_s_0[y_s_0==1.0][:, 0], X_s_0[y_s_0==1.0][:, 1], color='green', marker='x', s=60, linewidth=2, label= "group-0 +ve") 118 | plt.scatter(X_s_0[y_s_0==-1.0][:, 0], X_s_0[y_s_0==-1.0][:, 1], color='red', marker='x', s=60, linewidth=2, label = "group-0 -ve") 119 | plt.scatter(X_s_1[y_s_1==1.0][:, 0], X_s_1[y_s_1==1.0][:, 1], color='green', marker='o', facecolors='none', s=60, linewidth=2, label = "group-1 +ve") 120 | plt.scatter(X_s_1[y_s_1==-1.0][:, 0], X_s_1[y_s_1==-1.0][:, 1], color='red', marker='o', facecolors='none', s=60, linewidth=2, label = "group-1 -ve") 121 | 122 | 123 | plt.tick_params(axis='x', which='both', bottom='off', top='off', labelbottom='off') # dont need the ticks to see the data distribution 124 | plt.tick_params(axis='y', which='both', left='off', right='off', labelleft='off') 125 | plt.legend(loc=2, fontsize=21) 126 | plt.ylim((-8,12)) 127 | 128 | plt.savefig("img/data.png") 129 | plt.show() 130 | 131 | 132 | x_control = {"s1": x_control} # all the sensitive features are stored in a dictionary 133 | X = ut.add_intercept(X) 134 | 135 | 136 | return X,y,x_control 137 | 138 | -------------------------------------------------------------------------------- /disparate_mistreatment/synthetic_data_demo/img/data.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbilalzafar/fair-classification/45e85baad209134bbe2697c1a884dd1e2c2d9786/disparate_mistreatment/synthetic_data_demo/img/data.png -------------------------------------------------------------------------------- /disparate_mistreatment/synthetic_data_demo/img/fairness_acc_tradeoff_cons_type_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbilalzafar/fair-classification/45e85baad209134bbe2697c1a884dd1e2c2d9786/disparate_mistreatment/synthetic_data_demo/img/fairness_acc_tradeoff_cons_type_1.png -------------------------------------------------------------------------------- /disparate_mistreatment/synthetic_data_demo/img/syn_cons_dtype_1_cons_type_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbilalzafar/fair-classification/45e85baad209134bbe2697c1a884dd1e2c2d9786/disparate_mistreatment/synthetic_data_demo/img/syn_cons_dtype_1_cons_type_1.png -------------------------------------------------------------------------------- /disparate_mistreatment/synthetic_data_demo/img/syn_cons_dtype_1_cons_type_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbilalzafar/fair-classification/45e85baad209134bbe2697c1a884dd1e2c2d9786/disparate_mistreatment/synthetic_data_demo/img/syn_cons_dtype_1_cons_type_2.png -------------------------------------------------------------------------------- /disparate_mistreatment/synthetic_data_demo/img/syn_cons_dtype_1_cons_type_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbilalzafar/fair-classification/45e85baad209134bbe2697c1a884dd1e2c2d9786/disparate_mistreatment/synthetic_data_demo/img/syn_cons_dtype_1_cons_type_4.png -------------------------------------------------------------------------------- /disparate_mistreatment/synthetic_data_demo/plot_syn_boundaries.py: -------------------------------------------------------------------------------- 1 | import matplotlib 2 | import matplotlib.pyplot as plt # for plotting stuff 3 | import os 4 | 5 | matplotlib.rcParams['text.usetex'] = True 6 | matplotlib.rcParams.update({'figure.autolayout': True}) 7 | 8 | def get_line_coordinates(w, x1, x2): 9 | y1 = (-w[0] - (w[1] * x1)) / w[2] 10 | y2 = (-w[0] - (w[1] * x2)) / w[2] 11 | return y1,y2 12 | def plot_boundaries(X, y, x_control, w_arr, acc_arr, fp_fn_arr, fname): 13 | 14 | 15 | 16 | 17 | # print fp_fn_arr 18 | plt.figure() 19 | num_to_draw = 200 # we will only draw a small number of points to avoid clutter 20 | 21 | x_draw = X[:num_to_draw] 22 | y_draw = y[:num_to_draw] 23 | x_control_draw = x_control["s1"][:num_to_draw] 24 | 25 | X_s_0 = x_draw[x_control_draw == 0.0] 26 | X_s_1 = x_draw[x_control_draw == 1.0] 27 | y_s_0 = y_draw[x_control_draw == 0.0] 28 | y_s_1 = y_draw[x_control_draw == 1.0] 29 | plt.scatter(X_s_0[y_s_0==1.0][:, 1], X_s_0[y_s_0==1.0][:, 2], color='green', marker='x', s=60, linewidth=2) 30 | plt.scatter(X_s_0[y_s_0==-1.0][:, 1], X_s_0[y_s_0==-1.0][:, 2], color='red', marker='x', s=60, linewidth=2) 31 | plt.scatter(X_s_1[y_s_1==1.0][:, 1], X_s_1[y_s_1==1.0][:, 2], color='green', marker='o', facecolors='none', s=60, linewidth=2) 32 | plt.scatter(X_s_1[y_s_1==-1.0][:, 1], X_s_1[y_s_1==-1.0][:, 2], color='red', marker='o', facecolors='none', s=60, linewidth=2) 33 | 34 | 35 | assert(len(w_arr) == len(acc_arr)) 36 | assert(len(w_arr) == len(fp_fn_arr)) 37 | 38 | line_styles = ['c-', 'b--', 'k--', 'c--', 'b--'] 39 | 40 | 41 | for i in range(0,len(w_arr)): 42 | x1,x2 = max(x_draw[:,1])-2, min(x_draw[:,1]) 43 | y1,y2 = get_line_coordinates(w_arr[i], x1, x2) 44 | 45 | 46 | 47 | l = "Acc=%0.2f; FPR=%0.2f:%0.2f; FNR=%0.2f:%0.2f"%(acc_arr[i], fp_fn_arr[i][0.0]["fpr"], fp_fn_arr[i][1.0]["fpr"], fp_fn_arr[i][0.0]["fnr"], fp_fn_arr[i][1.0]["fnr"]) 48 | plt.plot([x1,x2], [y1,y2], line_styles[i], linewidth=5, label = l) 49 | 50 | 51 | 52 | 53 | plt.tick_params(axis='x', which='both', bottom='off', top='off', labelbottom='off') # dont need the ticks to see the data distribution 54 | plt.tick_params(axis='y', which='both', left='off', right='off', labelleft='off') 55 | plt.legend(loc=2, fontsize=21) 56 | 57 | plt.ylim((-8,12)) 58 | 59 | plt.savefig(fname) 60 | 61 | 62 | plt.show() -------------------------------------------------------------------------------- /fair_classification/funcs_disp_mist.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | import os,sys 3 | import traceback 4 | import numpy as np 5 | from random import seed, shuffle 6 | from collections import defaultdict 7 | from copy import deepcopy 8 | from cvxpy import * 9 | import dccp 10 | from dccp.problem import is_dccp 11 | import utils as ut 12 | 13 | SEED = 1122334455 14 | seed(SEED) # set the random seed so that the random permutations can be reproduced again 15 | np.random.seed(SEED) 16 | 17 | 18 | def train_model_disp_mist(x, y, x_control, loss_function, EPS, cons_params=None): 19 | 20 | # cons_type, sensitive_attrs_to_cov_thresh, take_initial_sol, gamma, tau, mu, EPS, cons_type 21 | """ 22 | 23 | Function that trains the model subject to various fairness constraints. 24 | If no constraints are given, then simply trains an unaltered classifier. 25 | Example usage in: "disparate_mistreatment/synthetic_data_demo/decision_boundary_demo.py" 26 | 27 | ---- 28 | 29 | Inputs: 30 | 31 | X: (n) x (d+1) numpy array -- n = number of examples, d = number of features, one feature is the intercept 32 | y: 1-d numpy array (n entries) 33 | x_control: dictionary of the type {"s": [...]}, key "s" is the sensitive feature name, and the value is a 1-d list with n elements holding the sensitive feature values 34 | loss_function: the loss function that we want to optimize -- for now we have implementation of logistic loss, but other functions like hinge loss can also be added 35 | EPS: stopping criteria for the convex solver. check the CVXPY documentation for details. default for CVXPY is 1e-6 36 | 37 | cons_params: is None when we do not want to apply any constraints 38 | otherwise: cons_params is a dict with keys as follows: 39 | - cons_type: 40 | - 0 for all misclassifications 41 | - 1 for FPR 42 | - 2 for FNR 43 | - 4 for both FPR and FNR 44 | - tau: DCCP parameter, controls how much weight to put on the constraints, if the constraints are not satisfied, then increase tau -- default is DCCP val 0.005 45 | - mu: DCCP parameter, controls the multiplicative factor by which the tau increases in each DCCP iteration -- default is the DCCP val 1.2 46 | - take_initial_sol: whether the starting point for DCCP should be the solution for the original (unconstrained) classifier -- default value is True 47 | - sensitive_attrs_to_cov_thresh: covariance threshold for each cons_type, eg, key 1 contains the FPR covariance 48 | ---- 49 | 50 | Outputs: 51 | 52 | w: the learned weight vector for the classifier 53 | 54 | """ 55 | 56 | max_iters = 100 # for the convex program 57 | max_iter_dccp = 50 # for the dccp algo 58 | 59 | 60 | num_points, num_features = x.shape 61 | w = Variable(num_features) # this is the weight vector 62 | 63 | # initialize a random value of w 64 | np.random.seed(112233) 65 | w.value = np.random.rand(x.shape[1]) 66 | 67 | if cons_params is None: # just train a simple classifier, no fairness constraints 68 | constraints = [] 69 | else: 70 | constraints = get_constraint_list_cov(x, y, x_control, cons_params["sensitive_attrs_to_cov_thresh"], cons_params["cons_type"], w) 71 | 72 | 73 | if loss_function == "logreg": 74 | # constructing the logistic loss problem 75 | loss = sum_entries( logistic( mul_elemwise(-y, x*w) ) ) / num_points # we are converting y to a diagonal matrix for consistent 76 | 77 | 78 | # sometimes, its a good idea to give a starting point to the constrained solver 79 | # this starting point for us is the solution to the unconstrained optimization problem 80 | # another option of starting point could be any feasible solution 81 | if cons_params is not None: 82 | if cons_params.get("take_initial_sol") is None: # true by default 83 | take_initial_sol = True 84 | elif cons_params["take_initial_sol"] == False: 85 | take_initial_sol = False 86 | 87 | if take_initial_sol == True: # get the initial solution 88 | p = Problem(Minimize(loss), []) 89 | p.solve() 90 | 91 | 92 | # construct the cvxpy problem 93 | prob = Problem(Minimize(loss), constraints) 94 | 95 | # print "\n\n" 96 | # print "Problem is DCP (disciplined convex program):", prob.is_dcp() 97 | # print "Problem is DCCP (disciplined convex-concave program):", is_dccp(prob) 98 | 99 | try: 100 | 101 | tau, mu = 0.005, 1.2 # default dccp parameters, need to be varied per dataset 102 | if cons_params is not None: # in case we passed these parameters as a part of dccp constraints 103 | if cons_params.get("tau") is not None: tau = cons_params["tau"] 104 | if cons_params.get("mu") is not None: mu = cons_params["mu"] 105 | 106 | prob.solve(method='dccp', tau=tau, mu=mu, tau_max=1e10, 107 | solver=ECOS, verbose=False, 108 | feastol=EPS, abstol=EPS, reltol=EPS,feastol_inacc=EPS, abstol_inacc=EPS, reltol_inacc=EPS, 109 | max_iters=max_iters, max_iter=max_iter_dccp) 110 | 111 | 112 | assert(prob.status == "Converged" or prob.status == "optimal") 113 | # print "Optimization done, problem status:", prob.status 114 | 115 | except: 116 | traceback.print_exc() 117 | sys.stdout.flush() 118 | sys.exit(1) 119 | 120 | 121 | # check that the fairness constraint is satisfied 122 | for f_c in constraints: 123 | assert(f_c.value == True) # can comment this out if the solver fails too often, but make sure that the constraints are satisfied empirically. alternatively, consider increasing tau parameter 124 | pass 125 | 126 | 127 | w = np.array(w.value).flatten() # flatten converts it to a 1d array 128 | 129 | 130 | return w 131 | 132 | 133 | def get_clf_stats(w, x_train, y_train, x_control_train, x_test, y_test, x_control_test, sensitive_attrs): 134 | 135 | 136 | 137 | 138 | assert(len(sensitive_attrs) == 1) # ensure that we have just one sensitive attribute 139 | s_attr = sensitive_attrs[0] # for now, lets compute the accuracy for just one sensitive attr 140 | 141 | 142 | # compute distance from boundary 143 | distances_boundary_train = get_distance_boundary(w, x_train, x_control_train[s_attr]) 144 | distances_boundary_test = get_distance_boundary(w, x_test, x_control_test[s_attr]) 145 | 146 | # compute the class labels 147 | all_class_labels_assigned_train = np.sign(distances_boundary_train) 148 | all_class_labels_assigned_test = np.sign(distances_boundary_test) 149 | 150 | 151 | train_score, test_score, correct_answers_train, correct_answers_test = ut.check_accuracy(None, x_train, y_train, x_test, y_test, all_class_labels_assigned_train, all_class_labels_assigned_test) 152 | 153 | 154 | cov_all_train = {} 155 | cov_all_test = {} 156 | for s_attr in sensitive_attrs: 157 | 158 | 159 | print_stats = False # we arent printing the stats for the train set to avoid clutter 160 | 161 | # uncomment these lines to print stats for the train fold 162 | # print "*** Train ***" 163 | # print "Accuracy: %0.3f" % (train_score) 164 | # print_stats = True 165 | s_attr_to_fp_fn_train = get_fpr_fnr_sensitive_features(y_train, all_class_labels_assigned_train, x_control_train, sensitive_attrs, print_stats) 166 | cov_all_train[s_attr] = get_sensitive_attr_constraint_fpr_fnr_cov(None, x_train, y_train, distances_boundary_train, x_control_train[s_attr]) 167 | 168 | 169 | print "\n" 170 | print "Accuracy: %0.3f" % (test_score) 171 | print_stats = True # only print stats for the test fold 172 | s_attr_to_fp_fn_test = get_fpr_fnr_sensitive_features(y_test, all_class_labels_assigned_test, x_control_test, sensitive_attrs, print_stats) 173 | cov_all_test[s_attr] = get_sensitive_attr_constraint_fpr_fnr_cov(None, x_test, y_test, distances_boundary_test, x_control_test[s_attr]) 174 | print "\n" 175 | 176 | return train_score, test_score, cov_all_train, cov_all_test, s_attr_to_fp_fn_train, s_attr_to_fp_fn_test 177 | 178 | def get_distance_boundary(w, x, s_attr_arr): 179 | 180 | """ 181 | if we have boundaries per group, then use those separate boundaries for each sensitive group 182 | else, use the same weight vector for everything 183 | """ 184 | 185 | distances_boundary = np.zeros(x.shape[0]) 186 | if isinstance(w, dict): # if we have separate weight vectors per group 187 | for k in w.keys(): # for each w corresponding to each sensitive group 188 | d = np.dot(x, w[k]) 189 | distances_boundary[s_attr_arr == k] = d[s_attr_arr == k] # set this distance only for people with this sensitive attr val 190 | else: # we just learn one w for everyone else 191 | distances_boundary = np.dot(x, w) 192 | return distances_boundary 193 | 194 | 195 | def get_constraint_list_cov(x_train, y_train, x_control_train, sensitive_attrs_to_cov_thresh, cons_type, w): 196 | 197 | """ 198 | get the list of constraints to be fed to the minimizer 199 | 200 | cons_type == 0: means the whole combined misclassification constraint (without FNR or FPR) 201 | cons_type == 1: FPR constraint 202 | cons_type == 2: FNR constraint 203 | cons_type == 4: both FPR as well as FNR constraints 204 | 205 | sensitive_attrs_to_cov_thresh: is a dict like {s: {cov_type: val}} 206 | s is the sensitive attr 207 | cov_type is the covariance type. contains the covariance for all misclassifications, FPR and for FNR etc 208 | """ 209 | 210 | constraints = [] 211 | for attr in sensitive_attrs_to_cov_thresh.keys(): 212 | 213 | attr_arr = x_control_train[attr] 214 | attr_arr_transformed, index_dict = ut.get_one_hot_encoding(attr_arr) 215 | 216 | if index_dict is None: # binary attribute, in this case, the attr_arr_transformed is the same as the attr_arr 217 | 218 | s_val_to_total = {ct:{} for ct in [0,1,2]} # constrain type -> sens_attr_val -> total number 219 | s_val_to_avg = {ct:{} for ct in [0,1,2]} 220 | cons_sum_dict = {ct:{} for ct in [0,1,2]} # sum of entities (females and males) in constraints are stored here 221 | 222 | for v in set(attr_arr): 223 | s_val_to_total[0][v] = sum(x_control_train[attr] == v) 224 | s_val_to_total[1][v] = sum(np.logical_and(x_control_train[attr] == v, y_train == -1)) # FPR constraint so we only consider the ground truth negative dataset for computing the covariance 225 | s_val_to_total[2][v] = sum(np.logical_and(x_control_train[attr] == v, y_train == +1)) 226 | 227 | 228 | for ct in [0,1,2]: 229 | s_val_to_avg[ct][0] = s_val_to_total[ct][1] / float(s_val_to_total[ct][0] + s_val_to_total[ct][1]) # N1/N in our formulation, differs from one constraint type to another 230 | s_val_to_avg[ct][1] = 1.0 - s_val_to_avg[ct][0] # N0/N 231 | 232 | 233 | for v in set(attr_arr): 234 | 235 | idx = x_control_train[attr] == v 236 | 237 | 238 | ################################################################# 239 | # #DCCP constraints 240 | dist_bound_prod = mul_elemwise(y_train[idx], x_train[idx] * w) # y.f(x) 241 | 242 | cons_sum_dict[0][v] = sum_entries( min_elemwise(0, dist_bound_prod) ) * (s_val_to_avg[0][v] / len(x_train)) # avg misclassification distance from boundary 243 | cons_sum_dict[1][v] = sum_entries( min_elemwise(0, mul_elemwise( (1 - y_train[idx])/2.0, dist_bound_prod) ) ) * (s_val_to_avg[1][v] / sum(y_train == -1)) # avg false positive distance from boundary (only operates on the ground truth neg dataset) 244 | cons_sum_dict[2][v] = sum_entries( min_elemwise(0, mul_elemwise( (1 + y_train[idx])/2.0, dist_bound_prod) ) ) * (s_val_to_avg[2][v] / sum(y_train == +1)) # avg false negative distance from boundary 245 | ################################################################# 246 | 247 | 248 | if cons_type == 4: 249 | cts = [1,2] 250 | elif cons_type in [0,1,2]: 251 | cts = [cons_type] 252 | 253 | else: 254 | raise Exception("Invalid constraint type") 255 | 256 | 257 | ################################################################# 258 | #DCCP constraints 259 | for ct in cts: 260 | thresh = abs(sensitive_attrs_to_cov_thresh[attr][ct][1] - sensitive_attrs_to_cov_thresh[attr][ct][0]) 261 | constraints.append( cons_sum_dict[ct][1] <= cons_sum_dict[ct][0] + thresh ) 262 | constraints.append( cons_sum_dict[ct][1] >= cons_sum_dict[ct][0] - thresh ) 263 | 264 | ################################################################# 265 | 266 | 267 | 268 | else: # otherwise, its a categorical attribute, so we need to set the cov thresh for each value separately 269 | # need to fill up this part 270 | raise Exception("Fill the constraint code for categorical sensitive features... Exiting...") 271 | sys.exit(1) 272 | 273 | 274 | return constraints 275 | 276 | 277 | def get_fpr_fnr_sensitive_features(y_true, y_pred, x_control, sensitive_attrs, verbose = False): 278 | 279 | 280 | 281 | # we will make some changes to x_control in this function, so make a copy in order to preserve the origianl referenced object 282 | x_control_internal = deepcopy(x_control) 283 | 284 | s_attr_to_fp_fn = {} 285 | 286 | for s in sensitive_attrs: 287 | s_attr_to_fp_fn[s] = {} 288 | s_attr_vals = x_control_internal[s] 289 | if verbose == True: 290 | print "|| s || FPR. || FNR. ||" 291 | for s_val in sorted(list(set(s_attr_vals))): 292 | s_attr_to_fp_fn[s][s_val] = {} 293 | y_true_local = y_true[s_attr_vals==s_val] 294 | y_pred_local = y_pred[s_attr_vals==s_val] 295 | 296 | 297 | 298 | acc = float(sum(y_true_local==y_pred_local)) / len(y_true_local) 299 | 300 | fp = sum(np.logical_and(y_true_local == -1.0, y_pred_local == +1.0)) # something which is -ve but is misclassified as +ve 301 | fn = sum(np.logical_and(y_true_local == +1.0, y_pred_local == -1.0)) # something which is +ve but is misclassified as -ve 302 | tp = sum(np.logical_and(y_true_local == +1.0, y_pred_local == +1.0)) # something which is +ve AND is correctly classified as +ve 303 | tn = sum(np.logical_and(y_true_local == -1.0, y_pred_local == -1.0)) # something which is -ve AND is correctly classified as -ve 304 | 305 | all_neg = sum(y_true_local == -1.0) 306 | all_pos = sum(y_true_local == +1.0) 307 | 308 | fpr = float(fp) / float(fp + tn) 309 | fnr = float(fn) / float(fn + tp) 310 | tpr = float(tp) / float(tp + fn) 311 | tnr = float(tn) / float(tn + fp) 312 | 313 | 314 | s_attr_to_fp_fn[s][s_val]["fp"] = fp 315 | s_attr_to_fp_fn[s][s_val]["fn"] = fn 316 | s_attr_to_fp_fn[s][s_val]["fpr"] = fpr 317 | s_attr_to_fp_fn[s][s_val]["fnr"] = fnr 318 | 319 | s_attr_to_fp_fn[s][s_val]["acc"] = (tp + tn) / (tp + tn + fp + fn) 320 | if verbose == True: 321 | if isinstance(s_val, float): # print the int value of the sensitive attr val 322 | s_val = int(s_val) 323 | print "|| %s || %0.2f || %0.2f ||" % (s_val, fpr, fnr) 324 | 325 | 326 | return s_attr_to_fp_fn 327 | 328 | 329 | def get_sensitive_attr_constraint_fpr_fnr_cov(model, x_arr, y_arr_true, y_arr_dist_boundary, x_control_arr, verbose=False): 330 | 331 | 332 | """ 333 | Here we compute the covariance between sensitive attr val and ONLY misclassification distances from boundary for False-positives 334 | (-N_1 / N) sum_0(min(0, y.f(x))) + (N_0 / N) sum_1(min(0, y.f(x))) for all misclassifications 335 | (-N_1 / N) sum_0(min(0, (1-y)/2 . y.f(x))) + (N_0 / N) sum_1(min(0, (1-y)/2. y.f(x))) for FPR 336 | 337 | y_arr_true are the true class labels 338 | y_arr_dist_boundary are the predicted distances from the decision boundary 339 | 340 | If the model is None, we assume that the y_arr_dist_boundary contains the distace from the decision boundary 341 | If the model is not None, we just compute a dot product or model and x_arr 342 | for the case of SVM, we pass the distace from bounday becase the intercept in internalized for the class 343 | and we have compute the distance using the project function 344 | 345 | 346 | this function will return -1 if the constraint specified by thresh parameter is not satifsified 347 | otherwise it will reutrn +1 348 | if the return value is >=0, then the constraint is satisfied 349 | """ 350 | 351 | 352 | assert(x_arr.shape[0] == x_control_arr.shape[0]) 353 | if len(x_control_arr.shape) > 1: # make sure we just have one column in the array 354 | assert(x_control_arr.shape[1] == 1) 355 | if len(set(x_control_arr)) != 2: # non binary attr 356 | raise Exception("Non binary attr, fix to handle non bin attrs") 357 | 358 | 359 | arr = [] 360 | if model is None: 361 | arr = y_arr_dist_boundary * y_arr_true # simply the output labels 362 | else: 363 | arr = np.dot(model, x_arr.T) * y_arr_true # the product with the weight vector -- the sign of this is the output label 364 | arr = np.array(arr) 365 | 366 | s_val_to_total = {ct:{} for ct in [0,1,2]} 367 | s_val_to_avg = {ct:{} for ct in [0,1,2]} 368 | cons_sum_dict = {ct:{} for ct in [0,1,2]} # sum of entities (females and males) in constraints are stored here 369 | 370 | for v in set(x_control_arr): 371 | s_val_to_total[0][v] = sum(x_control_arr == v) 372 | s_val_to_total[1][v] = sum(np.logical_and(x_control_arr == v, y_arr_true == -1)) 373 | s_val_to_total[2][v] = sum(np.logical_and(x_control_arr == v, y_arr_true == +1)) 374 | 375 | 376 | for ct in [0,1,2]: 377 | s_val_to_avg[ct][0] = s_val_to_total[ct][1] / float(s_val_to_total[ct][0] + s_val_to_total[ct][1]) # N1 / N 378 | s_val_to_avg[ct][1] = 1.0 - s_val_to_avg[ct][0] # N0 / N 379 | 380 | 381 | for v in set(x_control_arr): 382 | idx = x_control_arr == v 383 | dist_bound_prod = arr[idx] 384 | 385 | cons_sum_dict[0][v] = sum( np.minimum(0, dist_bound_prod) ) * (s_val_to_avg[0][v] / len(x_arr)) 386 | cons_sum_dict[1][v] = sum( np.minimum(0, ( (1 - y_arr_true[idx]) / 2 ) * dist_bound_prod) ) * (s_val_to_avg[1][v] / sum(y_arr_true == -1)) 387 | cons_sum_dict[2][v] = sum( np.minimum(0, ( (1 + y_arr_true[idx]) / 2 ) * dist_bound_prod) ) * (s_val_to_avg[2][v] / sum(y_arr_true == +1)) 388 | 389 | 390 | cons_type_to_name = {0:"ALL", 1:"FPR", 2:"FNR"} 391 | for cons_type in [0,1,2]: 392 | cov_type_name = cons_type_to_name[cons_type] 393 | cov = cons_sum_dict[cons_type][1] - cons_sum_dict[cons_type][0] 394 | if verbose == True: 395 | print "Covariance for type '%s' is: %0.7f" %(cov_type_name, cov) 396 | 397 | return cons_sum_dict 398 | 399 | 400 | def plot_fairness_acc_tradeoff(x_all, y_all, x_control_all, loss_function, cons_type): 401 | 402 | 403 | # very the covariance threshold using a range of decreasing multiplicative factors and see the tradeoffs between accuracy and fairness 404 | it = 0.2 405 | mult_range = np.arange(1.0, 0.0-it, -it).tolist() 406 | 407 | 408 | 409 | 410 | positive_class_label = 1 # positive class is +1 411 | test_acc = [] 412 | 413 | 414 | # first get the original values of covariance in the unconstrained classifier -- these original values are not needed for reverse constraint 415 | test_acc_arr, train_acc_arr, correlation_dict_test_arr, correlation_dict_train_arr, cov_dict_test_arr, cov_dict_train_arr = compute_cross_validation_error(x_all, y_all, x_control_all, num_folds, loss_function, 0, apply_accuracy_constraint, sep_constraint, sensitive_attrs, [{} for i in range(0,num_folds)], 0) 416 | 417 | for c in cov_range: 418 | print "LOG: testing for multiplicative factor: %0.2f" % c 419 | sensitive_attrs_to_cov_original_arr_multiplied = [] 420 | for sensitive_attrs_to_cov_original in cov_dict_train_arr: 421 | sensitive_attrs_to_cov_thresh = deepcopy(sensitive_attrs_to_cov_original) 422 | for k in sensitive_attrs_to_cov_thresh.keys(): 423 | v = sensitive_attrs_to_cov_thresh[k] 424 | if type(v) == type({}): 425 | for k1 in v.keys(): 426 | v[k1] = v[k1] * c 427 | else: 428 | sensitive_attrs_to_cov_thresh[k] = v * c 429 | sensitive_attrs_to_cov_original_arr_multiplied.append(sensitive_attrs_to_cov_thresh) 430 | 431 | 432 | test_acc_arr, train_acc_arr, correlation_dict_test_arr, correlation_dict_train_arr, cov_dict_test_arr, cov_dict_train_arr = compute_cross_validation_error(x_all, y_all, x_control_all, num_folds, loss_function, apply_fairness_constraints, apply_accuracy_constraint, sep_constraint, sensitive_attrs, sensitive_attrs_to_cov_original_arr_multiplied, c) 433 | test_acc.append(np.mean(test_acc_arr)) 434 | 435 | 436 | correlation_dict_train = get_avg_correlation_dict(correlation_dict_train_arr) 437 | correlation_dict_test = get_avg_correlation_dict(correlation_dict_test_arr) 438 | 439 | # just plot the correlations for the first sensitive attr, the plotting can be extended for the other values, but as a proof of concept, we will jsut show for one 440 | s = sensitive_attrs[0] 441 | 442 | for k,v in correlation_dict_test[s].items(): 443 | if v.get(positive_class_label) is None: 444 | positive_per_category[k].append(0.0) 445 | else: 446 | positive_per_category[k].append(v[positive_class_label]) 447 | 448 | positive_per_category = dict(positive_per_category) 449 | 450 | p_rule_arr = (np.array(positive_per_category[0]) / np.array(positive_per_category[1])) * 100.0 451 | 452 | 453 | ax = plt.subplot(2,1,1) 454 | plt.plot(cov_range, positive_per_category[0], "-o" , color="green", label = "Protected") 455 | plt.plot(cov_range, positive_per_category[1], "-o", color="blue", label = "Non-protected") 456 | ax.set_xlim([min(cov_range), max(cov_range)]) 457 | plt.xlabel('Multiplicative loss factor') 458 | plt.ylabel('Perc. in positive class') 459 | if apply_accuracy_constraint == False: 460 | plt.gca().invert_xaxis() 461 | plt.xlabel('Multiplicative covariance factor (c)') 462 | ax.legend() 463 | 464 | ax = plt.subplot(2,1,2) 465 | plt.scatter(p_rule_arr, test_acc, color="red") 466 | ax.set_xlim([min(p_rule_arr), max(max(p_rule_arr), 100)]) 467 | plt.xlabel('P% rule') 468 | plt.ylabel('Accuracy') 469 | 470 | plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=0.5) 471 | plt.show() 472 | 473 | 474 | -------------------------------------------------------------------------------- /fair_classification/linear_clf_pref_fairness.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | import os,sys 3 | import numpy as np 4 | import traceback 5 | 6 | sys.path.insert(0, "/home/mzafar/libraries/dccp") # we will store the latest version of DCCP here. 7 | from cvxpy import * 8 | import dccp 9 | from dccp.problem import is_dccp 10 | 11 | 12 | 13 | 14 | 15 | 16 | class LinearClf(): 17 | 18 | 19 | def __init__(self, loss_function, lam=None, train_multiple=False, random_state=1234): 20 | 21 | """ 22 | Model can be logistic regression or linear SVM in primal form 23 | 24 | We will define the lam parameter once and for all for a single object. 25 | For cross validating multiple models, we will write a function for doing that. 26 | 27 | """ 28 | 29 | 30 | """ Setting default lam val and Making sure that lam is provided for each group """ 31 | if lam is None: 32 | if train_multiple == False: 33 | lam = 0.0 34 | else: 35 | lam = {0:0.0, 1:0.0} 36 | 37 | else: 38 | if train_multiple == True: 39 | assert(isinstance(lam, dict)) 40 | 41 | 42 | self.loss_function = loss_function 43 | self.lam = lam 44 | self.train_multiple = train_multiple 45 | 46 | 47 | np.random.seed(random_state) 48 | 49 | 50 | def fit(self, X, y, x_sensitive, cons_params=None): 51 | 52 | """ 53 | X: n x d array 54 | y: n length vector 55 | x_sensitive: n length vector 56 | 57 | 58 | cons_params will be a dictionary 59 | cons_params["tau"], cons_params["mu"] and cons_params["EPS"] are the solver related parameters. Check DCCP documentation for details 60 | cons_params["cons_type"] specified which type of constraint to apply 61 | - cons_type = -1: No constraint 62 | - cons_type = 0: Parity 63 | - cons_type = 1: Preferred impact 64 | - cons_type = 2: Preferred treatment 65 | - cons_type = 3: Preferred both 66 | 67 | cons_params["s_val_to_cons_sum"]: The ramp approximation -- only needed for cons_type 1 and 3 68 | """ 69 | 70 | 71 | """ 72 | Setting up the initial variables 73 | """ 74 | 75 | max_iters = 100 # for CVXPY convex solver 76 | max_iter_dccp = 50 # for the dccp. notice that DCCP hauristic runs the convex program iteratively until arriving at the solution 77 | 78 | 79 | 80 | 81 | """ 82 | Construct the optimization variables 83 | """ 84 | 85 | constraints = [] 86 | 87 | np.random.seed(1234) # set the seed before initializing the values of w 88 | if self.train_multiple == True: 89 | w = {} 90 | for k in set(x_sensitive): 91 | w[k] = Variable(X.shape[1]) # this is the weight vector 92 | w[k].value = np.random.rand(X.shape[1]) # initialize the value of w -- uniform distribution over [0,1] 93 | else: 94 | w = Variable(X.shape[1]) # this is the weight vector 95 | w.value = np.random.rand(X.shape[1]) 96 | 97 | 98 | 99 | """ 100 | Solve the optimization problem here 101 | """ 102 | 103 | num_all = X.shape[0] # set of all data points 104 | 105 | if self.train_multiple == True: 106 | 107 | obj = 0 108 | for k in set(x_sensitive): 109 | idx = x_sensitive==k 110 | X_k = X[idx] 111 | y_k = y[idx] 112 | obj += sum_squares(w[k][1:]) * self.lam[k] # first term in w is the intercept, so no need to regularize that 113 | 114 | if self.loss_function == "logreg": 115 | obj += sum_entries( logistic( mul_elemwise(-y_k, X_k*w[k]) ) ) / num_all # notice that we are dividing by the length of the whole dataset, and not just of this sensitive group. this way, the group that has more people contributes more to the loss 116 | 117 | elif self.loss_function == "svm_linear": 118 | obj += sum_entries ( max_elemwise (0, 1 - mul_elemwise ( y_k, X_k*w[k])) ) / num_all 119 | 120 | else: 121 | raise Exception("Invalid loss function") 122 | 123 | else: 124 | 125 | obj = 0 126 | obj += sum_squares(w[1:]) * self.lam # regularizer -- first term in w is the intercept, so no need to regularize that 127 | if self.loss_function == "logreg": 128 | obj += sum_entries( logistic( mul_elemwise(-y, X*w) ) ) / num_all 129 | elif self.loss_function == "svm_linear": 130 | obj += sum_entries ( max_elemwise (0, 1 - mul_elemwise ( y, X*w)) ) / num_all 131 | else: 132 | raise Exception("Invalid loss function") 133 | 134 | 135 | """ 136 | Constraints here 137 | """ 138 | if cons_params is not None: 139 | 140 | cons_type = cons_params["cons_type"] 141 | if cons_type == -1: # no constraint 142 | pass 143 | elif cons_type == 0: # disp imp with single boundary 144 | cov_thresh = np.abs(0.) # perfect fairness -- see our AISTATS paper for details 145 | constraints += self.get_di_cons_single_boundary(X, y, x_sensitive, w, cov_thresh) 146 | elif cons_type in [1,3]: # preferred imp, pref imp + pref treat 147 | constraints += self.get_preferred_cons(X, x_sensitive, w, cons_type, cons_params["s_val_to_cons_sum"]) 148 | elif cons_type == 2: 149 | constraints += self.get_preferred_cons(X, x_sensitive, w, cons_type) 150 | else: 151 | raise Exception("Wrong constraint type") 152 | 153 | 154 | 155 | prob = Problem(Minimize(obj), constraints) 156 | # print "Problem is DCP (disciplined convex program):", prob.is_dcp() 157 | # print "Problem is DCCP (disciplined convex-concave program):", is_dccp(prob) 158 | 159 | 160 | """ 161 | Solving the problem 162 | """ 163 | 164 | try: 165 | 166 | tau, mu, EPS = 0.5, 1.2, 1e-4 # default dccp parameters, need to be varied per dataset 167 | if cons_params is not None: # in case we passed these parameters as a part of dccp constraints 168 | if cons_params.get("tau") is not None: tau = cons_params["tau"] 169 | if cons_params.get("mu") is not None: mu = cons_params["mu"] 170 | if cons_params.get("EPS") is not None: EPS = cons_params["EPS"] 171 | 172 | prob.solve(method='dccp', tau=tau, mu=mu, tau_max=1e10, 173 | verbose=False, 174 | feastol=EPS, abstol=EPS, reltol=EPS,feastol_inacc=EPS, abstol_inacc=EPS, reltol_inacc=EPS, 175 | max_iters=max_iters, max_iter=max_iter_dccp) 176 | 177 | 178 | # print "Optimization done, problem status:", prob.status 179 | assert(prob.status == "Converged" or prob.status == "optimal") 180 | 181 | 182 | # check that the fairness constraint is satisfied 183 | for f_c in constraints: 184 | try: 185 | assert(f_c.value == True) 186 | except: 187 | print "Assertion failed. Fairness constraints not satisfied." 188 | print traceback.print_exc() 189 | sys.stdout.flush() 190 | return 191 | # sys.exit(1) 192 | 193 | except: 194 | traceback.print_exc() 195 | sys.stdout.flush() 196 | # sys.exit(1) 197 | return 198 | 199 | 200 | """ 201 | Storing the results 202 | """ 203 | 204 | if self.train_multiple == True: 205 | self.w = {} 206 | for k in set(x_sensitive): 207 | self.w[k] = np.array(w[k].value).flatten() # flatten converts it to a 1d array 208 | else: 209 | self.w = np.array(w.value).flatten() # flatten converts it to a 1d array 210 | 211 | 212 | return self.w 213 | 214 | 215 | def decision_function(self, X, k=None): 216 | """ Predicts labels for all samples in X 217 | 218 | Parameters 219 | ---------- 220 | X : array-like of shape = [n_samples, n_features] 221 | The input samples. 222 | Returns 223 | 224 | k: the group whose decision boundary should be used. 225 | k = None means that we trained one clf for the whole dataset 226 | ------- 227 | y : array of shape = [n_samples] 228 | """ 229 | 230 | if k is None: 231 | ret = np.dot(X, self.w) 232 | else: 233 | ret = np.dot(X, self.w[k]) 234 | 235 | return ret 236 | 237 | 238 | def get_distance_boundary(self, X, x_sensitive): 239 | 240 | """ 241 | returns two vals 242 | 243 | distance_boundary_arr: 244 | arr with distance to boundary, each groups owns w is applied on it 245 | distance_boundary_dict: 246 | dict of the form s_attr_group (points from group 0/1) -> w_group (boundary of group 0/1) -> distances for this group with this boundary 247 | 248 | """ 249 | 250 | distances_boundary_dict = {} # s_attr_group (0/1) -> w_group (0/1) -> distances 251 | 252 | 253 | if not isinstance(self.w, dict): # we have one model for the whole data 254 | distance_boundary_arr = self.decision_function(X) 255 | 256 | for attr in set(x_sensitive): # there is only one boundary, so the results with this_group and other_group boundaries are the same 257 | 258 | distances_boundary_dict[attr] = {} 259 | idx = x_sensitive == attr 260 | 261 | for k in set(x_sensitive): 262 | distances_boundary_dict[attr][k] = self.decision_function(X[idx]) # apply same decision function for all the sensitive attrs because same w is trained for everyone 263 | 264 | 265 | else: # w is a dict 266 | distance_boundary_arr = np.zeros(X.shape[0]) 267 | 268 | 269 | for attr in set(x_sensitive): 270 | 271 | distances_boundary_dict[attr] = {} 272 | idx = x_sensitive == attr 273 | X_g = X[idx] 274 | 275 | distance_boundary_arr[idx] = self.decision_function(X_g, attr) # each group gets decision with their own boundary 276 | 277 | for k in self.w.keys(): 278 | distances_boundary_dict[attr][k] = self.decision_function(X_g, k) # each group gets a decision with both boundaries 279 | 280 | return distance_boundary_arr, distances_boundary_dict 281 | 282 | 283 | def get_di_cons_single_boundary(self, X, y, x_sensitive, w, cov_thresh): 284 | 285 | """ 286 | Parity impact constraint 287 | """ 288 | 289 | assert(self.train_multiple == False) # di cons is just for a single boundary clf 290 | assert(cov_thresh >= 0) # covariance thresh has to be a small positive number 291 | 292 | constraints = [] 293 | z_i_z_bar = x_sensitive - np.mean(x_sensitive) 294 | 295 | fx = X*w 296 | prod = sum_entries( mul_elemwise(z_i_z_bar, fx) ) / X.shape[0] 297 | 298 | 299 | constraints.append( prod <= cov_thresh ) 300 | constraints.append( prod >= -cov_thresh ) 301 | 302 | return constraints 303 | 304 | 305 | def get_preferred_cons(self, X, x_sensitive, w, cons_type, s_val_to_cons_sum=None): 306 | 307 | """ 308 | No need to pass s_val_to_cons_sum for preferred treatment (envy free) constraints 309 | 310 | For details on cons_type, see the documentation of fit() function 311 | """ 312 | 313 | constraints = [] 314 | 315 | if cons_type in [1,2,3]: # 1 - pref imp, 2 - EF, 3 - pref imp & EF 316 | 317 | prod_dict = {0:{}, 1:{}} # s_attr_group (0/1) -> w_group (0/1) -> val 318 | for val in set(x_sensitive): 319 | idx = x_sensitive == val 320 | X_g = X[idx] 321 | num_g = X_g.shape[0] 322 | 323 | for k in w.keys(): # get the distance with each group's w 324 | prod_dict[val][k] = sum_entries( max_elemwise(0, X_g*w[k]) ) / num_g 325 | 326 | 327 | 328 | else: 329 | raise Exception("Invalid constraint type") 330 | 331 | 332 | if cons_type == 1 or cons_type == 3: # 1 for preferred impact -- 3 for preferred impact and envy free 333 | 334 | constraints.append( prod_dict[0][0] >= s_val_to_cons_sum[0][0] ) 335 | constraints.append( prod_dict[1][1] >= s_val_to_cons_sum[1][1] ) 336 | 337 | 338 | 339 | if cons_type == 2 or cons_type == 3: # envy free 340 | constraints.append( prod_dict[0][0] >= prod_dict[0][1] ) 341 | constraints.append( prod_dict[1][1] >= prod_dict[1][0] ) 342 | 343 | return constraints 344 | -------------------------------------------------------------------------------- /fair_classification/loss_funcs.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import os 3 | import numpy as np 4 | import scipy.special 5 | from collections import defaultdict 6 | import traceback 7 | from copy import deepcopy 8 | 9 | 10 | 11 | def _hinge_loss(w, X, y): 12 | 13 | 14 | yz = y * np.dot(X,w) # y * (x.w) 15 | yz = np.maximum(np.zeros_like(yz), (1-yz)) # hinge function 16 | 17 | return sum(yz) 18 | 19 | def _logistic_loss(w, X, y, return_arr=None): 20 | """Computes the logistic loss. 21 | 22 | This function is used from scikit-learn source code 23 | 24 | Parameters 25 | ---------- 26 | w : ndarray, shape (n_features,) or (n_features + 1,) 27 | Coefficient vector. 28 | 29 | X : {array-like, sparse matrix}, shape (n_samples, n_features) 30 | Training data. 31 | 32 | y : ndarray, shape (n_samples,) 33 | Array of labels. 34 | 35 | """ 36 | 37 | 38 | yz = y * np.dot(X,w) 39 | # Logistic loss is the negative of the log of the logistic function. 40 | if return_arr == True: 41 | out = -(log_logistic(yz)) 42 | else: 43 | out = -np.sum(log_logistic(yz)) 44 | return out 45 | 46 | def _logistic_loss_l2_reg(w, X, y, lam=None): 47 | 48 | if lam is None: 49 | lam = 1.0 50 | 51 | yz = y * np.dot(X,w) 52 | # Logistic loss is the negative of the log of the logistic function. 53 | logistic_loss = -np.sum(log_logistic(yz)) 54 | l2_reg = (float(lam)/2.0) * np.sum([elem*elem for elem in w]) 55 | out = logistic_loss + l2_reg 56 | return out 57 | 58 | 59 | def log_logistic(X): 60 | 61 | """ This function is used from scikit-learn source code. Source link below """ 62 | 63 | """Compute the log of the logistic function, ``log(1 / (1 + e ** -x))``. 64 | This implementation is numerically stable because it splits positive and 65 | negative values:: 66 | -log(1 + exp(-x_i)) if x_i > 0 67 | x_i - log(1 + exp(x_i)) if x_i <= 0 68 | 69 | Parameters 70 | ---------- 71 | X: array-like, shape (M, N) 72 | Argument to the logistic function 73 | 74 | Returns 75 | ------- 76 | out: array, shape (M, N) 77 | Log of the logistic function evaluated at every point in x 78 | Notes 79 | ----- 80 | Source code at: 81 | https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/utils/extmath.py 82 | ----- 83 | 84 | See the blog post describing this implementation: 85 | http://fa.bianp.net/blog/2013/numerical-optimizers-for-logistic-regression/ 86 | """ 87 | if X.ndim > 1: raise Exception("Array of samples cannot be more than 1-D!") 88 | out = np.empty_like(X) # same dimensions and data types 89 | 90 | idx = X>0 91 | out[idx] = -np.log(1.0 + np.exp(-X[idx])) 92 | out[~idx] = X[~idx] - np.log(1.0 + np.exp(X[~idx])) 93 | return out 94 | 95 | -------------------------------------------------------------------------------- /fair_classification/stats_pref_fairness.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | import numpy as np 3 | from sklearn.preprocessing import MaxAbsScaler # normalize data with 0 and 1 as min/max absolute vals 4 | import scipy 5 | from multiprocessing import Pool, Process, Queue 6 | from sklearn.metrics import roc_auc_score 7 | import traceback 8 | 9 | 10 | def get_acc_all(dist_arr, y): 11 | """ 12 | Get accuracy for all data points 13 | Each group gets the prediction based on their own boundary 14 | """ 15 | return np.sum(sign_bin_clf(dist_arr) == y) / y.shape[0] 16 | 17 | def get_clf_stats(dist_arr, dist_dict, y, x_sensitive, print_stats=False): 18 | 19 | 20 | # compute the class labels 21 | all_class_labels_assigned = sign_bin_clf(dist_arr) 22 | 23 | 24 | s_val_to_cons_sum = {} 25 | 26 | 27 | acc = get_acc_all(dist_arr,y) 28 | 29 | if print_stats: 30 | print "\n\n\nAccuracy: %0.3f\n" % acc 31 | 32 | 33 | acc_stats = get_acc_stats(dist_dict, y, x_sensitive, print_stats) 34 | s_val_to_cons_sum = get_sensitive_attr_cov(dist_dict) 35 | 36 | return acc, s_val_to_cons_sum, acc_stats 37 | 38 | 39 | def get_fp_fn_tp_tn(y_true, y_pred): 40 | 41 | 42 | def check_labels_bin(arr): 43 | 44 | """ Can only have -1 and 1""" 45 | try: 46 | if len(set(arr)) == 1: 47 | elem = list(set(arr))[0] 48 | assert(elem==1 or elem==-1) 49 | else: 50 | assert(len(set(arr)) == 2) 51 | assert( sorted(list(set(arr)))[0] == -1 and sorted(list(set(arr)))[1] == 1 ) 52 | except: 53 | traceback.print_exc() 54 | raise Exception("Class labels (both true and predicted) can only take values -1 and 1... Exiting...") 55 | 56 | 57 | check_labels_bin(y_true) 58 | check_labels_bin(y_pred) 59 | 60 | 61 | 62 | acc = float(sum(y_true==y_pred)) / len(y_true) 63 | 64 | fp = sum(np.logical_and(y_true == -1.0, y_pred == +1.0)) # something which is -ve but is misclassified as +ve 65 | fn = sum(np.logical_and(y_true == +1.0, y_pred == -1.0)) # something which is +ve but is misclassified as -ve 66 | tp = sum(np.logical_and(y_true == +1.0, y_pred == +1.0)) # something which is +ve AND is correctly classified as +ve 67 | tn = sum(np.logical_and(y_true == -1.0, y_pred == -1.0)) # something which is -ve AND is correctly classified as -ve 68 | 69 | fpr = float(fp) / float(fp + tn) 70 | fnr = float(fn) / float(fn + tp) 71 | tpr = float(tp) / float(tp + fn) 72 | tnr = float(tn) / float(tn + fp) 73 | frac_pos = (tp + fp) / (tp + tn + fp + fn) # fraction classified as positive 74 | 75 | out_dict = {"fpr": fpr, "fnr": fnr, "acc": acc, "frac_pos": frac_pos} 76 | 77 | return out_dict 78 | 79 | 80 | 81 | def get_acc_stats(dist_dict, y, x_sensitive, verbose = False): 82 | 83 | 84 | """ 85 | output dict form: s_attr_group (0/1) -> w_group (0/1) -> fpr/fnr/acc/frac_pos 86 | """ 87 | 88 | acc_stats = {} 89 | 90 | try: 91 | assert(len(set(x_sensitive)) == 2) 92 | except: 93 | raise Exception("Fill the constraint code for categorical sensitive features... Exiting...") 94 | 95 | try: 96 | assert( sorted(list(set(x_sensitive)))[0] == 0 and sorted(list(set(x_sensitive)))[1] == 1 ) 97 | except: 98 | raise Exception("Sensitive feature can only take values 0 and 1... Exiting...") 99 | 100 | 101 | 102 | 103 | if verbose == True: 104 | print "|| s || frac_pos ||" 105 | 106 | 107 | for s_val in set(x_sensitive): 108 | idx = x_sensitive == s_val 109 | other_val = np.abs(1-s_val) 110 | acc_stats[s_val] = {} 111 | 112 | 113 | y_true_local = y[idx] 114 | y_pred_local = sign_bin_clf(dist_dict[s_val][s_val]) # predictions with this classifier 115 | y_pred_local_other = sign_bin_clf(dist_dict[s_val][other_val]) # predictions with other group's classifier 116 | 117 | 118 | assert(y_true_local.shape[0] == y_pred_local.shape[0] and y_true_local.shape[0] == y_pred_local_other.shape[0]) 119 | 120 | 121 | acc_stats[s_val][s_val] = get_fp_fn_tp_tn(y_true_local, y_pred_local) 122 | acc_stats[s_val][other_val] = get_fp_fn_tp_tn(y_true_local, y_pred_local_other) 123 | 124 | 125 | if verbose == True: 126 | if isinstance(s_val, float): # print the int value of the sensitive attr val 127 | s_val = int(s_val) 128 | 129 | 130 | print "|| %s || %0.2f (%0.2f) ||" % (s_val, acc_stats[s_val][s_val]["frac_pos"], acc_stats[s_val][other_val]["frac_pos"]) 131 | 132 | 133 | 134 | return acc_stats 135 | 136 | 137 | 138 | def sign_bin_clf(arr): 139 | """ 140 | prediction for a linear classifier. np.sign gives 0 for sing(0), we want 1 141 | 142 | if arr[i] >= 0, arr[i] = +1 143 | else arr[i] = -1 144 | 145 | """ 146 | arr = np.sign(arr) 147 | arr[arr==0] = 1 148 | return arr 149 | 150 | 151 | def get_sensitive_attr_cov(dist_dict): 152 | 153 | """ 154 | computes the ramp function for each group to estimate the acceptance rate 155 | """ 156 | 157 | s_val_to_cons_sum = {0:{}, 1:{}} # s_attr_group (0/1) -> w_group (0/1) -> ramp approx 158 | 159 | for s_val in dist_dict.keys(): 160 | for w_group in dist_dict[s_val].keys(): 161 | fx = dist_dict[s_val][w_group] 162 | s_val_to_cons_sum[s_val][w_group] = np.sum( np.maximum(0, fx) ) / fx.shape[0] 163 | 164 | 165 | return s_val_to_cons_sum 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | def add_intercept(x): 174 | 175 | """ Add intercept to the data before linear classification """ 176 | m,n = x.shape 177 | intercept = np.ones(m).reshape(m, 1) # the constant b 178 | return np.concatenate((intercept, x), axis = 1) 179 | 180 | 181 | 182 | def scale_data(x_train, x_test): 183 | 184 | """ 185 | We only scale the continuous features. No need to scale binary features 186 | """ 187 | 188 | 189 | idx_binary = [] # columns with boolean values 190 | for k in range(x_train.shape[1]): 191 | idx_binary.append( np.array_equal(x_train[:,k], x_train[:,k].astype(bool)) ) # checking if a column is binary 192 | idx_cont = np.logical_not(idx_binary) 193 | 194 | 195 | sc = MaxAbsScaler() 196 | sc.fit(x_train[:, idx_cont]) 197 | 198 | x_train[:, idx_cont] = sc.transform(x_train[:, idx_cont]) 199 | x_test[:, idx_cont] = sc.transform(x_test[:, idx_cont]) 200 | 201 | return 202 | 203 | -------------------------------------------------------------------------------- /fair_classification/utils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from random import seed, shuffle 3 | import loss_funcs as lf # our implementation of loss funcs 4 | from scipy.optimize import minimize # for loss func minimization 5 | from multiprocessing import Pool, Process, Queue 6 | from collections import defaultdict 7 | from copy import deepcopy 8 | import matplotlib.pyplot as plt # for plotting stuff 9 | import sys 10 | 11 | SEED = 1122334455 12 | seed(SEED) # set the random seed so that the random permutations can be reproduced again 13 | np.random.seed(SEED) 14 | 15 | 16 | 17 | 18 | def train_model(x, y, x_control, loss_function, apply_fairness_constraints, apply_accuracy_constraint, sep_constraint, sensitive_attrs, sensitive_attrs_to_cov_thresh, gamma=None): 19 | 20 | """ 21 | 22 | Function that trains the model subject to various fairness constraints. 23 | If no constraints are given, then simply trains an unaltered classifier. 24 | Example usage in: "synthetic_data_demo/decision_boundary_demo.py" 25 | 26 | ---- 27 | 28 | Inputs: 29 | 30 | X: (n) x (d+1) numpy array -- n = number of examples, d = number of features, one feature is the intercept 31 | y: 1-d numpy array (n entries) 32 | x_control: dictionary of the type {"s": [...]}, key "s" is the sensitive feature name, and the value is a 1-d list with n elements holding the sensitive feature values 33 | loss_function: the loss function that we want to optimize -- for now we have implementation of logistic loss, but other functions like hinge loss can also be added 34 | apply_fairness_constraints: optimize accuracy subject to fairness constraint (0/1 values) 35 | apply_accuracy_constraint: optimize fairness subject to accuracy constraint (0/1 values) 36 | sep_constraint: apply the fine grained accuracy constraint 37 | for details, see Section 3.3 of arxiv.org/abs/1507.05259v3 38 | For examples on how to apply these constraints, see "synthetic_data_demo/decision_boundary_demo.py" 39 | Note: both apply_fairness_constraints and apply_accuracy_constraint cannot be 1 at the same time 40 | sensitive_attrs: ["s1", "s2", ...], list of sensitive features for which to apply fairness constraint, all of these sensitive features should have a corresponding array in x_control 41 | sensitive_attrs_to_cov_thresh: the covariance threshold that the classifier should achieve (this is only needed when apply_fairness_constraints=1, not needed for the other two constraints) 42 | gamma: controls the loss in accuracy we are willing to incur when using apply_accuracy_constraint and sep_constraint 43 | 44 | ---- 45 | 46 | Outputs: 47 | 48 | w: the learned weight vector for the classifier 49 | 50 | """ 51 | 52 | 53 | assert((apply_accuracy_constraint == 1 and apply_fairness_constraints == 1) == False) # both constraints cannot be applied at the same time 54 | 55 | max_iter = 100000 # maximum number of iterations for the minimization algorithm 56 | 57 | if apply_fairness_constraints == 0: 58 | constraints = [] 59 | else: 60 | constraints = get_constraint_list_cov(x, y, x_control, sensitive_attrs, sensitive_attrs_to_cov_thresh) 61 | 62 | if apply_accuracy_constraint == 0: #its not the reverse problem, just train w with cross cov constraints 63 | 64 | f_args=(x, y) 65 | w = minimize(fun = loss_function, 66 | x0 = np.random.rand(x.shape[1],), 67 | args = f_args, 68 | method = 'SLSQP', 69 | options = {"maxiter":max_iter}, 70 | constraints = constraints 71 | ) 72 | 73 | else: 74 | 75 | # train on just the loss function 76 | w = minimize(fun = loss_function, 77 | x0 = np.random.rand(x.shape[1],), 78 | args = (x, y), 79 | method = 'SLSQP', 80 | options = {"maxiter":max_iter}, 81 | constraints = [] 82 | ) 83 | 84 | old_w = deepcopy(w.x) 85 | 86 | 87 | def constraint_gamma_all(w, x, y, initial_loss_arr): 88 | 89 | gamma_arr = np.ones_like(y) * gamma # set gamma for everyone 90 | new_loss = loss_function(w, x, y) 91 | old_loss = sum(initial_loss_arr) 92 | return ((1.0 + gamma) * old_loss) - new_loss 93 | 94 | def constraint_protected_people(w,x,y): # dont confuse the protected here with the sensitive feature protected/non-protected values -- protected here means that these points should not be misclassified to negative class 95 | return np.dot(w, x.T) # if this is positive, the constraint is satisfied 96 | def constraint_unprotected_people(w,ind,old_loss,x,y): 97 | 98 | new_loss = loss_function(w, np.array([x]), np.array(y)) 99 | return ((1.0 + gamma) * old_loss) - new_loss 100 | 101 | constraints = [] 102 | predicted_labels = np.sign(np.dot(w.x, x.T)) 103 | unconstrained_loss_arr = loss_function(w.x, x, y, return_arr=True) 104 | 105 | if sep_constraint == True: # separate gemma for different people 106 | for i in range(0, len(predicted_labels)): 107 | if predicted_labels[i] == 1.0 and x_control[sensitive_attrs[0]][i] == 1.0: # for now we are assuming just one sensitive attr for reverse constraint, later, extend the code to take into account multiple sensitive attrs 108 | c = ({'type': 'ineq', 'fun': constraint_protected_people, 'args':(x[i], y[i])}) # this constraint makes sure that these people stay in the positive class even in the modified classifier 109 | constraints.append(c) 110 | else: 111 | c = ({'type': 'ineq', 'fun': constraint_unprotected_people, 'args':(i, unconstrained_loss_arr[i], x[i], y[i])}) 112 | constraints.append(c) 113 | else: # same gamma for everyone 114 | c = ({'type': 'ineq', 'fun': constraint_gamma_all, 'args':(x,y,unconstrained_loss_arr)}) 115 | constraints.append(c) 116 | 117 | def cross_cov_abs_optm_func(weight_vec, x_in, x_control_in_arr): 118 | cross_cov = (x_control_in_arr - np.mean(x_control_in_arr)) * np.dot(weight_vec, x_in.T) 119 | return float(abs(sum(cross_cov))) / float(x_in.shape[0]) 120 | 121 | 122 | w = minimize(fun = cross_cov_abs_optm_func, 123 | x0 = old_w, 124 | args = (x, x_control[sensitive_attrs[0]]), 125 | method = 'SLSQP', 126 | options = {"maxiter":100000}, 127 | constraints = constraints 128 | ) 129 | 130 | try: 131 | assert(w.success == True) 132 | except: 133 | print "Optimization problem did not converge.. Check the solution returned by the optimizer." 134 | print "Returned solution is:" 135 | print w 136 | 137 | 138 | 139 | return w.x 140 | 141 | 142 | def compute_cross_validation_error(x_all, y_all, x_control_all, num_folds, loss_function, apply_fairness_constraints, apply_accuracy_constraint, sep_constraint, sensitive_attrs, sensitive_attrs_to_cov_thresh_arr, gamma=None): 143 | 144 | 145 | """ 146 | Computes the cross validation error for the classifier subject to various fairness constraints 147 | This function is just a wrapper of "train_model(...)", all inputs (except for num_folds) are the same. See the specifications of train_model(...) for more info. 148 | 149 | Returns lists of train/test accuracy (with each list holding values for all folds), the fractions of various sensitive groups in positive class (for train and test sets), and covariance between sensitive feature and distance from decision boundary (again, for both train and test folds). 150 | """ 151 | 152 | train_folds = [] 153 | test_folds = [] 154 | n_samples = len(y_all) 155 | train_fold_size = 0.7 # the rest of 0.3 is for testing 156 | 157 | # split the data into folds for cross-validation 158 | for i in range(0,num_folds): 159 | perm = range(0,n_samples) # shuffle the data before creating each fold 160 | shuffle(perm) 161 | x_all_perm = x_all[perm] 162 | y_all_perm = y_all[perm] 163 | x_control_all_perm = {} 164 | for k in x_control_all.keys(): 165 | x_control_all_perm[k] = np.array(x_control_all[k])[perm] 166 | 167 | 168 | x_all_train, y_all_train, x_control_all_train, x_all_test, y_all_test, x_control_all_test = split_into_train_test(x_all_perm, y_all_perm, x_control_all_perm, train_fold_size) 169 | 170 | train_folds.append([x_all_train, y_all_train, x_control_all_train]) 171 | test_folds.append([x_all_test, y_all_test, x_control_all_test]) 172 | 173 | def train_test_single_fold(train_data, test_data, fold_num, output_folds, sensitive_attrs_to_cov_thresh): 174 | 175 | x_train, y_train, x_control_train = train_data 176 | x_test, y_test, x_control_test = test_data 177 | 178 | w = train_model(x_train, y_train, x_control_train, loss_function, apply_fairness_constraints, apply_accuracy_constraint, sep_constraint, sensitive_attrs, sensitive_attrs_to_cov_thresh, gamma) 179 | train_score, test_score, correct_answers_train, correct_answers_test = check_accuracy(w, x_train, y_train, x_test, y_test, None, None) 180 | 181 | distances_boundary_test = (np.dot(x_test, w)).tolist() 182 | all_class_labels_assigned_test = np.sign(distances_boundary_test) 183 | correlation_dict_test = get_correlations(None, None, all_class_labels_assigned_test, x_control_test, sensitive_attrs) 184 | cov_dict_test = print_covariance_sensitive_attrs(None, x_test, distances_boundary_test, x_control_test, sensitive_attrs) 185 | 186 | distances_boundary_train = (np.dot(x_train, w)).tolist() 187 | all_class_labels_assigned_train = np.sign(distances_boundary_train) 188 | correlation_dict_train = get_correlations(None, None, all_class_labels_assigned_train, x_control_train, sensitive_attrs) 189 | cov_dict_train = print_covariance_sensitive_attrs(None, x_train, distances_boundary_train, x_control_train, sensitive_attrs) 190 | 191 | output_folds.put([fold_num, test_score, train_score, correlation_dict_test, correlation_dict_train, cov_dict_test, cov_dict_train]) 192 | 193 | 194 | return 195 | 196 | 197 | output_folds = Queue() 198 | processes = [Process(target=train_test_single_fold, args=(train_folds[x], test_folds[x], x, output_folds, sensitive_attrs_to_cov_thresh_arr[x])) for x in range(num_folds)] 199 | 200 | # Run processes 201 | for p in processes: 202 | p.start() 203 | 204 | 205 | # Get the reuslts 206 | results = [output_folds.get() for p in processes] 207 | for p in processes: 208 | p.join() 209 | 210 | 211 | test_acc_arr = [] 212 | train_acc_arr = [] 213 | correlation_dict_test_arr = [] 214 | correlation_dict_train_arr = [] 215 | cov_dict_test_arr = [] 216 | cov_dict_train_arr = [] 217 | 218 | results = sorted(results, key = lambda x : x[0]) # sort w.r.t fold num 219 | for res in results: 220 | fold_num, test_score, train_score, correlation_dict_test, correlation_dict_train, cov_dict_test, cov_dict_train = res 221 | 222 | test_acc_arr.append(test_score) 223 | train_acc_arr.append(train_score) 224 | correlation_dict_test_arr.append(correlation_dict_test) 225 | correlation_dict_train_arr.append(correlation_dict_train) 226 | cov_dict_test_arr.append(cov_dict_test) 227 | cov_dict_train_arr.append(cov_dict_train) 228 | 229 | 230 | return test_acc_arr, train_acc_arr, correlation_dict_test_arr, correlation_dict_train_arr, cov_dict_test_arr, cov_dict_train_arr 231 | 232 | 233 | 234 | def print_classifier_fairness_stats(acc_arr, correlation_dict_arr, cov_dict_arr, s_attr_name): 235 | 236 | correlation_dict = get_avg_correlation_dict(correlation_dict_arr) 237 | non_prot_pos = correlation_dict[s_attr_name][1][1] 238 | prot_pos = correlation_dict[s_attr_name][0][1] 239 | p_rule = (prot_pos / non_prot_pos) * 100.0 240 | 241 | print "Accuracy: %0.2f" % (np.mean(acc_arr)) 242 | print "Protected/non-protected in +ve class: %0.0f%% / %0.0f%%" % (prot_pos, non_prot_pos) 243 | print "P-rule achieved: %0.0f%%" % (p_rule) 244 | print "Covariance between sensitive feature and decision from distance boundary : %0.3f" % (np.mean([v[s_attr_name] for v in cov_dict_arr])) 245 | print 246 | return p_rule 247 | 248 | def compute_p_rule(x_control, class_labels): 249 | 250 | """ Compute the p-rule based on Doctrine of disparate impact """ 251 | 252 | non_prot_all = sum(x_control == 1.0) # non-protected group 253 | prot_all = sum(x_control == 0.0) # protected group 254 | non_prot_pos = sum(class_labels[x_control == 1.0] == 1.0) # non_protected in positive class 255 | prot_pos = sum(class_labels[x_control == 0.0] == 1.0) # protected in positive class 256 | frac_non_prot_pos = float(non_prot_pos) / float(non_prot_all) 257 | frac_prot_pos = float(prot_pos) / float(prot_all) 258 | p_rule = (frac_prot_pos / frac_non_prot_pos) * 100.0 259 | print 260 | print "Total data points: %d" % (len(x_control)) 261 | print "# non-protected examples: %d" % (non_prot_all) 262 | print "# protected examples: %d" % (prot_all) 263 | print "Non-protected in positive class: %d (%0.0f%%)" % (non_prot_pos, non_prot_pos * 100.0 / non_prot_all) 264 | print "Protected in positive class: %d (%0.0f%%)" % (prot_pos, prot_pos * 100.0 / prot_all) 265 | print "P-rule is: %0.0f%%" % ( p_rule ) 266 | return p_rule 267 | 268 | 269 | 270 | 271 | def add_intercept(x): 272 | 273 | """ Add intercept to the data before linear classification """ 274 | m,n = x.shape 275 | intercept = np.ones(m).reshape(m, 1) # the constant b 276 | return np.concatenate((intercept, x), axis = 1) 277 | 278 | def check_binary(arr): 279 | "give an array of values, see if the values are only 0 and 1" 280 | s = sorted(set(arr)) 281 | if s[0] == 0 and s[1] == 1: 282 | return True 283 | else: 284 | return False 285 | 286 | def get_one_hot_encoding(in_arr): 287 | """ 288 | input: 1-D arr with int vals -- if not int vals, will raise an error 289 | output: m (ndarray): one-hot encoded matrix 290 | d (dict): also returns a dictionary original_val -> column in encoded matrix 291 | """ 292 | 293 | for k in in_arr: 294 | if str(type(k)) != "" and type(k) != int and type(k) != np.int64: 295 | print str(type(k)) 296 | print "************* ERROR: Input arr does not have integer types" 297 | return None 298 | 299 | in_arr = np.array(in_arr, dtype=int) 300 | assert(len(in_arr.shape)==1) # no column, means it was a 1-D arr 301 | attr_vals_uniq_sorted = sorted(list(set(in_arr))) 302 | num_uniq_vals = len(attr_vals_uniq_sorted) 303 | if (num_uniq_vals == 2) and (attr_vals_uniq_sorted[0] == 0 and attr_vals_uniq_sorted[1] == 1): 304 | return in_arr, None 305 | 306 | 307 | index_dict = {} # value to the column number 308 | for i in range(0,len(attr_vals_uniq_sorted)): 309 | val = attr_vals_uniq_sorted[i] 310 | index_dict[val] = i 311 | 312 | out_arr = [] 313 | for i in range(0,len(in_arr)): 314 | tup = np.zeros(num_uniq_vals) 315 | val = in_arr[i] 316 | ind = index_dict[val] 317 | tup[ind] = 1 # set that value of tuple to 1 318 | out_arr.append(tup) 319 | 320 | return np.array(out_arr), index_dict 321 | 322 | def check_accuracy(model, x_train, y_train, x_test, y_test, y_train_predicted, y_test_predicted): 323 | 324 | 325 | """ 326 | returns the train/test accuracy of the model 327 | we either pass the model (w) 328 | else we pass y_predicted 329 | """ 330 | if model is not None and y_test_predicted is not None: 331 | print "Either the model (w) or the predicted labels should be None" 332 | raise Exception("Either the model (w) or the predicted labels should be None") 333 | 334 | if model is not None: 335 | y_test_predicted = np.sign(np.dot(x_test, model)) 336 | y_train_predicted = np.sign(np.dot(x_train, model)) 337 | 338 | def get_accuracy(y, Y_predicted): 339 | correct_answers = (Y_predicted == y).astype(int) # will have 1 when the prediction and the actual label match 340 | accuracy = float(sum(correct_answers)) / float(len(correct_answers)) 341 | return accuracy, sum(correct_answers) 342 | 343 | train_score, correct_answers_train = get_accuracy(y_train, y_train_predicted) 344 | test_score, correct_answers_test = get_accuracy(y_test, y_test_predicted) 345 | 346 | return train_score, test_score, correct_answers_train, correct_answers_test 347 | 348 | def test_sensitive_attr_constraint_cov(model, x_arr, y_arr_dist_boundary, x_control, thresh, verbose): 349 | 350 | 351 | """ 352 | The covariance is computed b/w the sensitive attr val and the distance from the boundary 353 | If the model is None, we assume that the y_arr_dist_boundary contains the distace from the decision boundary 354 | If the model is not None, we just compute a dot product or model and x_arr 355 | for the case of SVM, we pass the distace from bounday becase the intercept in internalized for the class 356 | and we have compute the distance using the project function 357 | 358 | this function will return -1 if the constraint specified by thresh parameter is not satifsified 359 | otherwise it will reutrn +1 360 | if the return value is >=0, then the constraint is satisfied 361 | """ 362 | 363 | 364 | 365 | 366 | assert(x_arr.shape[0] == x_control.shape[0]) 367 | if len(x_control.shape) > 1: # make sure we just have one column in the array 368 | assert(x_control.shape[1] == 1) 369 | 370 | arr = [] 371 | if model is None: 372 | arr = y_arr_dist_boundary # simply the output labels 373 | else: 374 | arr = np.dot(model, x_arr.T) # the product with the weight vector -- the sign of this is the output label 375 | 376 | arr = np.array(arr, dtype=np.float64) 377 | 378 | 379 | cov = np.dot(x_control - np.mean(x_control), arr ) / float(len(x_control)) 380 | 381 | 382 | ans = thresh - abs(cov) # will be <0 if the covariance is greater than thresh -- that is, the condition is not satisfied 383 | # ans = thresh - cov # will be <0 if the covariance is greater than thresh -- that is, the condition is not satisfied 384 | if verbose is True: 385 | print "Covariance is", cov 386 | print "Diff is:", ans 387 | print 388 | return ans 389 | 390 | def print_covariance_sensitive_attrs(model, x_arr, y_arr_dist_boundary, x_control, sensitive_attrs): 391 | 392 | 393 | """ 394 | reutrns the covariance between sensitive features and distance from decision boundary 395 | """ 396 | 397 | arr = [] 398 | if model is None: 399 | arr = y_arr_dist_boundary # simplt the output labels 400 | else: 401 | arr = np.dot(model, x_arr.T) # the product with the weight vector -- the sign of this is the output label 402 | 403 | 404 | sensitive_attrs_to_cov_original = {} 405 | for attr in sensitive_attrs: 406 | 407 | attr_arr = x_control[attr] 408 | 409 | 410 | bin_attr = check_binary(attr_arr) # check if the attribute is binary (0/1), or has more than 2 vals 411 | if bin_attr == False: # if its a non-binary sensitive feature, then perform one-hot-encoding 412 | attr_arr_transformed, index_dict = get_one_hot_encoding(attr_arr) 413 | 414 | thresh = 0 415 | 416 | if bin_attr: 417 | cov = thresh - test_sensitive_attr_constraint_cov(None, x_arr, arr, np.array(attr_arr), thresh, False) 418 | sensitive_attrs_to_cov_original[attr] = cov 419 | else: # sensitive feature has more than 2 categorical values 420 | 421 | cov_arr = [] 422 | sensitive_attrs_to_cov_original[attr] = {} 423 | for attr_val, ind in index_dict.items(): 424 | t = attr_arr_transformed[:,ind] 425 | cov = thresh - test_sensitive_attr_constraint_cov(None, x_arr, arr, t, thresh, False) 426 | sensitive_attrs_to_cov_original[attr][attr_val] = cov 427 | cov_arr.append(abs(cov)) 428 | 429 | cov = max(cov_arr) 430 | 431 | return sensitive_attrs_to_cov_original 432 | 433 | 434 | def get_correlations(model, x_test, y_predicted, x_control_test, sensitive_attrs): 435 | 436 | 437 | """ 438 | returns the fraction in positive class for sensitive feature values 439 | """ 440 | 441 | if model is not None: 442 | y_predicted = np.sign(np.dot(x_test, model)) 443 | 444 | y_predicted = np.array(y_predicted) 445 | 446 | out_dict = {} 447 | for attr in sensitive_attrs: 448 | 449 | attr_val = [] 450 | for v in x_control_test[attr]: attr_val.append(v) 451 | assert(len(attr_val) == len(y_predicted)) 452 | 453 | 454 | total_per_val = defaultdict(int) 455 | attr_to_class_labels_dict = defaultdict(lambda: defaultdict(int)) 456 | 457 | for i in range(0, len(y_predicted)): 458 | val = attr_val[i] 459 | label = y_predicted[i] 460 | 461 | # val = attr_val_int_mapping_dict_reversed[val] # change values from intgers to actual names 462 | total_per_val[val] += 1 463 | attr_to_class_labels_dict[val][label] += 1 464 | 465 | class_labels = set(y_predicted.tolist()) 466 | 467 | local_dict_1 = {} 468 | for k1,v1 in attr_to_class_labels_dict.items(): 469 | total_this_val = total_per_val[k1] 470 | 471 | local_dict_2 = {} 472 | for k2 in class_labels: # the order should be the same for printing 473 | v2 = v1[k2] 474 | 475 | f = float(v2) * 100.0 / float(total_this_val) 476 | 477 | 478 | local_dict_2[k2] = f 479 | local_dict_1[k1] = local_dict_2 480 | out_dict[attr] = local_dict_1 481 | 482 | return out_dict 483 | 484 | 485 | 486 | def get_constraint_list_cov(x_train, y_train, x_control_train, sensitive_attrs, sensitive_attrs_to_cov_thresh): 487 | 488 | """ 489 | get the list of constraints to be fed to the minimizer 490 | """ 491 | 492 | constraints = [] 493 | 494 | 495 | for attr in sensitive_attrs: 496 | 497 | 498 | attr_arr = x_control_train[attr] 499 | attr_arr_transformed, index_dict = get_one_hot_encoding(attr_arr) 500 | 501 | if index_dict is None: # binary attribute 502 | thresh = sensitive_attrs_to_cov_thresh[attr] 503 | c = ({'type': 'ineq', 'fun': test_sensitive_attr_constraint_cov, 'args':(x_train, y_train, attr_arr_transformed,thresh, False)}) 504 | constraints.append(c) 505 | else: # otherwise, its a categorical attribute, so we need to set the cov thresh for each value separately 506 | 507 | 508 | for attr_val, ind in index_dict.items(): 509 | attr_name = attr_val 510 | thresh = sensitive_attrs_to_cov_thresh[attr][attr_name] 511 | 512 | t = attr_arr_transformed[:,ind] 513 | c = ({'type': 'ineq', 'fun': test_sensitive_attr_constraint_cov, 'args':(x_train, y_train, t ,thresh, False)}) 514 | constraints.append(c) 515 | 516 | 517 | return constraints 518 | 519 | 520 | 521 | def split_into_train_test(x_all, y_all, x_control_all, train_fold_size): 522 | 523 | split_point = int(round(float(x_all.shape[0]) * train_fold_size)) 524 | x_all_train = x_all[:split_point] 525 | x_all_test = x_all[split_point:] 526 | y_all_train = y_all[:split_point] 527 | y_all_test = y_all[split_point:] 528 | x_control_all_train = {} 529 | x_control_all_test = {} 530 | for k in x_control_all.keys(): 531 | x_control_all_train[k] = x_control_all[k][:split_point] 532 | x_control_all_test[k] = x_control_all[k][split_point:] 533 | 534 | return x_all_train, y_all_train, x_control_all_train, x_all_test, y_all_test, x_control_all_test 535 | 536 | 537 | def get_avg_correlation_dict(correlation_dict_arr): 538 | # make the structure for the correlation dict 539 | correlation_dict_avg = {} 540 | # print correlation_dict_arr 541 | for k,v in correlation_dict_arr[0].items(): 542 | correlation_dict_avg[k] = {} 543 | for feature_val, feature_dict in v.items(): 544 | correlation_dict_avg[k][feature_val] = {} 545 | for class_label, frac_class in feature_dict.items(): 546 | correlation_dict_avg[k][feature_val][class_label] = [] 547 | 548 | # populate the correlation dict 549 | for correlation_dict in correlation_dict_arr: 550 | for k,v in correlation_dict.items(): 551 | for feature_val, feature_dict in v.items(): 552 | for class_label, frac_class in feature_dict.items(): 553 | correlation_dict_avg[k][feature_val][class_label].append(frac_class) 554 | 555 | # now take the averages 556 | for k,v in correlation_dict_avg.items(): 557 | for feature_val, feature_dict in v.items(): 558 | for class_label, frac_class_arr in feature_dict.items(): 559 | correlation_dict_avg[k][feature_val][class_label] = np.mean(frac_class_arr) 560 | 561 | return correlation_dict_avg 562 | 563 | 564 | 565 | def plot_cov_thresh_vs_acc_pos_ratio(x_all, y_all, x_control_all, num_folds, loss_function, apply_fairness_constraints, apply_accuracy_constraint, sep_constraint, sensitive_attrs): 566 | 567 | 568 | # very the covariance threshold using a range of decreasing multiplicative factors and see the tradeoffs between accuracy and fairness 569 | it = 0.05 570 | cov_range = np.arange(1.0, 0.0-it, -it).tolist() 571 | if apply_accuracy_constraint == True: 572 | if sep_constraint == False: 573 | it = 0.1 574 | cov_range = np.arange(0.0, 1.0 + it, it).tolist() 575 | if sep_constraint == True: 576 | cov_range = [0,1,5,10,20,50,100,500,1000] 577 | 578 | 579 | positive_class_label = 1 # positive class is +1 580 | train_acc = [] 581 | test_acc = [] 582 | positive_per_category = defaultdict(list) # for each category (male / female), the frac of positive 583 | 584 | # first get the original values of covariance in the unconstrained classifier -- these original values are not needed for reverse constraint 585 | test_acc_arr, train_acc_arr, correlation_dict_test_arr, correlation_dict_train_arr, cov_dict_test_arr, cov_dict_train_arr = compute_cross_validation_error(x_all, y_all, x_control_all, num_folds, loss_function, 0, apply_accuracy_constraint, sep_constraint, sensitive_attrs, [{} for i in range(0,num_folds)], 0) 586 | 587 | for c in cov_range: 588 | print "LOG: testing for multiplicative factor: %0.2f" % c 589 | sensitive_attrs_to_cov_original_arr_multiplied = [] 590 | for sensitive_attrs_to_cov_original in cov_dict_train_arr: 591 | sensitive_attrs_to_cov_thresh = deepcopy(sensitive_attrs_to_cov_original) 592 | for k in sensitive_attrs_to_cov_thresh.keys(): 593 | v = sensitive_attrs_to_cov_thresh[k] 594 | if type(v) == type({}): 595 | for k1 in v.keys(): 596 | v[k1] = v[k1] * c 597 | else: 598 | sensitive_attrs_to_cov_thresh[k] = v * c 599 | sensitive_attrs_to_cov_original_arr_multiplied.append(sensitive_attrs_to_cov_thresh) 600 | 601 | 602 | test_acc_arr, train_acc_arr, correlation_dict_test_arr, correlation_dict_train_arr, cov_dict_test_arr, cov_dict_train_arr = compute_cross_validation_error(x_all, y_all, x_control_all, num_folds, loss_function, apply_fairness_constraints, apply_accuracy_constraint, sep_constraint, sensitive_attrs, sensitive_attrs_to_cov_original_arr_multiplied, c) 603 | test_acc.append(np.mean(test_acc_arr)) 604 | 605 | 606 | correlation_dict_train = get_avg_correlation_dict(correlation_dict_train_arr) 607 | correlation_dict_test = get_avg_correlation_dict(correlation_dict_test_arr) 608 | 609 | # just plot the correlations for the first sensitive attr, the plotting can be extended for the other values, but as a proof of concept, we will jsut show for one 610 | s = sensitive_attrs[0] 611 | 612 | for k,v in correlation_dict_test[s].items(): 613 | if v.get(positive_class_label) is None: 614 | positive_per_category[k].append(0.0) 615 | else: 616 | positive_per_category[k].append(v[positive_class_label]) 617 | 618 | positive_per_category = dict(positive_per_category) 619 | 620 | p_rule_arr = (np.array(positive_per_category[0]) / np.array(positive_per_category[1])) * 100.0 621 | 622 | 623 | ax = plt.subplot(2,1,1) 624 | plt.plot(cov_range, positive_per_category[0], "-o" , color="green", label = "Protected") 625 | plt.plot(cov_range, positive_per_category[1], "-o", color="blue", label = "Non-protected") 626 | ax.set_xlim([min(cov_range), max(cov_range)]) 627 | plt.xlabel('Multiplicative loss factor') 628 | plt.ylabel('Perc. in positive class') 629 | if apply_accuracy_constraint == False: 630 | plt.gca().invert_xaxis() 631 | plt.xlabel('Multiplicative covariance factor (c)') 632 | ax.legend() 633 | 634 | ax = plt.subplot(2,1,2) 635 | plt.scatter(p_rule_arr, test_acc, color="red") 636 | ax.set_xlim([min(p_rule_arr), max(max(p_rule_arr), 100)]) 637 | plt.xlabel('P% rule') 638 | plt.ylabel('Accuracy') 639 | 640 | plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=0.5) 641 | plt.show() 642 | 643 | 644 | def get_line_coordinates(w, x1, x2): 645 | y1 = (-w[0] - (w[1] * x1)) / w[2] 646 | y2 = (-w[0] - (w[1] * x2)) / w[2] 647 | return y1,y2 -------------------------------------------------------------------------------- /preferential_fairness/README.md: -------------------------------------------------------------------------------- 1 | # Preferential fairness 2 | 3 | * [1. Fair classification demo](#1-fair-classification-demo) 4 | * [1.1. Generating a sample dataset](#11-generating-a-sample-dataset) 5 | * [1.2. Training an unconstrained classifier](#12-training-an-unconstrained-classifier) 6 | * [1.3. Training a parity fair classifier](#13-training-a-parity-fair-classifier) 7 | * [1.4. Training preferentially fair classifiers](#14-training-preferentially-fair-classifiers) 8 | * [1.5. Adult dataset](#15-adult-dataset) 9 | * [2. Using the code](#2-using-the-code) 10 | 11 | 12 | ## 1. Fair classification demo 13 | 14 | This repository provides the implementation for training preferentially fair classifiers: the classifiers where the benefits (fraction accepted into the positive class) for different groups might be disparate, yet each group would prefer their own decision outcomes. 15 | 16 | For more details, have a look at our paper. 17 | 18 | ### 1.1. Generating a sample dataset 19 | Lets start off by generating a sample dataset. 20 | 21 | ```shell 22 | $ cd synthetic_data_demo 23 | $ python decision_boundary_demo.py 24 | ``` 25 | 26 | The code will generate a dataset with a multivariate normal distribution. The data consists of two non-sensitive features and one sensitive feature. Sensitive feature encodes the demographic group (e.g., gender, race) of each data point. 27 | 28 | 29 | 30 | 31 | 32 | Green color denotes the positive class while red denotes negative. Circles represent the non-protected group (lets call it group-1) while crosses represent the protected group (lets call it group-0). It can be seen that class labels (green and red) are highly correlated with the sensitive feature value (protected and non-protected), that is, most of the green (positive class) points are in the non-protected class while most of red (negative class) points are in the protected class. 33 | 34 | 35 | 36 | 37 | _Close the figure to continue the code._ 38 | 39 | ### 1.2. Training an unconstrained classifier 40 | 41 | Next, the code will train a logistic regression classifier optimizing accuracy for each group separately: 42 | 43 | ```python 44 | clf = LinearClf(loss_function, lam=lam[s_attr_val], train_multiple=False) 45 | clf.fit(x_train[idx], y_train[idx], x_sensitive_train[idx], cons_params) 46 | ``` 47 | 48 | We are training the classifier without any constraints (more on constraints to come later). 49 | The following output is generated by the program: 50 | 51 | ``` 52 | == Unconstrained classifier == 53 | 54 | 55 | 56 | Accuracy: 0.840 57 | 58 | || s || frac_pos || 59 | || 0 || 0.20 (0.19) || 60 | || 1 || 0.84 (0.81) || 61 | ``` 62 | 63 | The "s" column denotes the protected (sensitive feature value 0) and non-protected group. The "frac_pos" column shows the fraction of points from each group selected into the positive class. The number in parentheses show the fraction in positive class had this group been classified using the classifier of the other group. In this case, we see that neither of the group is better off (getting more beneficial outcomes) by using the classifier of the other group. However, we do see examples in certain datasets where one group would prefer the classifier of the other group (details in Section 5 of our paper). 64 | 65 | Notice that this classifier violates both treatment parity (disparate treatment)--it trains a separate classifier for each group as well as impact parity--its beneficial outcome rates are different for different groups. Treatment parity (or satisfying disparate treatment) and impact parity (or satisfying disparate impact) are two well-known fairness criteria considered in the literature. See Sections 1 and 2 of our paper (and the discussion therein) for more details. 66 | 67 | 68 | 69 | The cyan line in the figure shows the decision boundary for group-0 (protected group). The legend shows the benefits each group would get by using this boundary. Specifically, 20% of group-0 would get beneficial outcomes by using this boundary whereas 81% of group-1 would get beneficial outcomes by using it. 70 | 71 | The accuracy is of course computed by computing the outcomes of each group with their own boundary. 72 | 73 | Next, we train a classifier satisfying both these criteria. 74 | 75 | ### 1.3. Training a parity fair classifier 76 | 77 | 78 | ```python 79 | cons_params["cons_type"] = 0 80 | clf = LinearClf(loss_function, lam=0.01, train_multiple=False) 81 | clf.fit(x_train, y_train, x_sensitive_train, cons_params) 82 | ``` 83 | This step uses the methodology introduced in our earlier AISTATS paper for more details. 84 | 85 | The results for the fair classifier look like this: 86 | 87 | ``` 88 | == Parity classifier == 89 | 90 | 91 | 92 | Accuracy: 0.558 93 | 94 | || s || frac_pos || 95 | || 0 || 0.53 (0.53) || 96 | || 1 || 0.52 (0.52) || 97 | 98 | ``` 99 | 100 | 101 | The parity classifier achieves both treatment and impact parity--the decisions received by a group (0 or 1) do not change based on the sensitive feature value and both groups get similar fractions of beneficial outcomes by the classifier. 102 | 103 | 104 | 105 | However, note that fairness comes at a very high cost: the classification accuracy drops from 0.84 to 0.56! 106 | 107 | Next, we leverage our preference-based fairness notions to reduce the cost of fairness. 108 | 109 | ### 1.4. Training preferentially fair classifiers 110 | 111 | First we train a preferred impact classifier. A classifier that ensures that each group gets at least as much beneficial outcomes as the ones provided by the parity classifier. 112 | 113 | ```python 114 | cons_params["cons_type"] = 1 115 | cons_params["tau"] = 1 116 | cons_params["s_val_to_cons_sum"] = s_val_to_cons_sum_di 117 | lam = {0:2.5, 1:0.01} 118 | clf = LinearClf(loss_function, lam=lam, train_multiple=True) 119 | clf.fit(x_train, y_train, x_sensitive_train, cons_params) 120 | ``` 121 | 122 | The results and the decision boundary for this experiment are: 123 | 124 | ``` 125 | == Preferred impact classifier == 126 | 127 | 128 | 129 | Accuracy: 0.758 130 | 131 | || s || frac_pos || 132 | || 0 || 0.54 (0.20) || 133 | || 1 || 0.85 (0.96) || 134 | 135 | ``` 136 | 137 | 138 | 139 | Notice that each group is getting at least as much beneficial outcomes as the parity classifier. Also, we are able to meet the preferred impact criterion at a much higher accuracy (0.76 vs. 0.56) as compared to the parity classifier. 140 | 141 | However, notice that the preferred impact classifier is not a preferred treatment classifier: group-1 would get more beneficial outcomes by using the decision boundary of group-0 (0.96) as compared the the beneficial outcomes it is getting with its own boundary (0.85). 142 | 143 | Next, we train a classifier that is _both_ preferred impact as well as preferred treatment--each group gets as least as much beneficial outcomes as the parity classifier _and_ no group gets more beneficial outcomes by using the classifier (or boundary) of another group. 144 | 145 | ```python 146 | cons_params["cons_type"] = 3 147 | cons_params["s_val_to_cons_sum"] = s_val_to_cons_sum_di 148 | lam = {0:2.5, 1:0.35} 149 | clf = LinearClf(loss_function, lam=lam, train_multiple=True) 150 | clf.fit(x_train, y_train, x_sensitive_train, cons_params) 151 | ``` 152 | 153 | The output looks like: 154 | 155 | ``` 156 | == Preferred treatment AND preferred impact classifier == 157 | 158 | 159 | 160 | Accuracy: 0.742 161 | 162 | || s || frac_pos || 163 | || 0 || 0.54 (0.36) || 164 | || 1 || 0.96 (0.96) || 165 | ``` 166 | 167 | Note that the group benefits satisfy both constraints. 168 | 169 | 170 | 171 | 172 | ### 1.5. Adult dataset 173 | 174 | We also provide a demo of our code on [Adult dataset](http://archive.ics.uci.edu/ml/datasets/Adult). For applying the fairness constraints on the adult dataset, execute the following commands: 175 | 176 | ```shell 177 | $ cd adult_data_demo 178 | $ python demo_constraints.py 179 | ``` 180 | 181 | 182 | ## 2. Using the code 183 | 184 | You can train a model by calling the fit() function in "fair_classification/linear_clf_pref_fairness.py". The predictions can be made by checking the sign of classifier's distance from boundary. You can use decision_function() in the same file for this part. 185 | -------------------------------------------------------------------------------- /preferential_fairness/adult_data_demo/demo_constraints.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | import os,sys 3 | import numpy as np 4 | from prepare_adult_data import load_adult_data 5 | from sklearn.model_selection import train_test_split 6 | sys.path.insert(0, '../../fair_classification/') # the code for fair classification is in this directory 7 | 8 | 9 | import stats_pref_fairness as compute_stats 10 | from linear_clf_pref_fairness import LinearClf 11 | 12 | 13 | def print_stats_and_plots(x,y,x_sensitive, clf): 14 | 15 | dist_arr, dist_dict = clf.get_distance_boundary(x, x_sensitive) 16 | acc, _, acc_stats = compute_stats.get_clf_stats(dist_arr, dist_dict, y, x_sensitive, print_stats=True) 17 | 18 | 19 | def test_adult_data(): 20 | 21 | """ Load data """ 22 | X, y, x_sensitive = load_adult_data(10000) # set plot_data to False to skip the data plot 23 | X = compute_stats.add_intercept(X) 24 | 25 | """ Split the data into train and test """ 26 | TEST_FOLD_SIZE = 0.3 27 | x_train, x_test, y_train, y_test, x_sensitive_train, x_sensitive_test = train_test_split(X, y, x_sensitive, test_size=TEST_FOLD_SIZE, random_state=1234, shuffle=False) 28 | compute_stats.scale_data(x_train, x_test) 29 | 30 | 31 | 32 | # Classifier parameters 33 | loss_function = "logreg" # perform the experiments with logistic regression 34 | EPS = 1e-3 35 | 36 | """ Unconstrained classifier """ 37 | 38 | 39 | cons_params = {} 40 | cons_params["EPS"] = EPS 41 | cons_params["cons_type"] = -1 # no constraint 42 | 43 | 44 | 45 | print "\n\n== Unconstrained classifier ==" 46 | # Train a classifier for each sensitive feature group separately optimizing accuracy for the respective group 47 | clf_group = {} 48 | lam = {0:1e-5, 1:1e-5} # the regularization parameter -- we set small values here, in the paper, we cross validate all of regularization parameters 49 | for s_attr_val in set(x_sensitive_train): 50 | idx = x_sensitive_train==s_attr_val # the index for the current sensitive feature group 51 | clf = LinearClf(loss_function, lam=lam[s_attr_val], train_multiple=False) 52 | clf.fit(x_train[idx], y_train[idx], x_sensitive_train[idx], cons_params) 53 | clf_group[s_attr_val] = clf 54 | 55 | # For simplicity of computing stats, we merge the two trained classifiers 56 | clf_merged = LinearClf(loss_function, lam=lam, train_multiple=True) 57 | clf_merged.w = {0:None, 1:None} 58 | for s_attr_val in set(x_sensitive_train): 59 | clf_merged.w[s_attr_val] = clf_group[s_attr_val].w 60 | 61 | print_stats_and_plots(x_test, y_test, x_sensitive_test, clf_merged) 62 | 63 | 64 | 65 | 66 | print "\n\n== Parity classifier ==" 67 | cons_params["cons_type"] = 0 68 | clf = LinearClf(loss_function, lam=1e-5, train_multiple=False) 69 | clf.fit(x_train, y_train, x_sensitive_train, cons_params) 70 | print_stats_and_plots(x_test, y_test, x_sensitive_test, clf) 71 | 72 | # compute the proxy value, will need this for the preferential classifiers 73 | dist_arr,dist_dict=clf.get_distance_boundary(x_train, x_sensitive_train) 74 | s_val_to_cons_sum_di = compute_stats.get_sensitive_attr_cov(dist_dict) 75 | 76 | 77 | 78 | 79 | print "\n\n\n\n== Preferred impact classifier ==" 80 | 81 | # Not all values of the lambda satisfy the constraints empirically (in terms of acceptace rates) 82 | # This is because the scale (or norm) of the group-conditional classifiers can be very different from the baseline parity classifier, and from each other. This affects the distance from boundary (w.x) used in the constraints. 83 | # We use a hold out set with different regaularizer values to validate the norms that satisfy the constraints. Check the appendix of our NIPS paper for more details. 84 | 85 | 86 | cons_params["cons_type"] = 1 87 | cons_params["tau"] = 0.1 88 | cons_params["s_val_to_cons_sum"] = s_val_to_cons_sum_di 89 | lam = {0:1e-3, 1:1e-5} 90 | clf = LinearClf(loss_function, lam=lam, train_multiple=True) 91 | clf.fit(x_train, y_train, x_sensitive_train, cons_params) 92 | print_stats_and_plots(x_test, y_test, x_sensitive_test, clf) 93 | 94 | 95 | 96 | 97 | 98 | 99 | print "\n\n\n\n== Preferred treatment AND preferred impact classifier ==" 100 | cons_params["cons_type"] = 3 101 | cons_params["s_val_to_cons_sum"] = s_val_to_cons_sum_di 102 | lam = {0:1e-3, 1:2e-3} 103 | clf = LinearClf(loss_function, lam=lam, train_multiple=True) 104 | clf.fit(x_train, y_train, x_sensitive_train, cons_params) 105 | print_stats_and_plots(x_test, y_test, x_sensitive_test, clf) 106 | 107 | 108 | 109 | def main(): 110 | test_adult_data() 111 | 112 | 113 | if __name__ == '__main__': 114 | main() -------------------------------------------------------------------------------- /preferential_fairness/adult_data_demo/prepare_adult_data.py: -------------------------------------------------------------------------------- 1 | import os,sys 2 | import urllib2 3 | import numpy as np 4 | from random import seed, shuffle 5 | from sklearn import preprocessing 6 | import pickle 7 | 8 | SEED = 1122 9 | seed(SEED) # set the random seed so that the random permutations can be reproduced again 10 | np.random.seed(SEED) 11 | 12 | """ 13 | The adult dataset can be obtained from: http://archive.ics.uci.edu/ml/datasets/Adult 14 | The code will look for the data files (adult.data, adult.test) in the present directory, if they are not found, it will download them from UCI archive. 15 | """ 16 | 17 | def check_data_file(fname): 18 | files = os.listdir(".") # get the current directory listing 19 | print "Looking for file '%s' in the current directory..." % fname 20 | 21 | if fname not in files: 22 | print "'%s' not found! Downloading from UCI Archive..." % fname 23 | addr = "http://archive.ics.uci.edu/ml/machine-learning-databases/adult/%s" % fname 24 | response = urllib2.urlopen(addr) 25 | data = response.read() 26 | fileOut = open(fname, "w") 27 | fileOut.write(data) 28 | fileOut.close() 29 | print "'%s' download and saved locally.." % fname 30 | else: 31 | print "File found in current directory.." 32 | 33 | print 34 | return 35 | 36 | 37 | def load_adult_data(load_data_size=None): 38 | 39 | """ 40 | if load_data_size is set to None (or if no argument is provided), then we load and return the whole data 41 | if it is a number, say 10000, then we will return randomly selected 10K examples 42 | """ 43 | 44 | attrs = ['age', 'workclass', 'fnlwgt', 'education', 'education_num', 'marital_status', 'occupation', 'relationship', 'race', 'sex', 'capital_gain', 'capital_loss', 'hours_per_week', 'native_country'] # all attributes 45 | int_attrs = ['age', 'fnlwgt', 'education_num', 'capital_gain', 'capital_loss', 'hours_per_week'] # attributes with integer values -- the rest are categorical 46 | sensitive_attrs = ['sex'] # the fairness constraints will be used for this feature 47 | attrs_to_ignore = ['race', 'sex' ,'fnlwgt'] # sex is the sensitive feature so we will not use it in classification, we will not consider fnlwght for classification since its computed externally and it highly predictive for the class (for details, see documentation of the adult data) 48 | attrs_for_classification = set(attrs) - set(attrs_to_ignore) 49 | 50 | # adult data comes in two different files, one for training and one for testing, however, we will combine data from both the files 51 | data_files = ["adult.data", "adult.test"] 52 | 53 | 54 | 55 | X = [] 56 | y = [] 57 | x_control = {} 58 | 59 | attrs_to_vals = {} # will store the values for each attribute for all users 60 | for k in attrs: 61 | if k in sensitive_attrs: 62 | x_control[k] = [] 63 | elif k in attrs_to_ignore: 64 | pass 65 | else: 66 | attrs_to_vals[k] = [] 67 | 68 | for f in data_files: 69 | check_data_file(f) 70 | 71 | for line in open(f): 72 | line = line.strip() 73 | if line == "": continue # skip empty lines 74 | line = line.split(", ") 75 | if len(line) != 15 or "?" in line: # if a line has missing attributes, ignore it 76 | continue 77 | 78 | class_label = line[-1] 79 | if class_label in ["<=50K.", "<=50K"]: 80 | class_label = -1 81 | elif class_label in [">50K.", ">50K"]: 82 | class_label = +1 83 | else: 84 | raise Exception("Invalid class label value") 85 | 86 | y.append(class_label) 87 | 88 | 89 | for i in range(0,len(line)-1): 90 | attr_name = attrs[i] 91 | attr_val = line[i] 92 | # reducing dimensionality of some very sparse features 93 | if attr_name == "native_country": 94 | if attr_val!="United-States": 95 | attr_val = "Non-United-Stated" 96 | elif attr_name == "education": 97 | if attr_val in ["Preschool", "1st-4th", "5th-6th", "7th-8th"]: 98 | attr_val = "prim-middle-school" 99 | elif attr_val in ["9th", "10th", "11th", "12th"]: 100 | attr_val = "high-school" 101 | 102 | if attr_name in sensitive_attrs: 103 | x_control[attr_name].append(attr_val) 104 | elif attr_name in attrs_to_ignore: 105 | pass 106 | else: 107 | attrs_to_vals[attr_name].append(attr_val) 108 | 109 | def convert_attrs_to_ints(d): # discretize the string attributes 110 | for attr_name, attr_vals in d.items(): 111 | if attr_name in int_attrs: continue 112 | uniq_vals = sorted(list(set(attr_vals))) # get unique values 113 | 114 | # compute integer codes for the unique values 115 | val_dict = {} 116 | for i in range(0,len(uniq_vals)): 117 | val_dict[uniq_vals[i]] = i 118 | 119 | # replace the values with their integer encoding 120 | for i in range(0,len(attr_vals)): 121 | attr_vals[i] = val_dict[attr_vals[i]] 122 | d[attr_name] = attr_vals 123 | 124 | 125 | # convert the discrete values to their integer representations 126 | convert_attrs_to_ints(x_control) 127 | convert_attrs_to_ints(attrs_to_vals) 128 | 129 | 130 | # if the integer vals are not binary, we need to get one-hot encoding for them 131 | 132 | for attr_name in attrs_for_classification: 133 | 134 | attr_vals = attrs_to_vals[attr_name] 135 | 136 | if attr_name in int_attrs or attr_name == "native_country": # the way we encoded native country, its binary now so no need to apply one hot encoding on it 137 | X.append(attr_vals) 138 | 139 | else: 140 | lb = preprocessing.LabelBinarizer() 141 | attr_vals = lb.fit_transform(attr_vals).T.tolist() 142 | for bin_val_arr in attr_vals: # each binarized array of size n (n = num examples in the dataset) 143 | X.append(bin_val_arr) 144 | 145 | 146 | # convert to numpy arrays for easy handline 147 | X = np.array(X, dtype=float).T 148 | y = np.array(y, dtype = float) 149 | for k, v in x_control.items(): x_control[k] = np.array(v, dtype=float) 150 | 151 | # shuffle the data 152 | perm = range(0,len(y)) # shuffle the data before creating each fold 153 | shuffle(perm) 154 | X = X[perm] 155 | y = y[perm] 156 | for k in x_control.keys(): 157 | x_control[k] = x_control[k][perm] 158 | 159 | # see if we need to subsample the data 160 | if load_data_size is not None: 161 | print "Loading only %d examples from the data" % load_data_size 162 | X = X[:load_data_size] 163 | y = y[:load_data_size] 164 | for k in x_control.keys(): 165 | x_control[k] = x_control[k][:load_data_size] 166 | 167 | x_sensitive = x_control["sex"] 168 | # np.savez("adult", X, y, x_sensitive) 169 | return X, y, x_sensitive 170 | 171 | 172 | 173 | -------------------------------------------------------------------------------- /preferential_fairness/synthetic_data_demo/decision_boundary_demo.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | import os,sys 3 | import numpy as np 4 | from generate_synthetic_data import * 5 | from sklearn.model_selection import train_test_split 6 | import matplotlib.pyplot as plt # for plotting stuff 7 | sys.path.insert(0, '../../fair_classification/') # the code for fair classification is in this directory 8 | from plot_synthetic_boundaries import plot_data 9 | 10 | import stats_pref_fairness as compute_stats # for computing stats 11 | from linear_clf_pref_fairness import LinearClf 12 | 13 | 14 | def print_stats_and_plots(x,y,x_sensitive, clf, fname): 15 | 16 | dist_arr, dist_dict = clf.get_distance_boundary(x, x_sensitive) 17 | acc, _, acc_stats = compute_stats.get_clf_stats(dist_arr, dist_dict, y, x_sensitive, print_stats=True) 18 | 19 | if isinstance(clf.w, dict): 20 | w_arr = [clf.w[0], clf.w[1]] 21 | lt_arr = ['c--', 'b--'] 22 | label_arr = [ 23 | "$\mathcal{B}_0: %0.2f; \mathcal{B}_1: %0.2f$" % (acc_stats[0][0]["frac_pos"], acc_stats[1][0]["frac_pos"]), 24 | "$\mathcal{B}_0: %0.2f; \mathcal{B}_1: %0.2f$" % (acc_stats[0][1]["frac_pos"], acc_stats[1][1]["frac_pos"]), 25 | ] 26 | else: 27 | w_arr = [clf.w] 28 | lt_arr = ['k--'] 29 | label_arr = [ 30 | "$\mathcal{B}_0: %0.2f; \mathcal{B}_1: %0.2f$" % (acc_stats[0][0]["frac_pos"], acc_stats[1][1]["frac_pos"]), 31 | ] 32 | 33 | title = "$Acc: %0.2f$" % acc 34 | plot_data(x, y, x_sensitive, w_arr, label_arr, lt_arr, fname, title) 35 | 36 | 37 | 38 | 39 | def test_synthetic_data(): 40 | 41 | """ Generate the synthetic data """ 42 | data_type = 1 43 | X, y, x_sensitive = generate_synthetic_data(data_type=data_type, n_samples=1000) # set plot_data to False to skip the data plot 44 | X = compute_stats.add_intercept(X) 45 | 46 | """ Split the data into train and test """ 47 | TEST_FOLD_SIZE = 0.3 48 | x_train, x_test, y_train, y_test, x_sensitive_train, x_sensitive_test = train_test_split(X, y, x_sensitive, test_size=TEST_FOLD_SIZE, random_state=1234, shuffle=False) 49 | 50 | # show the data 51 | plot_data(x_test, y_test, x_sensitive_test, None, None, None, "img/data.png", None) 52 | 53 | 54 | 55 | 56 | 57 | """ Training the classifiers """ 58 | # Classifier parameters 59 | loss_function = "logreg" # perform the experiments with logistic regression 60 | EPS = 1e-4 61 | 62 | 63 | 64 | cons_params = {} 65 | cons_params["EPS"] = EPS 66 | cons_params["cons_type"] = -1 # no constraint 67 | 68 | 69 | 70 | print "\n\n== Unconstrained classifier ==" 71 | # Train a classifier for each sensitive feature group separately optimizing accuracy for the respective group 72 | clf_group = {} # will store the classifier for eah group here 73 | lam = {0:0.01, 1:0.01} # the regularization parameter -- we set small values here, in the paper, we cross validate all of regularization parameters 74 | for s_attr_val in set(x_sensitive_train): 75 | idx = x_sensitive_train==s_attr_val # the index for the current sensitive feature group 76 | clf = LinearClf(loss_function, lam=lam[s_attr_val], train_multiple=False) # for more info, see the clf documentation in the respective file 77 | clf.fit(x_train[idx], y_train[idx], x_sensitive_train[idx], cons_params) 78 | clf_group[s_attr_val] = clf 79 | 80 | # For simplicity of computing stats, we merge the two trained classifiers 81 | clf_merged = LinearClf(loss_function, lam=lam, train_multiple=True) 82 | clf_merged.w = {0:None, 1:None} 83 | for s_attr_val in set(x_sensitive_train): 84 | clf_merged.w[s_attr_val] = clf_group[s_attr_val].w 85 | 86 | print_stats_and_plots(x_test, y_test, x_sensitive_test, clf_merged, "img/unconstrained.png") 87 | 88 | 89 | 90 | 91 | print "\n\n== Parity classifier ==" 92 | cons_params["cons_type"] = 0 93 | clf = LinearClf(loss_function, lam=0.01, train_multiple=False) 94 | clf.fit(x_train, y_train, x_sensitive_train, cons_params) 95 | print_stats_and_plots(x_test, y_test, x_sensitive_test, clf, "img/parity.png") 96 | 97 | # compute the proxy value, will need this for the preferential classifiers 98 | dist_arr,dist_dict=clf.get_distance_boundary(x_train, x_sensitive_train) 99 | s_val_to_cons_sum_di = compute_stats.get_sensitive_attr_cov(dist_dict) # will need this for applying preferred fairness constraints 100 | 101 | 102 | 103 | 104 | print "\n\n\n\n== Preferred impact classifier ==" 105 | 106 | 107 | # Not all values of the lambda satisfy the constraints empirically (in terms of acceptace rates) 108 | # This is because the scale (or norm) of the group-conditional classifiers can be very different from the baseline parity classifier, and from each other. This affects the distance from boundary (w.x) used in the constraints. 109 | # We use a hold out set with different regaularizer values to validate the norms that satisfy the constraints. Check the appendix of our NIPS paper for more details. 110 | 111 | cons_params["cons_type"] = 1 112 | cons_params["tau"] = 1 # the tau value varies based on the dataset we look at. See the DCCP documentation for details 113 | cons_params["s_val_to_cons_sum"] = s_val_to_cons_sum_di 114 | lam = {0:2.5, 1:0.01} 115 | clf = LinearClf(loss_function, lam=lam, train_multiple=True) 116 | clf.fit(x_train, y_train, x_sensitive_train, cons_params) 117 | print_stats_and_plots(x_test, y_test, x_sensitive_test, clf, "img/preferred_impact.png") 118 | 119 | 120 | 121 | print "\n\n\n\n== Preferred treatment AND preferred impact classifier ==" 122 | cons_params["cons_type"] = 3 123 | cons_params["s_val_to_cons_sum"] = s_val_to_cons_sum_di 124 | lam = {0:2.5, 1:0.35} 125 | clf = LinearClf(loss_function, lam=lam, train_multiple=True) 126 | clf.fit(x_train, y_train, x_sensitive_train, cons_params) 127 | print_stats_and_plots(x_test, y_test, x_sensitive_test, clf, "img/preferred_both.png") 128 | 129 | 130 | 131 | def main(): 132 | test_synthetic_data() 133 | 134 | 135 | if __name__ == '__main__': 136 | main() -------------------------------------------------------------------------------- /preferential_fairness/synthetic_data_demo/generate_synthetic_data.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import math 3 | import numpy as np 4 | from random import seed, shuffle 5 | from scipy.stats import multivariate_normal # generating synthetic data 6 | 7 | def generate_synthetic_data(data_type, n_samples): 8 | 9 | """ 10 | Code for generating the synthetic data. 11 | We will have two non-sensitive features and one sensitive feature. 12 | A sensitive feature value of 0.0 means the example is considered to be in protected group (e.g., female) and 1.0 means it's in non-protected group (e.g., male). 13 | """ 14 | 15 | # in the same program run, we might generate this data multiple times (once for single and once for two models) 16 | # so make sure that the seed is always the same 17 | SEED = 1234 18 | seed(SEED) 19 | np.random.seed(SEED) 20 | 21 | def gen_gaussian(mean_in, cov_in, class_label): 22 | nv = multivariate_normal(mean = mean_in, cov = cov_in) 23 | X = nv.rvs(n_samples) 24 | y = np.ones(n_samples, dtype=float) * class_label 25 | return nv,X,y 26 | 27 | 28 | if data_type == 1: 29 | 30 | disc_factor = math.pi / 8.0 # this variable determines the initial discrimination in the data -- decraese it to generate more discrimination 31 | 32 | 33 | 34 | """ Generate the non-sensitive features randomly """ 35 | # We will generate one gaussian cluster for each class 36 | mu1, sigma1 = [2, 2], [[5, 1], [1, 5]] 37 | mu2, sigma2 = [-2,-2], [[10, 1], [1, 3]] 38 | nv1, X1, y1 = gen_gaussian(mu1, sigma1, 1) # positive class 39 | nv2, X2, y2 = gen_gaussian(mu2, sigma2, -1) # negative class 40 | 41 | # join the posisitve and negative class clusters 42 | X = np.vstack((X1, X2)) 43 | y = np.hstack((y1, y2)) 44 | 45 | # shuffle the data 46 | perm = range(0,n_samples*2) 47 | shuffle(perm) 48 | X = X[perm] 49 | y = y[perm] 50 | 51 | rotation_mult = np.array([[math.cos(disc_factor), -math.sin(disc_factor)], [math.sin(disc_factor), math.cos(disc_factor)]]) 52 | X_aux = np.dot(X, rotation_mult) 53 | 54 | 55 | """ Generate the sensitive feature here """ 56 | x_control = [] # this array holds the sensitive feature value 57 | for i in range (0, len(X)): 58 | x = X_aux[i] 59 | 60 | # probability for each cluster that the point belongs to it 61 | p1 = nv1.pdf(x) 62 | p2 = nv2.pdf(x) 63 | 64 | # normalize the probabilities from 0 to 1 65 | s = p1+p2 66 | p1 = p1/s 67 | p2 = p2/s 68 | 69 | r = np.random.uniform() # generate a random number from 0 to 1 70 | 71 | if r < p1: # the first cluster is the positive class 72 | x_control.append(1.0) # 1.0 means its male 73 | else: 74 | x_control.append(0.0) # 0.0 -> female 75 | 76 | x_control = np.array(x_control) 77 | 78 | elif data_type == 2: 79 | 80 | ## More datasets here. 81 | pass 82 | 83 | 84 | 85 | return X,y,x_control 86 | 87 | -------------------------------------------------------------------------------- /preferential_fairness/synthetic_data_demo/img/data.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbilalzafar/fair-classification/45e85baad209134bbe2697c1a884dd1e2c2d9786/preferential_fairness/synthetic_data_demo/img/data.png -------------------------------------------------------------------------------- /preferential_fairness/synthetic_data_demo/img/parity.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbilalzafar/fair-classification/45e85baad209134bbe2697c1a884dd1e2c2d9786/preferential_fairness/synthetic_data_demo/img/parity.png -------------------------------------------------------------------------------- /preferential_fairness/synthetic_data_demo/img/preferred_both.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbilalzafar/fair-classification/45e85baad209134bbe2697c1a884dd1e2c2d9786/preferential_fairness/synthetic_data_demo/img/preferred_both.png -------------------------------------------------------------------------------- /preferential_fairness/synthetic_data_demo/img/preferred_impact.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbilalzafar/fair-classification/45e85baad209134bbe2697c1a884dd1e2c2d9786/preferential_fairness/synthetic_data_demo/img/preferred_impact.png -------------------------------------------------------------------------------- /preferential_fairness/synthetic_data_demo/img/unconstrained.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbilalzafar/fair-classification/45e85baad209134bbe2697c1a884dd1e2c2d9786/preferential_fairness/synthetic_data_demo/img/unconstrained.png -------------------------------------------------------------------------------- /preferential_fairness/synthetic_data_demo/plot_synthetic_boundaries.py: -------------------------------------------------------------------------------- 1 | import matplotlib 2 | import matplotlib.pyplot as plt # for plotting stuff 3 | import os 4 | import numpy as np 5 | 6 | matplotlib.rcParams['text.usetex'] = True # for type-1 fonts 7 | 8 | def get_line_coordinates(w, x1, x2): 9 | y1 = (-w[0] - (w[1] * x1)) / w[2] 10 | y2 = (-w[0] - (w[1] * x2)) / w[2] 11 | return y1,y2 12 | 13 | def plot_data(X, y, x_sensitive, w_arr, label_arr, lt_arr, fname, title, group=None): 14 | 15 | 16 | # print fp_fn_arr 17 | plt.figure() 18 | num_to_draw = 200 # we will only draw a small number of points to avoid clutter 19 | fs = 20 # font size for labels and legends 20 | 21 | x_draw = X[:num_to_draw] 22 | y_draw = y[:num_to_draw] 23 | x_sensitive_draw = x_sensitive[:num_to_draw] 24 | 25 | 26 | x_lim = [min(x_draw[:,-2]) - np.absolute(0.3*min(x_draw[:,-2])), max(x_draw[:,-2]) + np.absolute(0.5 * max(x_draw[:,-2]))] 27 | y_lim = [min(x_draw[:,-1]) - np.absolute(0.3*min(x_draw[:,-1])), max(x_draw[:,-1]) + np.absolute(0.7 * max(x_draw[:,-1]))] 28 | 29 | X_s_0 = x_draw[x_sensitive_draw == 0.0] 30 | X_s_1 = x_draw[x_sensitive_draw == 1.0] 31 | y_s_0 = y_draw[x_sensitive_draw == 0.0] 32 | y_s_1 = y_draw[x_sensitive_draw == 1.0] 33 | 34 | if w_arr is not None: # we are plotting the boundaries of a trained classifier 35 | plt.scatter(X_s_0[y_s_0==1.0][:, -2], X_s_0[y_s_0==1.0][:, -1], color='green', marker='x', s=70, linewidth=2) 36 | plt.scatter(X_s_0[y_s_0==-1.0][:, -2], X_s_0[y_s_0==-1.0][:, -1], color='red', marker='x', s=70, linewidth=2) 37 | plt.scatter(X_s_1[y_s_1==1.0][:, -2], X_s_1[y_s_1==1.0][:, -1], color='green', marker='o', facecolors='none', s=70, linewidth=2) 38 | plt.scatter(X_s_1[y_s_1==-1.0][:, -2], X_s_1[y_s_1==-1.0][:, -1], color='red', marker='o', facecolors='none', s=70, linewidth=2) 39 | 40 | 41 | for i in range(0, len(w_arr)): 42 | w = w_arr[i] 43 | l = label_arr[i] 44 | lt = lt_arr[i] 45 | 46 | x1,x2 = min(x_draw[:,1]), max(x_draw[:,1]) 47 | y1,y2 = get_line_coordinates(w, x1, x2) 48 | 49 | plt.plot([x1,x2], [y1,y2], lt, linewidth=3, label = l) 50 | 51 | 52 | plt.title(title, fontsize=fs) 53 | 54 | else: # just plotting the data 55 | plt.scatter(X_s_0[y_s_0==1.0][:, -2], X_s_0[y_s_0==1.0][:, -1], color='green', marker='x', s=70, linewidth=2, label= "group-0 +ve") 56 | plt.scatter(X_s_0[y_s_0==-1.0][:, -2], X_s_0[y_s_0==-1.0][:, -1], color='red', marker='x', s=70, linewidth=2, label= "group-0 -ve") 57 | plt.scatter(X_s_1[y_s_1==1.0][:, -2], X_s_1[y_s_1==1.0][:, -1], color='green', marker='o', facecolors='none', s=70, linewidth=2, label= "group-1 +ve") 58 | plt.scatter(X_s_1[y_s_1==-1.0][:, -2], X_s_1[y_s_1==-1.0][:, -1], color='red', marker='o', facecolors='none', s=70, linewidth=2, label= "group-1 -ve") 59 | 60 | 61 | if True: # turn the ticks on or off 62 | plt.tick_params(axis='x', which='both', bottom='off', top='off', labelbottom='off') # dont need the ticks to see the data distribution 63 | plt.tick_params(axis='y', which='both', left='off', right='off', labelleft='off') 64 | plt.legend(loc=2, fontsize=fs) 65 | plt.xlim(x_lim) 66 | plt.ylim(y_lim) 67 | 68 | 69 | 70 | plt.savefig(fname) 71 | 72 | 73 | plt.show() 74 | 75 | 76 | 77 | --------------------------------------------------------------------------------