-
Notifications
You must be signed in to change notification settings - Fork 923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AgentSet: Add agg
method
#2266
AgentSet: Add agg
method
#2266
Conversation
This commit introduces the `agg` method to the `AgentSet` class, allowing users to apply aggregation functions (e.g., `min`, `max`, `sum`, `np.mean`) to attributes of agents within the `AgentSet`. This enhancement makes it easier to compute summary statistics across agent attributes directly within the `AgentSet` interface.
Performance benchmarks:
|
While it will probably be used most of aggregation, in theory it could also be used for other things. Is it the most fitting name? |
I am fine with Another future pr is to bring |
I just realized these are totally equivalent: # Get the minimum energy value
min_energy = agentset.agg("energy", min)
min_energy = min(agentset.get("energy"))
# Calculate the total energy of sheep in the model
total_energy = model.get_agents_of_type[Sheep].agg("energy", sum)
total_energy = sum(model.get_agents_of_type[Sheep].get("energy"))
# Compute the average wealth of the poorest 10% using numpy
average_wealth = agentset.select(at_most=0.1).agg("wealth", np.mean)
average_wealth = np.mean(agentset.select(at_most=0.1).get("wealth")) Is there something as too much syntactic sugar? It does read nicely left to right though. |
Not sure about the last example, but yes, on the others, you are correct. If I read the last example correctly, you select 10% of the agents and then take their mean wealth, which is not equivalent to computing the average wealth of the poorest 10% using numpy. agentset.sort("wealth").select(at_most=0.1).agg("wealth", np.mean)
np.mean(agentset.sort("wealth").select(at_most=0.1)) So for just one attribute and one aggregation function, we now have 2 ways to do it if we merge this PR. I think the real use case is when you extend this to multipel attributes/and or multiple callables. |
I think the reading from left to right and the fact that this method is documented, and thus reminds people of "oh this is something we can do", provide enough value to add this relatively minor feature to the codebase. It's also consistent with how We could extend this to allow a single dict input: .agg({"attribute": Callable, ["multiple", "attributes"]: Callable}) Then the output would be a list or tuple. |
For inspiration, I looked at DataFrame.agg. This allows
Of course their use case is different because agg applies over all rows/columns, while our use case involves first selecting one or more attributes upon which to apply the aggregation function. My hunch is to allow either 2 argumenst (i.e., an attribute name and. a callable), or a single dict as you suggest. I like the clean simple case of just passign an attribute/list of attributes and a single callable and would love to preserve that API: agentset.agg("wealth", calculate_gini) Alternatively, you just wrap everying into a long list of arguments rather than use a dict: agentset.agg("wealth", calculate_gini,
["attr_a", "attr_b"], some _other_function) So basically, you allways pass an even number where the odd entry is a string or list of strings, while the even entry is the callable to apply. I agree that this can come in a later PR. |
So, for now, merge as is? If so, this PR is ready for final review. |
This PR introduces the `agg` method to the `AgentSet` class, allowing users to apply aggregation functions (e.g., `min`, `max`, `sum`, `np.mean`) to attributes of agents within the `AgentSet`. This enhancement makes it easier to compute summary statistics across agent attributes directly within the `AgentSet` interface. This will be useful in both the model operation itself as well as for future DataCollector use.
This PR introduces the
agg
method to theAgentSet
class, allowing users to apply aggregation functions (e.g.,min
,max
,sum
,np.mean
) to attributes of agents within theAgentSet
. This enhancement makes it easier to compute summary statistics across agent attributes directly within theAgentSet
interface.This will be useful in both the model operation itself as well as for future DataCollector use.
New Method:
agg
Parameters:
attribute
(str): The name of the attribute to aggregate.func
(Callable): The aggregation function to apply (e.g.,min
,max
,sum
,np.mean
).Returns:
AgentSet
.Usage Examples
Future work
This function could be expended by:
However, this PRs limit the scope to a single attribute and single function, since that's the most common use case.