adding ai_economist for modding
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
# Copyright (c) 2020, salesforce.com, inc.
|
||||
# All rights reserved.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
# For full license text, see the LICENSE file in the repo root
|
||||
# or https://opensource.org/licenses/BSD-3-Clause
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
||||
def get_gini(endowments):
|
||||
"""Returns the normalized Gini index describing the distribution of endowments.
|
||||
|
||||
https://en.wikipedia.org/wiki/Gini_coefficient
|
||||
|
||||
Args:
|
||||
endowments (ndarray): The array of endowments for each of the agents in the
|
||||
simulated economy.
|
||||
|
||||
Returns:
|
||||
Normalized Gini index for the distribution of endowments (float). A value of 1
|
||||
indicates everything belongs to 1 agent (perfect inequality), whereas a
|
||||
value of 0 indicates all agents have equal endowments (perfect equality).
|
||||
|
||||
Note:
|
||||
Uses a slightly different method depending on the number of agents. For fewer
|
||||
agents (<30), uses an exact but slow method. Switches to using a much faster
|
||||
method for more agents, where both methods produce approximately equivalent
|
||||
results.
|
||||
"""
|
||||
n_agents = len(endowments)
|
||||
|
||||
if n_agents < 30: # Slower. Accurate for all n.
|
||||
diff_ij = np.abs(
|
||||
endowments.reshape((n_agents, 1)) - endowments.reshape((1, n_agents))
|
||||
)
|
||||
diff = np.sum(diff_ij)
|
||||
norm = 2 * n_agents * endowments.sum(axis=0)
|
||||
unscaled_gini = diff / (norm + 1e-10)
|
||||
gini = unscaled_gini / ((n_agents - 1) / n_agents)
|
||||
return gini
|
||||
|
||||
# Much faster. Slightly overestimated for low n.
|
||||
s_endows = np.sort(endowments)
|
||||
return 1 - (2 / (n_agents + 1)) * np.sum(
|
||||
np.cumsum(s_endows) / (np.sum(s_endows) + 1e-10)
|
||||
)
|
||||
|
||||
|
||||
def get_equality(endowments):
|
||||
"""Returns the complement of the normalized Gini index (equality = 1 - Gini).
|
||||
|
||||
Args:
|
||||
endowments (ndarray): The array of endowments for each of the agents in the
|
||||
simulated economy.
|
||||
|
||||
Returns:
|
||||
Normalized equality index for the distribution of endowments (float). A value
|
||||
of 0 indicates everything belongs to 1 agent (perfect inequality),
|
||||
whereas a value of 1 indicates all agents have equal endowments (perfect
|
||||
equality).
|
||||
"""
|
||||
return 1 - get_gini(endowments)
|
||||
|
||||
|
||||
def get_productivity(coin_endowments):
|
||||
"""Returns the total coin inside the simulated economy.
|
||||
|
||||
Args:
|
||||
coin_endowments (ndarray): The array of coin endowments for each of the
|
||||
agents in the simulated economy.
|
||||
|
||||
Returns:
|
||||
Total coin endowment (float).
|
||||
"""
|
||||
return np.sum(coin_endowments)
|
||||
Reference in New Issue
Block a user