Abstract
A primary goal of collective population behavior studies is to determine the rules governing crowd distributions in order to predict future behaviors in new environments. Current topdown modeling approaches describe, instead of predict, specific emergent behaviors, whereas bottomup approaches must postulate, instead of directly determine, rules for individual behaviors. Here, we employ classical density functional theory (DFT) to quantify, directly from observations of local crowd density, the rules that predict mass behaviors under new circumstances. To demonstrate our theorybased, datadriven approach, we use a model crowd consisting of walking fruit flies and extract two functions that separately describe spatial and social preferences. The resulting theory accurately predicts experimental fly distributions in new environments and provides quantification of the crowd “mood”. Should this approach generalize beyond milling crowds, it may find powerful applications in fields ranging from spatial ecology and active matter to demography and economics.
Similar content being viewed by others
Introduction
Identifying the role of social interactions and environmental influences on living systems has been the goal of many recent studies of collective population behavior^{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}. Current agentbased models of crowds can reproduce many emergent behaviors, ranging from random milling to swarming, but often must postulate preconceived rules for individual agent interactions with each other and their environment^{1,2,3,4,5,6,7,8,9,10}. In contrast to such bottomup approaches, some studies have inferred interaction rules from observations of individual motions within a crowd for a few species of fish^{11,12}, birds^{13}, and insects^{14,15}, but these studies have largely been limited to specific behaviors and have not been developed for making predictions under new circumstances. To date, a general predictive approach to emergent collective behavior in living systems has been lacking.
Such approaches, however, have been developed successfully for large collections of interacting atoms and molecules in the field of statistical physics. One of the central tenants of statistical physics is that generic thermodynamic behaviors emerge from underlying interaction rules among large numbers of particles^{16,17}. Remarkably, these emergent behaviors are often insensitive to the detailed nature of the underlying interactions. Here, we pursue the hypothesis that a similar scenario emerges in the study of large crowds^{18,19,20,21,22} so that behaviors arising from generic agentbased models can be predicted using a topdown approach. Accordingly, our strategy is to begin with a family of models that roughly capture the “microscopic” behaviors of individuals as they rearrange within a crowd. We do this, not because we are interested directly in individual behaviors, but rather because we are interested in the generic “macroscopic” behaviors that emerge in crowds en masse. This tack is not a priori obvious since active systems do not possess a fixed energy, their temperature is illdefined, and there are no obvious equilibrium states^{23}. Nonetheless, we show here that mathematical equivalents of free energy, the Hamiltonian, and equilibrium states arise naturally from plausible models of crowd behavior.
In this work, we present the following results. We introduce a general class of plausible agentbased models in which two different functions,“vexation” and “frustration,” quantify location and social preferences, respectively. For this class of models, we develop a coarsegrained approach stemming from classical densityfunctional theory (DFT) that allows us to determine the general mathematical form of the probability distributions describing a crowd. We then discuss the conditions a system must possess to be describable by our theory and test our approach using a living system consisting of walking fruit flies (Drosophila melanogaster), which we confine to a variety of twodimensional environments. For this fruitfly system, we successfully extract the vexation and frustration functions corresponding to a variety of different physical settings. Furthermore, these functions are sufficiently stable that, by mixing and matching functions from different experiments, we accurately predict crowd distributions in new environments. Finally, by exposing the fly system to conditions that elicit distinct social motivations, we are able to identify changes in the overall behavior of the crowd, i.e., its “mood,” by tracking the evolution of the social preference function.
Results
General mathematical form of crowddensity distributions
Consider, as an example, a crowd at a political rally (Fig. 1a). Under such circumstances, individuals will seek the best locations—presumably closest to the stage—while avoiding overcrowded areas where there is insufficient “personal space.” Moreover, individuals will, from time to time, move to new, better locations that become available.
A plausible agentbased model of this behavior would assign an intrinsic desirability of each location x through a “vexation” function V(x) that takes its minimum value at the most ideal location near the stage. In addition, it would account for crowding effects through the local crowd areal density n(x) by introducing a “frustration” function f′(n), so that the relative preferablity of location x is actually the sum of vexation and frustration effects, V(x) + f′(n(x)). Finally, this model would include a behavioral rule to account for the tendency for individuals to seek improved locations. When an agent considers a move from location x to x′, the change in the agent’s dissatisfaction is ΔH ≡ (V(x′) + f′(n(x′)) − (V(x) + f′(n(x)). A rule where each agent executes such moves with probability 1/(e^{ΔH} + 1) captures the intuition that moves that increase the dissatisfaction ΔH > 0 are unlikely, and moves that decrease the dissatisfaction ΔH < 0 are likely, while moves where ΔH = 0 occur with 50% probability. The disadvantage of such an agent based modeling approach is that the rules for each agent are postulated and comparison with experiment requires gathering statistics from repeated simulations, each of which scales as the number of agents or worse. Again, our purpose here is not to develop such a model in detail, but rather to explore the toplevel, global behaviors that emerge from this class of models, which we conjecture should apply to crowds more generally.
To extract such global behaviors, we develop a topdown approach by considering the system as a whole and summing the changes in the individual agent dissatisfactions ΔH to obtain a net global population dissatisfaction functional H[n(x)] (Methods). Integrating over dn and area element dA yields
where the net frustration effect at location x is described by \(f(n) = {\int} f\prime (n){\kern 1pt} dn\), and a local density approximation^{24,25} \(F[n({\mathbf{x}})] \equiv {\int} f(n({\mathbf{x}})){\kern 1pt} dA\) is in this case sufficient for capturing the crowd behavior. This global functional H[n(x)] and the model described above then lead mathematically to the prediction (Methods) that the probability for observing a crowd arrangement with density n(x) will be given by the probability density functional
where Z is an overall normalization constant. Since we cannot measure the function n(x) directly in experimental crowds, we instead consider discrete counts of individuals within equal area bins (quadrats)^{26}. Thus, to make contact with experiments we discretize Eq. 1 as \(H = \mathop {\sum}\nolimits_b \left( {f_{N_b} + v_bN_b} \right)\), where v_{b} is the average value of the vexation V(x) over bin b, and \(f_{N_b} \equiv f(N_b/A)A\) approximates the total frustration contribution of bin b of area A (Methods). Substituting this discretization into Eq. 2, the overall probability factors into independent distributions for each bin of the form
where z_{b} is a bindependent normalization constant and N! accounts for equivalent configurations among the bins (Methods). Thus, we predict that the fluctuations of the bin counts will be statistically independent and follow a modified Poisson form for each bin. This formulation dramatically reduces the complexity of the system description from tracking each individual to tracking the local density in each bin. Additionally, instead of rules with potentially complex interactions for each agent, the global system behavior of the density is determined by just two functions, v_{b} and a binindependent f_{N}. Because this reduction in the number of variables is the result of transitioning to a localdensity description as in classical densityfunctional theory, but now with the modification that interactions are inferred from density fluctuations, we call our approach densityfunctional fluctuation theory (DFFT).
Remarkably, rather than postulating these functions, they can be extracted directly from measurements of density distributions in each bin. In particular, in the case of neutral interactions (f_{N} = 0), the bin counts will be singleparameter Poisson distributed, as expected for an experiment counting socalled completely spatially random events^{26}. From the mean of these distributions one can extract an effective v_{b} (Fig. 1b, c), or logarithm of the socalled intensity^{26}, that can arise either from actual preferences for particular locations or from other kinetic interactions with the environment, such as slowing down near barriers^{27}. In the case of interactions, such probability distributions can vary substantially from their noninteracting form (Fig. 1d) when the interactions are included (Fig. 1e, f). For example, socalled contagious distributions, which correspond to attractive interactions and show increased variancetomean ratios, have been observed^{26,28,29}. If the interaction is strongly attractive, groups will form, resulting in a bimodal bin probability distribution corresponding to low and high density regions (Fig. 1e), with the high density region constrained by the packing limit. In contrast, highly repulsive interactions (Fig. 1f) lead to more uniform distribution of individuals in the crowd^{26} and will narrow the bin probability distribution. Finally, from distortions off of the Poisson form, we can determine an effective frustration function f_{N}, without assuming any particular functional form, that describes any local interaction, attractive or repulsive. This formulation holds whether the interaction is directly related to density or to more complex factors such as orientation distributions, as well as higherorder many body interactions (Methods). The power of this approach is that, since v_{b} is tied to the interactions with the environment and f_{N} is tied to interagent interactions, it may be possible to combine vexations and frustrations from previous measurements to predict future crowd behaviors.
Several conditions must be met when applying this methodology to crowds under realistic circumstances. For example, the system must be sufficiently ergodic. Thus, the time scales for measurements must be longer than the system decorrelation time. In addition, the agent interactions with their environment should be sufficiently independent of the agent density, the agent interactions should be sufficiently independent of location, and both should be stable over the measurement time. Finally, bin sizes must be appropriately chosen. The bins must be large enough to yield reliable estimates of density, as well as to avoid trivial correlations in neighboring bins, yet small enough that the underlying vexation and local density are nearly constant across each bin.
Extraction of functionals for model system of walking flies
To test whether this approach applies to actual populations, we consider a model crowd consisting of wildtype male Drosophila melanogaster from an outbred laboratory stock. It is well know that flies exhibit complex spatial preferences^{30,31} and social behaviors^{32,33}. Here we seek to determine whether a large crowd of individuals with such complex behaviors indeed can be described within our vexation and frustration framework. The flies are confined in 1.5 mm tall transparent chambers where they can walk freely but cannot fly or climb on top of each other. We record overhead videos of the flies, bin the arena, and use custom Matlabbased tracking algorithms (Methods) to measure the individual bin counts N_{b} in each video frame. To explore a variety of behaviors, we use arenas of different shapes^{30} and apply heat gradients^{34} across the arenas to generate different spatial preferences. We find that the flies fully adjust to such changes in their environments after 5 min. We also find that the behavior of the flies changes slowly over a time scale of hours (Methods). We thus take care to make our observations over 10 minute windows during time periods where the behavior is stable.
A top down image of 65 flies in a quasi 1D arena that is uncomfortably heated on the right is shown in Fig. 2a. We find that a bin size of 0.15 cm^{2}, corresponding to the area of approximately 7 flies, ensures that the counts are spatially independent (Fig. 2b) and that the density does not vary substantially over each bin. We also find that the decorrelation time for N_{b} is about 5 s (Fig. 2c) indicating the system is sufficiently ergodic over the time scale of our observation windows. We show representative probability distributions P_{b}(N) for a high and a low density bin in Fig. 2d, e, respectively. We find that the distribution peaks are centered at higher N near the left side of the chamber suggesting lower vexation there. Additionally, the high density probability distribution is significantly narrower than the fitted Poisson distribution, hinting that there are repulsive interactions among the flies.
To validate our description and quantify the vexations and frustrations, we plot what we call as a mnemonic the “pseudofree energy” −ln(N!P_{b}(N)) = (v_{b}N +ln z_{b}) + f_{N} versus N in Fig. 2f. To determine whether the frustration f_{N} is indeed universal, we subtract a linear term corresponding to a bindependent vexation and normalization constant, v_{b}N +ln z_{b}, from each curve. Remarkably, the resulting curves can be made to collapse, indicating that a single, universal frustration function f_{N} applies equally well to all bins (Fig. 2g). The positive curvature indicates that higher densities are less preferable than expected from noninteracting populations, and thus indicates repulsive interactions. We also show the bindependent vexation values v_{b} used to collapse the curves in Fig. 2h. Finally, as an indicator of the strength of the collapse, we find that modifying the best leastsquared fit Poisson distributions by including just eight universal frustration values (f_{0} through f_{7}) decreases our reduced χ^{2} value for 166 degrees of freedom from 8.1 to 0.95. Additionally, our DFFT model is favored by the likelihood ratio test with probability p < 0.001 for accepting the hypothesis that the frustration values should be taken to be zero and a vexationonly model be used. This latter test confirms that the aforementioned reduction in χ^{2} is not a result of overfitting (Methods).
Predictions of crowd density under new circumstances
An important consequence of the physical independence of f_{N} from v_{b} is that it should be possible to use the frustrations extracted from the quasi1D chamber to predict fly distributions in distinct vexations (Fig. 3). We demonstrate this capability by predicting the measured density distributions for large numbers of flies (on the order of 100) in three distinct geometries and temperature gradients (Fig. 3a). Using measurements of just a few flies in each chamber, we extract density distributions and determine the corresponding vexation v_{b}. Combining this fewfly vexation for each environment with the manyfly frustration f_{N} extracted from the quasi 1D geometry, we predict the fly distributions under dense conditions. Fig. 3b shows this procedure for the staircase geometry. We find that the individual fly probability distributions (density normalized by total number of flies) for low and high densities are significantly different (Fig. 3c). In contrast, including the interactions through our DFFT approach predicts a more homogeneous population that matches the observed distribution (Fig. 3d). These results demonstrate that, using our DFFT analysis, it is indeed possible to make accurate predictions by combining vexations from lowdensity experiments in different environments with a frustration that corresponds to a particular behavior (“mood”).
Frustration used to quantify the “mood” of a crowd
Conversely, by keeping the environmental conditions fixed and analyzing different time points in the experiments or changing the ratio of male to female flies, the resulting change in “mood” can be quantified by extracting the corresponding functionals. For example, after spending about six hours in the chamber without food or water, the flies exhibit transient groups or clusters of about 1020 individuals. This change in behavior is quantified by the different curvatures for the frustrations f_{N} characterizing the initial (blue curve) and deprived states (red curve) in Fig. 4. The nearly flat frustration associated with this behavior indicates that male flies are willing to surmount their natural repulsion and form higher density groups under deprivation conditions, a previously undocumented spontaneous selforganized change in collective behavior^{31,32,35}. Attraction between individuals can be induced by introducing female flies. For groups of flies with equal numbers of males and females which have been separated for several days, we find pair formation (yellow ellipses). This behavior is characterized by a sharp downward curvature in the frustration at low N (yellow curve). Exposing this population to similar deprivation conditions drives formation of larger groups (purple circle) at the expense of pair formation. This behavior is captured by the shift of downward curvature in the frustration to larger bin occupations of N≈7 (purple curve). These data establish that the DFFT approach has the power to detect and quantify changes in social behaviors.
Discussion
Collectively, these results demonstrate that topdown approaches are a promising method for predicting crowd distributions and quantifying crowd behaviors. The DFFT analysis that we present is particularly powerful because it separates the influence of the environment on agents from interactions among those agents. This separation then enables predictions of crowd distributions in new situations through mixing and matching of the vexations and frustrations from previous observations in different scenarios. In addition, the realtime quantification of frustrations opens the door to tracking behavioral changes and potentially extrapolating the time evolution of frustrations to anticipate future behaviors.
There are a number of directions in which the formal framework suggested here can be extended, paralleling developments from the traditional densityfunctional theory literature. Extensions to timedependent DFT methods (TDDFT)^{36,37} would enable the prediction of situations in which crowds gather and disperse in response to changes in the environment. This approach would also apply to situations in which the center of mass of the entire group is moving as whole, such as in herd migration and bacterial and insect swarming. Moreover, by including the local current density (“flow”) in the functional, such approaches may even be able to describe crowds where correlated subgroups move with different local velocities, such as in flocks of birds. Likewise, extensions to multicomponent DFT^{38} would enable corresponding predictions and observations in crowds composed of distinct groups exhibiting interactions such as intergroup conflict, predatorprey relations, or mating behavior.
Should these results extend to human populations, the implications are profound. From publicly available video data of people milling in public spaces, this approach could predict how people would distribute themselves under extreme crowding. Additionally, a simple application running on a handheld device could easily measure density fluctuations and extract functionals that are indicative of the current behavioral state or mood of the crowd. Through comparison with a library of functionals measured from past events, such an application could provide early warning as a crowd evolves towards a dangerous behavior. Finally, given the recent proliferation of newly available cellphone and census data^{39,40} these approaches may also extend to population flows on larger scales, such as migration. Here, vexations could correspond to political or environmental drivers and frustrations to population pressures. The resulting predictions of migration during acute events would enable better planning by all levels of government officials, from local municipalities to international bodies^{40,41}, with the potential to save millions of human lives.
Methods
Global dissatisfaction functional H[n(x)]
The main text describes a net global population dissatisfaction functional H[n(x)]. To derive this functional, we begin by considering a deterministic model, in which agents reject or accept potential moves with unit probability according to whether ΔH ≡ (V(x′) + f′(n(x′)) − (V(x) + f′(n(x)) is positive or negative, respectively. In such a model, it is clear that equilibrium is attained and all motion ceases when ΔH = 0 for all pairs of points x and x′. This statement is equivalent to the combination V(x) + f′(n(x)) attaining some constant value μ across the system,
This equation corresponds precisely to the Lagrangemultiplier equation for minimization of the functional
subject to the constraint of fixed number of agents \(N = {\int} n({\mathbf{x}})\,dA\), with μ being the corresponding Lagrangemultiplier. Here, μ plays an analogous role to the “chemical potential” from Statistical Physics.
Probability density functional P[n(x)]
To make the transition to the probability functional P[n(x)], we note that the stochastic model described in the text maps directly onto a particular Markov chain. Each step on this chain corresponds to a threestage process. First, (a) an agent is selected at random to consider a possible move from current location x. Selecting a random agent at each time step allows agents to adjust their locations at equal rates. In this approach, choosing the physical time interval between Markov steps to be inversely related to the number of agents preserves the time scale of the overall crowd dynamics. Second, (b) a location x′ nearby x is selected at random as a move to be considered by the given agent. We note that for this work, we assume that the new location x′ is selected in a symmetric way so that that agents at x contemplate moves to x′ with the same probability that agents at x′ contemplate moves to x. This assumption seems most plausible given the systems we consider here. Other selection criteria, however, are possible and would modify the distribution below. Finally, (c) the contemplated move is accepted or rejected according to the probability 1/(e^{ΔH} + 1), where ΔH is defined specifically as the change in the value of the functional described in Eq. 5 as a result of the move.
There are two critical things to note about this Markov chain. The first is that it gives a very natural description of agent behavior. The second is that it corresponds precisely to the standard MetropolisBarker algorithm^{42,43} for drawing random samples from the Boltzmann distribution P ∝ exp(−H) for a Hamiltonian H. Thus, under our proposed motion model, the population itself naturally samples from the distribution quoted in the text,
Discretization H=Σ_{b}(\({\boldsymbol{ f}}_{{\boldsymbol{N}}_{\boldsymbol{b}}} + {\boldsymbol{v}}_{\boldsymbol{b}} {\boldsymbol{N}}_{\boldsymbol{b}}\))
To arrive at the discretization described in the text, it is important to note that the density n(x) appearing in the probability functional P[n(x)] corresponds to the fluctuating crowd density, as opposed to the average density n_{ave}(x). As such, in practice, this density must be described in terms of the discrete locations x_{a} of all agents a in the crowd at any give time. The most natural description for the associated density operator is
where δ^{(σ)}(x, x_{a}) is a function describing the range over which the presence of an agent at x_{a} contributes to the density n(x) at point x. To conserve number of agents, this function must integrate to unity. The analysis carried out in the text divides space into bins b of area A_{b}, and estimates the density in each bin as n = N_{b}/A_{b} where N_{b} corresponds to the total number of agents in bin b. This definition sets the range function as
To capture relevant variations in vexation and density, the bins cannot be selected so large that these quantities vary significantly across each bin. Alternately, to avoid missing the effects of nearby agents, the bins cannot be selected to be smaller than the agent’s interaction range.
Finally, combining equations 5, 7, and 8, yields
where \(f_{N_b} \equiv f(N_b/A_b)A_b\) and \(v_b \equiv {\int}_b V({\mathbf{x}}){\kern 1pt} dA/A_b\).
Bin occupation probability distributions P _{b}(N)
To arrive at the final discrete probability expression in the text, there are now two routes. One can directly insert Eq. 9 above into Eq. 2 from the main text, or one can employ Eq. 9 directly to compute ΔH to determine the probabilities for moves. In the latter case, the predicted probability distribution becomes exact so long as we interpret f′(n) in the main text at points x′ and x to represent forward and reverse finite difference derivatives \(f_{+}^{\prime} (n({\mathbf{x}}\prime )) = (f(n({\mathbf{x}}\prime ) + {\mathrm{\Delta }})  f(n({\mathbf{x}}\prime )))/{\mathrm{\Delta }}\) and \(f_{}^\prime \left( {n\left( {\mathbf{x}} \right)} \right) = \left( {f\left( {n\left( {\mathbf{x}} \right)} \right)  f\left( {n\left( {\mathbf{x}} \right)  {\mathrm{\Delta }}} \right)} \right)/{\mathrm{\Delta }}\), respectively, where Δ ≡ 1/A_{b}. Finally, because the Boltzmann factor above gives probabilities for individual arrangements of agents among bins, we must account for the multiple ways to realize a set of bin counts {N_{b}} by permuting individuals among the bins. Multiplying by the combinatorial factor N_{tot}!/(N_{1}!…N_{b}!…), we find
where Z is a normalization factor.
As described in the text, we note that the form of the joint probability distribution above predicts the occupations of different bins to be very nearly statistically independent. The only deviation from complete statistical independence comes from the constraint of a fixed total number of agents \(N_{{\mathrm{tot}}} = \mathop {\sum}\nolimits_b N_b\). Due to this constraint, the probability distribution is difficult to use in making predictions. We can overcome this difficulty using a standard technique from statistical physics. Specifically, introducing a factor \(e^{\mu N_{{\mathrm{tot}}}}\) removes the constraint without significantly affecting the calculated local distributions. As a result, the individual bin distributions then become statistically independent and of the form
In statistical physics this mathematical transformation corresponds to using a Grand Canonical Ensemble^{44} to simplify statistical calculations. Physically, this approach corresponds to relaxing the constraint of a fixed number of agents by allowing exchanges between the system being considered and a large reservoir whose vexation is controlled by μ. Mathematically, we can add and subtract a constant within the exponent, (v_{b} − c − (μ − c)) without affecting the distribution. Accordingly, we redefine v_{b} and μ with a constant shift such that v_{b} ← v_{b} − c and μ ← μ − c and, further, choose c so that μ = 0, resulting in Eq. 3 in the text. Note that motion between bins is controlled only by differences in vexations, so that none of this affects the dynamics represented in our analysis. When considering a different number of agents in the same chamber, however, μ will take on a different value and so μ − c can no longer be set to zero. Accordingly, to predict distributions for new numbers of flies, we employ Eq. 11 above and adjust μ so that the vexation of the associated reservoir fixes the new total number of flies.
Orientation and higherorder manybody interactions
Remarkably, our conclusions hold also for plausible models in which the interagent interactions are not explicitly expressed in terms of the local density n(x). To see this, we can consider the same behavioral rule of moves accepted according to probability 1/(e^{ΔH} + 1), but with H now defined as a sum of two parts,
where V(x) is the usual vexation function for the individual agents, and now U(x_{a}) is some potentially complex manybody interaction of finite range depending explicitly on the locations of all of the agents x_{a}.
As above, the form of the Markov chain associated with the move model leads directly to the Boltzmann distribution P(x_{a}) = Z^{−1}e^{−H}. To recover the frustrationvexation probability form analyzed throughout the text, we now follow the standard Statistical Mechanics approach of defining an pseudofreeenergy functional by integrating out internal degrees of freedom. Specifically, we will keep the bin occupancies constant while integrating over all arrangements of agents consistent with these occupancies. For sufficiently small bins in which vexation does not vary significantly, we again find to a good approximation \(\mathop {\sum}\nolimits_a V({\mathbf{x}}_a) = \mathop {\sum}\nolimits_b v_bN_b\), so that vexation simply gives a constant factor. Next, for sufficiently large bins, the net contributions to U(x_{a}) from interactions occurring within the bins will be large compared to the boundary effects from contributions from interactions crossing bin boundaries. Thus, we can imagine decomposing the overall interaction into a sum over the bins of the interactions just among agents a within each bin b, \(U({\mathbf{x}}_a) = \mathop {\sum}\nolimits_b U(\{{\mathbf{x}}_a\} _{a\, \in \,b})\), where we can improve accuracy by repeating the same agent locations {x_{a}}_{a∈b} in neighboring bins (socalled periodic boundary conditions).
Combining these approximations, and summing over all ways to assign agents to bins with counts {N_{b}} and over all possible locations for the agents within each bin, yields the same frustrationvexation form considered throughout the text,
where B is the total number of bins, and
defines the effective binfrustration functional f_{N} as an Ndimensional integral over the area of a single bin (with periodic boundary conditions applied to the interactions). Finally, we note that the above generalizes naturally to orientationdependent interactions by considering the coordinates {x_{a}} to include orientation, as well as spatial coordinates. If the vexation is orientationindependent, we recover precisely the form above. Otherwise, the entire framework generalizes naturally to consideration of joint locationorientation densities n(x,θ).
Experimental setup
All experiments were performed 3–15 days posteclosion using common fruit flies (D. melanogaster) from an outbred laboratory stock reared at room temperature on a 12 h/12h daynight cycle. Flies are anesthetized using CO_{2} and sorted within a few days posteclosure. We wait for 24 h after sorting before running experiments. Most observations started between 1–5 h after the light was turned on. The experiment chambers are constructed by sandwiching a 1.5 mm thick aluminum frame between two transparent acrylic sheets. The chamber is suspended above an LED light table. Holes in the upper acrylic sheet allow for the introducing flies via aspiration from above. To heat the chambers, 2 Ω highpower resistors are adhered using JB Weld to the aluminum sheet and powered by a variable power supply. On the opposite side of the sheet, a beaker of ice water is used as a heat sink. Chamber temperature is measured for two locations using a contact thermometer to ensure no more than 2 degrees Celsius drift and consistent temperature gradients between trials. We heat one side of the chamber to temperatures between 40–50 degrees Celsius^{34}. The opposing side of the chamber is connected to a heat sink and kept at temperatures between 25–35 degrees Celsius. We find that the resulting temperature gradient drives a strong avoidance behavior for the hotter wall while avoiding fly death as the flies avoid the hightemperature region. A video camera (AVT Marlin, Andover, MA) records overhead images of flies at frame rates around 30 fps and relays these images to a computer where they are analyzed by a custom MATLAB program in realtime. The entire apparatus was enclosed in a black box to prevent biases introduced by ambient light or additional visual cues.
Image analysis
To label fly centroids, images were thresholded to find fly silhouettes. For high density experiments, large groups become common and a more sophisticated approach is necessary to separate clusters, which may be as large at 10 flies. First, the images of several individual flies are combined to make a single, averaged fly mask. This mask is then convolved with images of fly groups. The best fits for these convolutions are used to approximate the locations of flies whose silhouettes overlap. (For additional details, see code provided under Code Availability statement below.) Labeling is then manually checked and we find this technique robust enough to label male flies with 0.25 % error or 1 in 400 flies mislabeled. The mating flies required extensive manual corrections due to changes in the fly postures and the polydispersity of fly sizes, since females are larger than males. For the analysis in this paper we sampled these positions at intervals of 1 s.
Due to wallexclusion effects, the area of a chamber is different from the area accessible by the centroid of a fly. We thus exclude the outer area of the chamber that corresponds to approximately half the width of a fly. Areas of the bins are then extracted using images from the experiment.
To demonstrate another method for tracking flies that only measures local densities, a simpler method was used for counting flies in the “C” shaped chamber. After thresholding, the number of pixels corresponding to a fly were summed in each bin and then a discrete fly density was assigned to each bin using knowledge of the total number of flies in the chamber. This method has the advantage of computational speed, but weights larger flies more heavily and requires reanalysis for different bin sizes.
Measurement timing and thermal ramp protocol
Observations for Fig. 3 were conducted using time intervals from approximately 5–15 min after being introduced into the chamber so that the flies could explore their new chamber and adjust to a steady state. To measure the vexation of the square experiment, we performed 12 separate single fly measurements each lasting 10 min. Similar results are obtained if three flies are used over a single 10 min period. Thus, measurements of vexation in the “C” and stair shaped chambers used two and three concurrent flies and only needed a single ten minute observation to measure the vexation.
To probe the changing fly behaviors shown in Fig. 4, we track the flies for up to 9 h before flies begin to die from deprivation^{45,46}. To test whether fly behavior is changing over our standard 10 minute time windows, we compare the probabilities, P_{b}(N), from the first 5 min of the window with the last 5 min and find that they are consistent. The only exception to this is during the very first 5 min after the flies are introduced into the chamber as they become oriented to their new environment that we do not include in our analysis. To elicit different behaviors and location preferences with the same population of flies, we apply a heat gradient to generate an avoidance behavior^{34} starting at 20 min after being introduced to the chamber. By minute 30, the chamber has reached a steady temperature and we observe that the flies exhibit an approximately constant average distribution. At minute 40, we turn the heat off and let it adjust to room temperature for the remainder of the experiment. Throughout these observations, we qualitatively observe several different behaviors. For the first 5 min, flies are most active and their frustration has a slightly higher positive curvature than the frustration for the 5–15 min period. When the chamber is heated, the frustration stays approximately the same despite the drastic change in the vexation. After the chamber cools down, flies enter a readjustment phase where they are much less active. After this readjustment phase, however, flies again exhibit behavior similar to that from the 5–15 min interval. By 6 h, flies in all the experiments switch to a grouping behavior as shown in Fig. 4.
Validation of assumptions underlying theoretical analysis
As mentioned above, we made some general assumptions developing our theory which we now validate for the walking fly system. First, to verify attainment of equilibrium and sufficient ergodicity, we consider the normalized autocorrelation function
where <…>_{t} indicates average over all times. This function shows the expected rapid exponential decay (Fig. 2c), and has an integral which gives the decorrelation time τ = 0.92 s. Indeed, we find this time to be quite short, typically on the order of a few seconds, for all of our experimental runs. This decay time is two orders of magnitude faster than the typical run time and does not vary significantly when computed in different time subwindows, strongly suggesting rapid mixing and stationarity of the random process, thereby allowing the interchange of time and ensemble averages, and establishing the existence of equilibrium in the timescales under study. Our videos thus represent hundreds of independent samples drawn from the equilibrium ensemble underlying our analysis.
We next consider whether the bins are truly independently distributed as expected in Eq. 3. Accordingly, we consider the normalized timeaveraged spatialcorrelation function
where <…>_{b,t} indicates average all times and bins, and Δ is the twodimensional vector displacement between bins (Fig. 2b)). The data show essentially no correlation between bins, thereby verifying the product form of the global bin distribution function in Eq. 3 in the main text. This confirms not only that we have chosen appropriately sized bins but also, more fundamentally, establishes that there are little or no fly–fly interaction effects between bins, so that the local density approximation (LDA) form for the frustration, \(F[n({\mathbf{x}})] = {\int} f(n({\mathbf{x}}))dA\), indeed gives a good representation of the behavior of the fly populations at scales greater than 0.15 cm^{2}.
Parameter estimation
To estimate the frustration and vexation for the crowds in our experiments, we start by constructing the posterior function P(f_{N}, v_{b}N_{b}(t)), which represents the relative likelihood of different parameter choices for our model given the data (number counts within each bin) that has actually been observed. Then, to find the a posteriori estimate of the parameters, we maximize this likelihood by performing a numerical gradient minimization of
where C is an irrelevant normalization constant, B corresponds to the total number of bins in the system, T the total number of independent time samples employed, and 〈…〉_{b} and 〈…〉_{b,t} represent averages over either all bins or bins and times, respectively. Finally, for the last two terms, σ accounts for the range about zero of a Gaussian prior distribution on the frustration and vexation parameters. This Gaussian prior distribution reflects the fact that the frustration and vexation parameters v_{b} and f_{N} can in principle take any real value, but in practice generally fall in a range on the order of from about −15 to 15 because these parameters enter as exponentials in our probability models. Because the amount of data that we handle is on the order of tens of thousands of frames, the likelihood peaks strongly around its maximum, and the precise form of the Gaussian prior is largely irrelevant. Indeed, changing the value of σ from a reasonable value of 15 to an unreasonably small value of 1, only changes our final results for the frustration by 11.4%. Throughout the rest of our work, we take σ = 15.
Uncertainty in parameter estimation
The sharp peaks associated with the large amount of data ensure the accuracy of the asymptotic Gaussian approximation, in which the joint probability distribution representing the range of parameters supported by the data is a multivariate Gaussian distribution. As a result, the associated covariance matrix of uncertainties in the parameters is the inverse of the Fisher information matrix I (i.e., the second derivative of −lnP evaluated at the location of its maximum). The matrices of parameter uncertainties and crosscorrelations among them are computed as follows. For our full DFFT model, with vexation and frustration, and the simple Poisson model, with vexation only, we calculate the inverses of the following matrices, respectively,
and
where the matrix elements of each block are
Here, P_{b}(N) is defined as in Eq. 3 in the main text, T again represents the total number of independent time frames, and the “ ~ ” indicates internal summation indices.
Finally, a subtle, but important, ambiguity arises in the extraction of frustrations and vexations. Specifically, because the exponent in the observed probabilities for each bin takes the form (ln z_{b} + v_{b}N + f_{N}), making the replacements (v_{b} → v_{b} − α; z_{b} → z_{b} − β; f_{N} → f_{N} + β + αN;) leaves the predictions of the model unchanged, and any choice of parameters corresponding to these replacements represents the data equally well. As a result, the Fisher matrices described above are singular. To resolve this “gauge invariance” and remove the singularity, we must break the symmetry among equivalent models by adding two constraints (one for α and one for β) to our choice of f_{N}. Here, we do this by enforcing the natural choice that f_{0} ≡ 0 and f_{1} ≡ 0, corresponding to the convention that that the frustration does not affect the probability for bins with either N = 0 or N = 1 flies. Finally, in terms of the information matrices above, implementing this constraint corresponds to dropping the first two rows and columns associated with these parameters from the I_{DFFT} matrix.
Uncertainty in predictions of average occupations
With the uncertainties in the extraction of the vexation and frustration parameters from above, we next determined the uncertainties in our predictions of the average bin occupations for large populations in new arenas. The predicted mean densities are
where the normalization is
where P_{b}(N) is the probability of having N flies in bin b, v_{b} is the vexation in bin b, and f_{N} is the frustration associated with having N flies in a bin. We accordingly computed the associated uncertainties using standard linearized error propagation as
where var(X) and covar(X, Y) represent the variance of random variable X and covariance between X and Y, respectively, as determined by the inverse of the Fisher information matrix as discussed above. Finally, the derivatives needed in Eq. 25 are
and
where \(\left\langle {N_b^2} \right\rangle \equiv \mathop {\sum}\nolimits_N N^2P_b(N)\) with P_{b}(N) as defined above.
A few technical notes are in order to understand the terms present in Eq. 25. First, note that crosscorrelations between vexations in different bins are not relevant because \(\bar N_b\) depends solely on v_{b} and not on vexations from other bins. Also, crosscorrelations between extracted vexations v_{b} and frustrations f_{N} are zero in our case because we extract the vexations and frustrations from different, and thus independent, experiments when making our predictions for average occupations. Finally, the uncertainties in f_{0} and f_{1} are not included because these uncertainties are zero due to the gauge choice discussed in the section above.
Uncertainty in experimentally measured bin statistics
For each independent bin, we obtain from the experiment a sequence of length N_{T} with elements each corresponding to a bin occupation that can range from zero to the maximum packing of files, N = 0,…,N_{max}. From this data, we hope to extract probability parameters p_{N} describing the bin occupation distributions studied in the main text. For simplicity of notation, we here use lower case p to denote experimentally measured probabilities.
To account for timecorrelations in bin occupancies, particularly at high frame rates, we downsample at intervals given by the decorrelation time τ and actually consider uncorrelated sequences of length T = N_{T}/τ. The data then correspond to the result of a random process of making T independent selections among N_{max} + 1 possible bin occupations. Thus, for each bin, the probability of observing a given data sequence becomes the multinomial distribution,
where h_{N} represents the number of times (“hits”) we observe each of the possible occupancies N.
To extract the underlying uncertainties, we note that Bayes’ theorem gives the following distribution for the probability parameters to take the values {p_{N}} given the actually observed counts {h_{N}},
This posterior probability is proportional to an undetermined prior probability P({p_{N}}) describing our a priori expectations for the values of the {p_{N}} parameters. However, as per our discussion surrounding Eq. 17 above, in the large T limit, the Poissonlike product factor in Eq. 29 above will be highly peaked, and the unknown prior P({p_{N}}) will not have a substantial effect on the posterior distribution.
To completely eliminate the effects of unwarranted assumptions entering through our choice of prior, we assume an uninformative prior distribution that is consistent with the invariance of the probability values under the inclusion of new samples, and choose the multivariate generalization of Haldane’s uninformative improper prior distribution^{47},
With this choice, upon normalization, Eq. 29 becomes the Dirichlet distribution,
where Γ(x) is the Gamma function. This distribution yields expected values for the probabilities equal precisely to the observed frequencies \(\bar p_N = h_N/T\). The variances of this distribution, then give our desired uncertainties,
Note that when T is large and \(\bar p_N \ll 1\), the uncertainties correspond to what we would naïvely expect from Poisson counting, namely an uncertainty of \(\sqrt {h_N}\) in the counts, corresponding to an uncertainty of \(\sqrt {h_N} /T = \sqrt {\overline{p}_N/T}\) in the extracted probabilities. Such an analysis, however, misses the important factor of \(\sqrt {1  \overline{p}_N}\) and leads to significant errors in our case.
Finally, for the uncertainty in the experimental average occupation \(\bar N_{{\mathrm{exp}}t} = \mathop {\sum}\nolimits_N Np_N\), the corresponding variance is
where the needed covariances of the Dirichlet distribution are
Code availability
Readers can access the code related to parameter estimation and crowd density predictions by going to (https://github.com/MendezV/DFFT) or to (https://doi.org/10.5281/zenodo.1285931). Readers can also access code related to image analysis procedures by visitng (https://github.com/yunuskink/Fitflyflytracking) or (https://doi.org/10.5281/zenodo.1304326). There are no access restrictions to this software.
Data availability
The fly density data that support the findings of this study are available in the Open Science Framework database at (https://doi.org/10.17605/OSF.IO/7UBZ2).
References
Silverberg, J. L., Bierbaum, M., Sethna, J. P. & Cohen, I. Collective motion of humans in mosh and circle pits at heavy metal concerts. Phys. Rev. Lett. 110, 228701 (2013).
Helbing, D., Farkas, I. & Vicsek, T. Simulating dynamical features of escape panic. Nature 407, 487 (2000).
Bellomo, N. & Dogbe, C. On the modeling of traffic and crowds: a survey of models, speculations, and perspectives. SIAM Rev. 53, 409–463 (2011).
Leonard, N. E. & Fiorelli, E. Virtual leaders, artificial potentials and coordinated control of groups. Decision and Control, 2001. In Proceedings of the 40th IEEE Conference on, Vol. 3, 2968–2973. (IEEE, 2001).
Couzin, I. D., Krause, J., Franks, N. R. & Levin, S. A. Effective leadership and decisionmaking in animal groups on the move. Nature 433, 513 (2005).
Vicsek, T., Czirók, A., BenJacob, E., Cohen, I. & Shochet, O. Novel type of phase transition in a system of selfdriven particles. Phys. Rev. Lett. 75, 1226 (1995).
Moussad, M., Helbing, D. & Theraulaz, G. How simple rules determine pedestrian behavior and crowd disasters. Proc. Natl Acad. Sci. USA 108, 6884–6888 (2011).
An, L. Modeling human decisions in coupled human and natural systems: review of agentbased models. Ecol. Model. 229, 25–36 (2012).
Zheng, X., Zhong, T. & Liu, M. Modeling crowd evacuation of a building based on seven methodological approaches. Build. Environ. 44, 437–445 (2009).
Treuille, A., Cooper, S. & Popović, Z. Continuum crowds. ACM Trans. Graph. 25, 1160–1168 (2006).
Hinz, R. C. & de Polavieja, G. G. Ontogeny of collective behavior reveals a simple attraction rule. Proc. Natl Acad. Sci. USA 114, 2295–2300 (2017).
Katz, Y., Tunstrøm, K., Ioannou, C. C., Huepe, C. & Couzin, I. D. Inferring the structure and dynamics of interactions in schooling fish. Proc. Natl Acad. Sci. USA 108, 18720–18725 (2011).
Ballerini, M. et al. Interaction ruling animal collective behavior depends on topological rather than metric distance: evidence from a field study. Proc. Natl Acad. Sci. USA 105, 1232–1237 (2008).
Berman, G. J., Choi, D. M., Bialek, W. & Shaevitz, J. W. Mapping the stereotyped behaviour of freely moving fruit flies. J. R. Soc. Interface 11, 20140672 (2014).
Kelley, D. H. & Ouellette, N. T. Emergent dynamics of laboratory insect swarms. Sci. Rep., 3, 1073 (2013).
Boltzmann, L. Über die mechanische Bedeutung des zweiten Hauptsatzes der Wärmetheorie:(vorgelegt in der Sitzung am 8 February 1866). (Staatsdruckerei, 1866).
Diderik Van der Waals, J. Over de Continuiteit van den Gasen Vloeistoftoestand, volume 1. Sijthoff, 1873.
Toner, J. & Tu, Y. Flocks, herds, and schools: a quantitative theory of flocking. Phys. Rev. E 58, 4828 (1998).
Mora, T. & Bialek, W. Are biological systems poised at criticality? J. Stat. Phys. 144, 268–302 (2011).
Cavagna, A. et al. Dynamic scaling in natural swarms. Nat. Phys. 13, 914 (2017).
Tennenbaum, M., Liu, Z., Hu, D. & FernandezNieves, A. Mechanics of fire ant aggregations. Nat. Mater. 15, 54 (2016).
Sinhuber, Ml & Ouellette, N. T. Phase coexistence in insect swarms. Phys. Rev. Lett. 119, 178003 (2017).
Solon, A. P. et al. Pressure is not a state function for generic active fluids. Nat. Phys. 11, 673 (2015).
Sham, L. J. & Kohn, W. Oneparticle properties of an inhomogeneous interacting electron gas. Phys. Rev. 145, 561 (1966).
Hohenberg, P. & Kohn, W. Inhomogeneous electron gas. Phys. Rev. 136(3B), B864 (1964).
Diggle, P. J. Statistical Analysis of Spatial and SpatioTemporal Point Patterns, Third Edition. Chapman & Hall/CRC Monographs on Statistics and Applied Probability. (CRC Press, 2013).
Kim, I. S. & Dickinson, M. H. Idiothetic path integration in the fruit fly Drosophila melanogaster. Curr. Biol. 27, 2227–2238 (2017).
Neyman, J. On a new class of contagious distributions, applicable in entomology and bacteriology. Ann. Math. Statist. 10, 35–57 (1939).
Taylor, L. R. Aggregation, variance and the mean. Nature 189, 732–735 (1961).
Besson, M. & Martin, J.R. Centrophobism/thigmotaxis, a new role for the mushroom bodies in Drosophila. Dev. Neurobiol. 62, 386–396 (2005).
Ramdya, P. et al. Mechanosensory interactions drive collective behaviour in Drosophila. Nature 519, 233 (2015).
Schneider, J., Atallah, J. & Levine, J. D. 3 one, two, and many—a perspective on what groups of Drosophila melanogaster can tell us about social dynamics. Adv. Genet. 77, 59 (2012).
Schneider, J., Dickinson, M. H. & Levine, J. D. Social structures depend on innate determinants and chemosensory processing in Drosophila. Proc. Natl Acad. Sci. USA 109, 17174–17179 (2012).
Zars, M. & Zars, T. High and low temperatures have unequal reinforcing properties in Drosophila spatial learning. J. Comp. Physiol. A. 192, 727 (2006).
Ramdya, P., Schneider, J. & Levine, J. D. The neurogenetics of group behavior in Drosophila melanogaster. J. Exp. Biol. 220, 35–41 (2017).
Runge, E. & Gross, E. K. U. Densityfunctional theory for timedependent systems. Phys. Rev. Lett. 52, 997 (1984).
Chan, G. K.L. & Finken, R. Timedependent density functional theory of classical fluids. Phys. Rev. Lett. 94, 183001 (2005).
Capitani, J. F., Nalewajski, R. F. & Parr, R. G. NonBorn–Oppenheimer density functional theory of molecular systems. J. Chem. Phys. 76, 568–573 (1982).
Wesolowski, A. et al. Quantifying the impact of human mobility on malaria. Science 338, 267–270 (2012).
Hauer, M. E. Migration induced by sealevel rise could reshape the US population landscape. Nat. Clim. Change 7, 321–325 (2017).
Clark, P. U. et al. Consequences of twentyfirstcentury policy for multimillennial climate and sealevel change. Nat. Clim. Chang. 6, 360–369 (2016).
Metropolis, N., Rosenbluth, A. W., Rosenbluth, M. N., Teller, A. H. & Teller, E. Equation of state calculations by fast computing machines. J. Chem. Phys. 21, 1087–1092 (1953).
Barker, A. A. Monte Carlo calculations of the radial distribution functions for a protonelectron plasma. Aust. J. Phys. 18, 119–134 (1965).
Reif, F. Chapter 8, Fundamentals of Statistical and Thermal Physics. (McGraw Hill, New York, 1965).
Ji, F. & Zhu, Y. A novel assay reveals hygrotactic behavior in Drosophila. PLoS. One. 10, e0119162 (2015).
Bell, W. J., Cathy, T., Roggero, R. J., Kipp, L. R. & Tobin, T. R. Sucrosestimulated searching behaviour of Drosophila melanogaster in a uniform habitat: modulation by period of deprivation. Anim. Behav. 33, 436–448 (1985).
Haldane, J. A note on inverse probability. Math. Proc. Camb. Philos. Soc. 28, 55–61 (1932).
Acknowledgements
The authors thank Xiaoning Wang, MarcAntoine Bouvattier, Alonso Botero, Tom Corwin, Tom Mifflin, Greg Godfrey, and Nathan Sitaraman for their help with the initial stages of the project. We further thank the Cohen and Arias groups for discussions throughout this work. The work was primarily funded by the Army Research Office ArmyARO W911NF1610433. J.F.M. was also supported in part by an Office of the Vice President for Research at the University of Los Andes. Y.K. was also supported in part by funding from the National Science Foundation Graduate Research Fellowship Award No. DGE1650441.
Author information
Authors and Affiliations
Contributions
J.F.M.V.: Development and implementation of analyses to extract vexations, frustrations, and predicted mean occupations. Analysis of statistical uncertainties in all of these quantities and also in the extraction of binoccupancy distributions from experimental data. Final display format for crosscorrelating predicted mean occupancies with predictions. Theoretical parts of the Methods Section. Significant contributions to main text. Y.A.K.: Design and implementation of experiments along with development of image analysis techniques. Running data through analyses provided by Méndez Valderrama. Experimental parts of the Methods Section, design, and implementation of figures, and an early draft of the manuscript. Significant contributions to main text. Y.A.K. and J.F.M.V. contributed equally to this work. J.S.: Codevelopment of underlying Markov chain, proper accounting for degeneracy factor, identification of multinomial and Poisson distributions for the noninteracting case, initiation of use of maximumlikelihood estimation and Bayesian uncertainty techniques. I.C.: Significant input into design of experiments, and primary responsibility for main text. T.A.A.: Development of underlying motion model, densityfunctional theory analysis, and prediction of form of population fluctuations. Codevelopment of underlying Markov chain. Complete early draft of manuscript, and significant contributions to main text.
Corresponding author
Ethics declarations
Competing interests
The authors declare no competing interests.
Additional information
Publisher's note: Springer Nature remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.
Electronic supplementary material
Rights and permissions
Open Access This article is licensed under a Creative Commons Attribution 4.0 International License, which permits use, sharing, adaptation, distribution and reproduction in any medium or format, as long as you give appropriate credit to the original author(s) and the source, provide a link to the Creative Commons license, and indicate if changes were made. The images or other third party material in this article are included in the article’s Creative Commons license, unless indicated otherwise in a credit line to the material. If material is not included in the article’s Creative Commons license and your intended use is not permitted by statutory regulation or exceeds the permitted use, you will need to obtain permission directly from the copyright holder. To view a copy of this license, visit http://creativecommons.org/licenses/by/4.0/.
About this article
Cite this article
MéndezValderrama, J.F., Kinkhabwala, Y.A., Silver, J. et al. Densityfunctional fluctuation theory of crowds. Nat Commun 9, 3538 (2018). https://doi.org/10.1038/s4146701805750z
Received:
Accepted:
Published:
DOI: https://doi.org/10.1038/s4146701805750z
This article is cited by

A density functional theory for ecology across scales
Nature Communications (2023)

Synchronization of complex human networks
Nature Communications (2020)

Characterizing reticulation in online social networks during disasters
Applied Network Science (2020)

Effects of Urban Atmospheres on Changing Attitudes of Crowded Public Places: An Action Plan
International Journal of Community WellBeing (2020)

Correcting locomotion dependent observation biases in thermal preference of Drosophila
Scientific Reports (2019)
Comments
By submitting a comment you agree to abide by our Terms and Community Guidelines. If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.