A framework for studying behavioral evolution by reconstructing ancestral repertoires

Although different animal species often exhibit extensive variation in many behaviors, typically scientists examine one or a small number of behaviors in any single study. Here, we propose a new framework to simultaneously study the evolution of many behaviors. We measured the behavioral repertoire of individuals from six species of fruit flies using unsupervised techniques and identified all stereotyped movements exhibited by each species. We then fit a Generalized Linear Mixed Model to estimate the intra- and inter-species behavioral covariances, and, by using the known phylogenetic relationships among species, we estimated the (unobserved) behaviors exhibited by ancestral species. We found that much of intra-specific behavioral variation has a similar covariance structure to previously described long-time scale variation in an individual’s behavior, suggesting that much of the measured variation between individuals of a single species in our assay reflects differences in the status of neural networks, rather than genetic or developmental differences between individuals. We then propose a method to identify groups of behaviors that appear to have evolved in a correlated manner, illustrating how sets of behaviors, rather than individual behaviors, likely evolved. Our approach provides a new framework for identifying co-evolving behaviors and may provide new opportunities to study the mechanistic basis of behavioral evolution.


Introduction
Behavior is one of the most variable and rapidly evolving phenotypes, with notable differences even between closely related species (Lorenz, 1958;Martins, 1996). Variable behaviors and rapid behavioral evolution likely facilitates adaptation to new or varying environments and speciation (Baier and Hoekstra, 1914;West-Eberhard, 2003). Despite the importance of animal behavior, progress in revealing the genetic basis of behavioral evolution has been slow (Gleason and Ritchie, 2004;Yamamoto and Ishikawa, 2013;Ellison et al., 2011;Shaw and Lesnick, 2009). In contrast, recent decades have seen significant progress in understanding the genetic causes of morphological evolution (Williams and Carroll, 2009;Shubin et al., 2009;Levine and Davidson, 2005;Stern and Frankel, 2013).
While there are many potential reasons for the discrepancy between studies of behavioral and morphological evolution, including the lack of a fossil record for behavior, a key difficulty has been identifying which aspects of an animal's development and physiology are the proximate causes of behavior evolution. Evolutionary changes in behavior could emerge from alterations in the developmental patterning of neural circuits (e.g., brain networks, descending commands, central pattern generators), changes in hormonal regulation that influence neural activity, or even from changes in non-neuronal morphology (Baker et al., 2001;Massey et al., 2019). Each of these possibilities could result in behavioral effects at different, yet overlapping, timescales -from muscle twitches to stereotyped suites of behaviors to longer-lived states like foraging or courtship or aging that may control the relative frequency of a given behavior. This complexity may make it difficult to identify the precise aspects of behavior that have evolved.
To address these difficulties, the standard approach in the genetic study of behavioral evolution has been to identify focal behaviors that exhibit robust differences between species, such as courtship behavior in fruit flies (Cande et al., 2012;Cande et al., 2014;Ding et al., 2019) or burrow formation in deermice (Weber et al., 2013;Hu and Hoekstra, 2017). It has been possible to identify genomic regions that correlate with quantitative changes in focal behaviors. However, usually multiple genomic regions are identified, each containing many genes. Given the large number of putative genes involved, combined with the possibility of epistatic interactions between loci, identification of the contributions of individual genes to behavioral evolution has progressed slowly.
An alternative approach to focusing on single behaviors is to examine the full repertoire of movements that an animal performs. By identifying sets of behaviors that evolve together, as was recently performed for hand-tuned traits in a study of birds-of-paradise evolution (Ligon et al., 2018), it may be possible to identify regulators of these suites of behaviors. This approach has been thwarted by the challenge of robustly measuring multiple behavioral phenotypes simultaneously. Recent progress in the unsupervised identification of animal behaviors across length and time scales, however, has made this approach possible (Berman, 2018;Brown and de Bivort, 2018). In this study, we introduce a quantitative framework for studying the evolutionary dynamics of large suites of behavior. We have focused initially on fruit flies, which provide a convenient model for this problem -both because they exhibit a wide range of complex behaviors and because unsupervised approaches can be used to map all of the animal movements captured in video recordings (Berman et al., 2014;Cande et al., 2018;Berman et al., 2016).
We recorded movies of isolated male flies from six species in a nearly stimulus-deprived environment. Because we did not record flies experiencing social and other environmental cues, we did not observe many charismatic natural behaviors, such as courtship and aggression. Nevertheless, we found that the behaviors they performed, including walking and grooming, contain species-specific information. We thus hypothesized that our quantitative representations of behaviors could be studied in an evolutionary context. To infer the evolutionary trajectories of behavioral evolution, we estimated ancestral behavioral repertoires with a Generalized Linear Mixed Model (GLMM) approach (Hadfield, 2010), which builds upon Felsenstein's approach to reconstructing ancestral states (Felsenstein, 1985;Felsenstein, 2005;Hadfield and Nakagawa, 2010;O'Meara, 2012). Using these results, we develop a framework that allows us to model the behavioral traits that co-vary both within a species and along the phylogeny. We found that within-species variance has a similar structure to long-lasting internal states of the animal that we characterized previously, and that inter-species variance can capture how disparate behaviors may have evolved together. This latter finding points toward the presence of higher order behavioral traits that would not have been detected by studying individual behaviors in isolation and that may be amenable to further evolutionary and genetic analysis.

Experiments and behavioral quantification
We captured video recordings of all behaviors performed by single flies isolated in a largely featureless environment for multiple individuals from six species of the Drosophila melanogaster species subgroup: D. mauritiana, D. melanogaster, D. santomea, D. sechellia, D. simulans, and D. yakuba (Cande et al., 2018). Although the animals could not jump or fly in these chambers and were not expected to exhibit social or feeding behaviors, the flies displayed a variety of complex behaviors, including locomotion and grooming. Each of these behaviors involves multiple body parts that move at varying time scales. The species studied here were chosen because their phylogenetic relationships are well understood (Clark et al., 2007;Obbard et al., 2012;Chyb and Gompel, 2013;Seetharam and Stuart, 2013) (summarized in the tree seen in Figure 3), and genetic tools are available for most of these species (Stern et al., 2017). Since a single strain represents a genomic 'snapshot' of each species, we assayed multiple individuals from each of multiple strains of each species to attempt to capture species-specific differences, and not variation specific to particular strains (see Materials and methods). In total, we collected data from 561 flies, each measured for an hour at a sampling rate of 100 Hz.
While previous studies have identified differences in specific behaviors, such as courtship behavior, between these species (Cande et al., 2012;Ding et al., 2019;Yamamoto and Ishikawa, 2013;Auer and Benton, 2016), here we assayed the full repertoire of behaviors the flies performed in the arena, with the aim of identifying combinations of behaviors that may be evolving together. To measure this repertoire, we used a previously described behavior mapping method (Berman et al., 2014;Cande et al., 2018) that starts from raw video images and finds each animal's stereotyped movements in an unsupervised manner. The output of this method is a two-dimensional probability density function (PDF) that contains many peaks and valleys ( Figure 1A), where each peak corresponds to a different stereotyped behavior (e.g., right wing grooming, proboscis extension, running, etc).
Briefly, to create the density plots, raw video images were rotationally and translationally aligned to create an egocentric frame for the fly. The transformed images were decomposed using Principal Components Analysis into a low-dimensional set of time series. For each of these postural mode time series, a Morlet wavelet transform was applied, obtaining a local spectrogram between 1 Hz and 50 Hz (the Nyquist frequency). After normalization, each point in time was mapped using t-SNE (van der Maaten and Hinton, 2008) into a two-dimensional plane. Finally, convolving these points with a two-dimensional gaussian and applying the watershed transform (Meyer, 1994), produced 134 different regions, each of these containing a single local maximum of probability density that corresponds to a particular stereotypical behavior. We integrate over this local region of the probability density to calculate the probability that a fly is performing this behavior at a random point in time. Thus, we can associate each fly with a 134-dimensional real-valued vector that represents the probability of the fly performing a certain stereotyped behavior at a given time during the hour-long experimental session. We will refer to this quantity as the animal's behavioral vector,P.
The behavioral map averaged across all six species is shown in Figure 1A and displays a pattern of movements similar to those we found in previous work, where locomotion, idle/slow, anterior/ posterior movements, etc. are segregated into different regions (Berman et al., 2014;Cande et al., 2018). Averaging across all individuals of each species, we found the mean behavioral vector for each species ( Figure 1B) and observed that each species performs certain behaviors with different probabilities. For example, D. mauritiana individuals spend more time performing fast locomotion than all other species on average, and D. yakuba individuals spend much of their time performing an almost species-unique type of slow locomotion, but little time running quickly.
These average probability maps provide some insight into potential species differences, but to identify species-specific behaviors, we also need to account for variation in the probability that individuals of each species perform each behavior. One way to address this problem is to ask whether an individual's species identity can be predicted solely from its multi-dimensional behavioral vector. To explore this question, we first used t-SNE to project all 561 individuals into a two-dimensional plane (Figure 2A), using the Jensen-Shannon divergence as the distance metric between individual behavioral vectors. In this plot, different colors represent different species, and different symbols with the same color represent different strains within the same species. Although species do not segment cleanly into separate regions of this plane, the distribution of species is far from random, with individuals from the same species tending to group near to one other. Given this structure, there is likely species-specific information in the behavioral vectors.
To quantify this observation, we applied a multinomial logistic regression classifier to the data, performing a six-way classification based solely on the high-dimensional behavioral vectors. After training, the classifier correctly classified 89 AE :2% of vectors in our test set (a randomly selected 30% of the entire data set that was not used during training). Moreover, the confusion matrix ( Figure 2B) revealed no systematic misclassification bias amongst the species. Note that we have used a relatively simple classifier compared to modern deep learning methods (Goodfellow et al., 2016), so these results likely represent a lower bound on the distinguishability of the behavioral vectors. Thus, the behavioral vectors contain considerable species-specific information. We therefore proceeded to explore how these behavioral vectors may have evolved along the phylogeny.
A B Figure 2. Classification of fly species based on behavioral repertoires. (A) A t-SNE embedding of the behavioral repertoires shows that behavioral repertoires contain some species-specific information. Each dot represents one individual fly, with different colors representing different species and different symbols with the same color representing different strains within the same species. The distance matrix (561 by 561) used to create the embedding is the Jensen-Shannon divergence between the behavioral densities of individual flies. (B) Confusion matrix for the logistic regression with each row normalized. All the values are averaged from 100 different trials. The standard error is less than 0.01 for the diagonal elements and less than 0.005 for each of the off-diagonal elements.

Reconstructing ancestral behavioral repertoires
Multiple methods have been proposed for reconstructing ancestral states from data collected from extant species (Felsenstein, 1985;Felsenstein, 2005;Yang, 2006;O'Meara, 2012;Royer-Carenzi and Didier, 2016). These methods generally fall into two camps: parsimony reconstruction, which attempts to reconstruct evolutionary history with the fewest number of evolutionary changes (Cunningham et al., 1998), and diffusion-processes, which model evolution as a random walk on a multi-dimensional landscape (Hadfield and Nakagawa, 2010). Given the high-dimensional behavioral vectors that we are attempting to model, a diffusion process is more likely to capture the intertrait correlations that we would like to understand. Thus, we focus on a diffusion-based model here. Given a phylogeny for a collection of species, we modeled how species-specific complexes of behaviors might have emerged. We assumed that each animal's behavior is a quantitative trait with an additive random effect, that is, each animal's behavior is a trait that results from the additive effects of many genetic loci, each of small effect, that is combined with a non-genetic effect that represents inter-specific variation. We do not, however, assume that all behaviors evolve independently of each other. Thus, we are interested in predicting (1) whether intra-and inter-species variation can be separated to identify independently evolving sets or linear combinations of behaviors and (2) how behaviors co-vary along the phylogeny, potentially revealing co-evolving suites of behaviors.
We assumed that the observed flies' behaviors evolved via a diffusion process with Gaussian noise from a common ancestor along the known phylogenetic tree. Note that this is a less restrictive assumption than neutrality, as multiple traits under selection may evolve in a correlated manner. Specifically, we fit a multi-response Generalized Linear Mixed Model (GLMM) to the data, using the approach described in Hadfield, 2010, modeling the evolutionary process such that the logarithm of the behavioral vector,P, for each individual (l ¼ ðl 1 ; :::; l K¼134 Þ), is given bỹ (1) where is the mean behavior of the common ancestor (treated as the fixed effects of this model), and andẽ are the random effects corresponding to the phylogenetic and individual variability, respectively. We assume that these random effects are generated from the multi-dimensional normal distributions N ð0; A V ðaÞ Þ (phylogenetic) and N ð0; I V ðeÞ Þ (individual). Here, the matrix A represents the information contained in the phylogenetic tree, with A ij being proportional to the length of the path from the most recent common ancestor of species i and j to the common ancestor. A is normalized so that the diagonal elements are all equal to 1. Therefore, A ij represents the phylogenetic similarity between a pair of species. I is the identity matrix, and V ðaÞ and V ðeÞ are the phylogenetic and within-species covariance matrices, respectively. We fit m, V ðaÞ , and V ðeÞ using Markov Chain Monte Carlo (MCMC) simulations, confirming that the MCMC converged using the Gelman-Rubin diagnostic (see Materials and methods, Figure 3-figure supplement 1). In addition, our model is able to infer the mean endpoint behavioral repertoires (Figure 3-figure supplement 2), providing confidence that our model is consistent with our input data. In addition to the inferred behavioral states corresponding to the common ancestor, P Anc , we also reconstructed the mean behavioral representations for the intermediate ancestors ( Figure 3).
We also found that the model that allows behavioral co-evolution out-performs a model where each behavioral trait evolves independently. Specifically, we fit a model where behavioral correlations between individuals of different species were removed by enforcing that V ðaÞ and V ðeÞ must be diagonal matrices, a reduction of more than 17,500 parameters compared to the full model (see Materials and methods for details). The phylogenetic ancestral reconstruction was then made for each behavioral trait separately. To compare the relative performance of these models, we used the Deviance Information Criterion (DIC) (Spiegelhalter et al., 2002), a commonly used assessment tool for MCMC-fit hierarchical models that lack a good estimate for the number of effective parameters. Like other information-theoretic model selection criteria (e.g., the Akaike Information Criterion or the Bayesian Information Criterion), smaller values of the DIC imply a larger posterior probability of the model given the available data. Despite the large reduction in the number of parameters for the independent-trait model, the DIC for the independent-trait model was substantially higher (DIC ¼ ð242 AE 2Þ Â 10 3 ) than the DIC for the full model (DIC ¼ ð114 AE 2Þ Â 10 3 ). Moreover, the full model was able to predict the inter-and intra-species covariances between dissimilar pairs of behaviors ( Figure 3-figure supplement 3). Hence, modeling the evolution of the full behavioral repertoire captures the structure of the observed data better than a single trait approach.

Individual variability and long timescale correlations
While it is not possible to directly test the accuracy of our ancestral state reconstructions (Figure 3), the inferred covariance matrices generate predictions about behavioral and genetic correlations that are, in principle, testable. We therefore focus on the fitted covariance matrices, V ðeÞ and V ðaÞ (each in IR 134Â134 ), which account for within-species and phylogenetic random effects, respectively.
We will focus first on the intra-species covariance matrix, V ðeÞ . We note first that the matrix exhibits a modular structure ( Figure 4A). After rearranging the behavior order via an information-based clustering procedure (Slonim et al., 2005), we see that a block diagonal pattern emerges, with positive correlations lying within the blocks and negative correlations lying off the diagonal. The details of this particular clustering approach are described in Materials and methods, but we find that the results are nearly identical for several different clustering methodologies (Figure 4-figure supplement 1). Quantifying the matrix's modularity via the average within-cluster dissimilarity, where C k is the set of all behaviors belonging to the k th cluster, we find that hdi » 0:30 and 0.22 for the 3-and 6-cluster solutions, respectively. These values are significantly smaller than the average distances obtained using random cluster assignments (hdi ¼ 0:46 AE 0:03 and 0:45 AE 0:04 for 3 and 6 Behavior density   clusters respectively, see Figure 4-figure supplement 2 for other numbers of clusters and clustering methods). Thus, we can conclude that the intra-specific covariance matrix has a far-from-random modular structure, implying that between individuals of the same species, groups of behaviors tend to vary together in a stereotyped manner.
Moreover, these groups of behaviors that co-vary together within a species are not random collections of behaviors. Instead, we found that co-varying clusters are spatially contiguous in the behavioral map, implying that covariances of groups of similar behaviors (behaviors involving moving similar parts of the animals' bodies at similar speeds) compose much of the observed intra-species variance. The clustering method does not take the spatial structure of the behavioral map into account at all (just the values in V ðeÞ ), so the clusters of local behaviors in the behavior map reflect underlying similarity in the covariance of nearby behaviors, rather than an artifact of the algorithm. Moreover, co-varying clusters are hierarchically organized, where coarse-grained co-varying behaviors can be sub-divided into smaller co-varying clusters ( Figure 4B), a feature that is not guaranteed by the information-based clustering algorithm.
This hierarchical structure of the behavioral map is reminiscent of the hierarchical temporal structure of behavior that was hypothesized originally by ethologists (Tinbergen, 1951;Deutsch et al., 2020) and was observed to optimally explain the history-dependent long timescale non-stationary structure of Drosophila melanogaster behavioral transitions (Berman et al., 2016). Thus, we hypothesized that the structure of the intra-species covariance matrix might be linked to deviations from statistical stationarity in the behavioral data that were not explicitly measured in the unsupervised clustering or modeled in the GLMM.   To explore this connection further, we performed an analysis that is analogous to the single-species study in Berman et al., 2016, finding coarse-grainings of the behavioral space (i.e., a description of the behavioral space using fewer behaviors) that are optimally predictive of the future behaviors that the flies perform. Specifically, if bðtÞ is the behavior that a fly performs at time t, we would like to create a clustered version of our behavioral map, Z, such that we maximize the information that zðtÞ 2 Z, the cluster that the fly is in at time t contains as much information about the future behavior of the fly, bðt þ t Þ as possible. To keep Z from separating each behavior into its own cluster, we also need to make sure that Z is as simple a clustering as possible (i.e., a smaller number of clusters and a more even distribution of time spent within each clusters).
To be more precise, we calculated Z using the the Deterministic Information Bottleneck (DIB) method (Strouse and Schwab, 2017). This approach minimizes the functional J t ¼ ÀIðZðtÞ; Zðb þ t ÞÞ þ gHðZÞ; ( 3) where bðt þ t Þ is a fly's behavior at time t þ t , ZðtÞ is the coarse-grained behavior visited at time t, IðbðtÞ; Zðt þ t ÞÞ is the mutual information between these quantities, g is a positive constant, and HðZÞ is the entropy of the coarse-grained representation (see Materials and methods). As g is increased, progressively simpler, but less predictive, representations are found.
Applying this method to the data, pooled across all six species and using t ¼ 50 ( Figure 4C, Figure 4-figure supplement 3), we found the same hierarchical division of the behavioral map that was observed for freely moving D. melanogaster (Berman et al., 2016). Moreover, we found that the structure of the space using this approach closely mirrors the structure found via directly clustering the intra-species covariance matrix, V ðeÞ ( Figure 4C). Quantifying the similarity between both clustering partitions by calculating the Weighted Similarity Index (WSI), a modification of the Rand Index (Rand, 1971) (Materials and methods), the WSI between the information-based clustering method and the predictive information bottleneck for three clusters is WSI ¼ 0:73, and WSI ¼ 0:87 for six clusters. For random clusterings, we would expect to observe 0:51 AE 0:02 and 0:70 AE 0:01 for 3 and 6 clusters, respectively, indicating a non-random overlap between these two partitions. Figure 4-figure supplement 1, shows that this result is independent of the clustering method and the number of clusters.
The overlap between these two coarse-grainings indicates that most individual variability in the behaviors we observe results from non-stationarity in behavioral measurements, rather than from individual-specific variation. That is, much of the intraspecific variation appears to reflect flies recorded when they were experiencing different hidden behavioral states (e.g., circadian state, hunger, etc.), rather than reflecting fixed (environmental or genetic) differences between flies. This variation may have arisen because, although we controlled many variables (e.g., fly age, circadian cycle, temperature, and humidity), it is not possible to control for all internal factors (e.g., hunger, arousal, etc.) that affect an animal's behavioral patterns (Anderson, 2016). The temporal coarse-graining of the behavioral space that we found via the DIB provides insight into these non-stationarities, as they are optimally predictive of the fly's future behaviors. Given the contiguous nature of these regions, this result means that flies tended to stay within specific regions of the behavioral space much longer than one would assume from a Markov model, hinting that there is an important connection between variability across animals and variability between animals.
More precisely, these results imply that variation in behavior observed among individuals, especially in non-manipulated settings, may often reflect a large component of hidden behavioral states ( Figure 5A). Thus, it may be possible to improve upon behavioral measurements in many settings by controlling for the variability associated with these hidden states. For example, just because one fly performs less anterior grooming than another may reflect that the animal is in a different long timescale behavioral state, rather than that the animal has a genetically encoded preference for reduced grooming.
A potential method for accounting for these artifacts is to normalize each individual's behavioral density such that the amount of time that the animal spends in each of the coarse-grained regions is equalized. In other words, the amount of time spent anterior grooming, locomoting, etc. are set to be the same for all animals, thus accounting for the variability associated with the inferred hidden states. Mathematically, if P i is the probability of observing behavior i, and C i is the clustering assignment of this behavior, we can define a normalized probability,P i , via where P ðCÞ i ¼ P k2C P k is the total density in cluster C for an individual fly and P ðCÞ is the average across all animals.
We found that applying this normalization to our data often results in substantial changes in the inferred distributions of behavioral densities. For example, Figure 5B displays how the difference in behavioral density between D. santomea and D. yakuba (as measured by the Mahalanobis distance   between the distributions) alters as a result of normalization. For some behaviors, the signal increases (red points), and in some cases, it decreases (blue points). Thus, it is important to take these non-stationary effects into account when estimating how often single behaviors are performed in studies of behavioral evolution. To measure these non-stationary effects, many behaviors must be measured, not just a focal behavior, thus partially explaining the relative success of our multi-trait model compared to a model where each trait is analyzed independently.

Identifying phylogenetically linked behaviors
One of the advantages of our approach is that we separate variations in behavior corresponding to evolutionary patterns, the phylogenetic variability, from variations among individuals of the same species. By studying the properties of the phylogenetic covariance matrix (V ðaÞ ), we can identify multiple behaviors that may have evolved together. We first characterized the coarse-grained structure within V ðaÞ through the information-based clustering used in the previous section (Slonim et al., 2005) (see Materials and methods). As seen in Figure 6A, the phylogenetically co-varying clusters are not spatially contiguous in the behavioral map. This finding is in contrast to the spatial contiguity we observed for the intra-species covariance matrix ( Figure 4B). For example, the two-cluster solution ( Figure 6A larger number of clusters, correlated groups are not contiguously arranged within the behavior map. Thus, our model predicts that many non-similar behaviors are evolving in a correlated manner. To quantify these patterns as traits, we decomposed V ðaÞ via an eigendecomposition. As seen in Figure 6B, almost all of the variance within the matrix can be explained with only the first six eigenmodes. These eigenvectors ( Figure 6C) share similar non-local structure to the clusterings described above. By projecting individual behavioral vectors onto these eigenvectors, the resulting dot products represent a meta-trait that is a linear combination of phylogenetically linked behaviors.
These evolving meta-traits may be suitable targets for further neurobiological or genetic studies. Three examples of these distributions are shown in Figure 6D-F for several pairs of closely related species. These three examples were not chosen at random, but instead because they showed significant differentiation between species. The aim of this analysis is not to show that all meta-traits would differ between all pairs of species, which is unlikely, but rather that it is possible to identify synthetic meta-traits that could be further interrogated with experimental methods.

Discussion
We have developed a quantitative framework to study the evolution of behavioral repertoires, using fruit flies (Drosophila) as a model system. We started with observations of 561 individuals from six extant species behaving in an unremarkable environment. This assay did not include social behaviors, such as courtship and aggression, nor many foraging behaviors. Thus, at first glance, it might seem like we had excluded most species-specific behaviors from the analysis. Nonetheless, we found that other complex behaviors, like walking, running, and grooming, exhibit species-specific features that can be used to reliably assign individuals to the correct species. Thus, the motor patterns of behaviors that are not normally investigated for their species-specific features are likely evolving between even closely related species. It is not clear, however, if these differences reflect natural selection or genetic drift. All of these behaviors are critical to individual survival, however, so it is possible that these behaviors have evolved, at least in part, in response to natural selection. It is clear, however, that the underlying mechanisms, and perhaps the neural circuitry, controlling these behaviors must have evolved.
Inspired by these observations, we estimated patterns of behavioral evolution in the context of a well-understood phylogeny. We fit a Generalized Mixed Linear Model to our behavioral measurements and the given phylogeny to reconstruct ancestral behavioral repertoires and the intra-and inter-species covariance matrices. We found that the patterns of intra-species variability are similar to long timescale behavioral dynamics that violate statistical stationarity -a result we reported previously in a study of a single species (Berman et al., 2016). This result suggests that much of the intraspecific variability that emerged by sampling flies under well-controlled conditions reflects variability in the hidden behavioral states of individual flies. While it may be challenging to conceptualize that seemingly simple behaviors, like the pace of walking and running, are reflective of an underlying long time scale behavior state, many short time scale behaviors, such as the individual movements involved in grooming (Seeds et al., 2014), courtship (Calhoun et al., 2019;Deutsch et al., 2020) and aggression (Hoopfer, 2016;Duistermars et al., 2018) reflect behaviors performed only, or mainly, in the context of a longer lasting behavioral state. These types of long timescale variability may be a statistical confound for evolutionary and experimental studies of behavior. We therefore propose a method to control for these internal states by normalizing the frequency of behaviors relative to an estimate of an animal's non-stationary states. This method improved the accuracy of behavioral phenotyping and dramatically altered estimates of some species-specific behaviors. For more focused studies, it may not be necessary to measure the full suite of behaviors to effectively normalize for behavioral state, since state can sometimes be estimated from a smaller number of behaviors. In fact, targeted studies of charismatic behaviors, including behaviors associated with aggression or courtship, often implicitly normalize by behavioral state.
Given our estimates for how suites of behaviors evolved, we examined whether the inter-species covariance matrix could be used to identify behavioral meta-traits that might be subjected to further evolutionary and experimental analysis. We identified multiple suites of behaviors that differed between closely related species, providing a starting point for further analysis of how the mechanisms underlying these suites of behaviors have evolved.
There are multiple possible interpretations of these phylogenetically correlated behaviors. For instance, at the neural level, each of these groups of movements may reflect a motor response to shared upstream commands (Cande et al., 2018). Here, for example, different types of locomotion might be controlled through the same descending neural circuitry, but due to evolutionary changes, the same commands could lead to different behavioral outputs, as has been observed in fly courtship patterns (Ding et al., 2019). Alternatively, at the genetic level, multiple behaviors may be linked by pleiotropic effects of individual genetic changes. Finally, groups of co-evolving traits may not be linked mechanistically, but co-evolution may instead reflect selection on suites of behaviors. For example, the male neurons that drive fly courtship song production in the ventral nerve cord are unlikely to be related to the female neurons in the central brain that perceive and interpret the courtship song. Nonetheless, these traits co-evolve such that females tend to prefer songs produced by males of their own species (Bennet-Clark and Ewing, 1969;Ding et al., 2019).
The analysis framework introduced here represents the first attempt to analyze full behavioral repertoires to gain insight into evolution. In principle, this approach could be applied to any data set where a large number of behaviors have been sampled in many species. However, there are several areas where one could add future improvements to this approach. First, we recorded behavior from only six species of flies. Adding additional species would place more constraints on the evolutionary dynamics, likely resulting in less variance in the ancestral state estimations and potentially adding more structure to the relatively low rank (i.e., highly modular) covariance matrices. Additionally, further work is required to determine the balance between sampling within and between strains and species that optimizes estimates of evolutionary dynamics.
Second, our framework assumes that all evolutionary changes in behavior resemble a diffusion process. Although this assumption is a reasonable initial hypothesis (Felsenstein, 1985), it may be possible to test this assumption. For example, deeper sampling of additional species may allow identification of specific behaviors on particular lineages where neutrality can be rejected (Tajima, 1993). If evidence emerges that the analyzed behaviors do not evolve under a diffusion process but under stabilizing selection, for example, the model for ancestral reconstruction can be changed from a Brownian motion to an Ornstein-Uhlenbeck process (Martins and Hansen, 1997;Hansen and Martins, 1996;Royer-Carenzi and Didier, 2016). Such a change can be implemented by altering the structure of the phylogenetic matrix, A (Martins and Hansen, 1997;Caetano and Beaulieu, 2020), but without other alterations to the overall methodology presented here.
Another potential limitation of our analysis is that some of the observed inter-specific differences may reflect species-specific responses to environmental factors like room temperature or humidity, rather than underlying genetic or developmental factors. Two observations mitigate against this possibility, however. First, there were no significant differences in overall activity level of the different species, which would be a key indicator of environment-induced covariance. Second, the intra-species covariance matrix (derived from data from all species) agrees well with previous findings within a single species (Berman et al., 2016), implying that many of the potential environmental co-varying factors are shared across all six species.
In addition, all of our current analyses ignored the temporal structure of behavior and sequences of movements. While we found that the intra-specific variance has a similar structure to temporal structure that we reported previously (Berman et al., 2016), the order in which behaviors occur may also provide important biological information, especially during events like courtship or aggression. It should be possible to incorporate temporal structure directly into the regression (Caetano and Beaulieu, 2020). Deciding exactly which quantities to measure and how they should be incorporated, however, are complex questions that are outside the scope of this initial study. In addition, the number of fit parameters, already over 18,000 here, would need to grow even larger to accommodate modeling transition rates between the behaviors as traits themselves. Thus, fitting such models would necessitate even larger data sets than the one collected here. Moreover, because the Perron-Frobenius Theorem mathematically couples the transition probabilities between behaviors and the probabilities of the fly performing a given behavior, additional care (and data) is required to ensure that observed differences in behavior are due to changes in temporal structure rather than changes in the frequencies of performing a given behavior.
Lastly, capturing the full range of animal behaviors for a large number of animals presents a number of technological challenges, which is why we focused on measuring behavior in a highly simplified environment. However, a more complete understanding of the structure of behavior will require more sophisticated ways to capture behavioral dynamics in more naturalistic settings and during complex social arrangements. While modern deep learning methods have made tracking animals in more realistic settings increasingly plausible (Pereira et al., 2019;Mathis and Mathis, 2020), there are still considerable hurdles to translating this information into a form that can be subjected to the kind of analysis we propose here.
Despite these limitations, this work represents a new way to quantitatively characterize the evolution of complex behaviors, which may provide new phenotypes that can be subjected to experimental analysis. In the absence of a behavioral fossil record, reconstructing ancestral behaviors requires an inferential approach like the one we present here. In addition, more complex models could be built to test assumptions of the diffusion-based model we employed. Finally, a strength of our approach is that it makes falsifiable predictions about how behaviors are linked mechanistically, providing predictions that can be tested experimentally to provide further insight into the genetic and neurobiological structure of behavior.

Data collection
All fly handling and imaging of fly behavior followed the procedures described in Cande et al., 2018, excepting that we did not provide retinol-free food to any of the animals, nor we did provide any red light cycling during the experiments. Individual male flies were collected upon eclosion and housed singly in 2 mL wells in a 96-well 'condo,' with food deposited in the bottom of each well, which was sealed at the top with an airpore sheet. In total, we collected data from 561 individual from 18 strains and six species. Flies were imaged at age 7-12 days, within 4 hr of lights on. Individuals were sampled from multiple strains and species: three strains of D. mauritiana (mau29: 29 flies,

Generalized linear mixed model
We fit our GLMM (Equation 1) using the software introduced in Hadfield, 2010. The covariance matrices V ðeÞ and V ðaÞ 2 IR KÂK , K ¼ 134 and the mean vector 2 IR KÂ1 were inferred from the posterior distribution via MCMC sampling. Prior distributions for the covariance matrices were given by Inverse Wishart Distributions (conjugate priors for the multi-Gaussian model) with K degrees of freedom and 1

Kþ1
IþJ 2 as scale matrix, with J and I the unit and identity matrices respectively. Tree branch length were estimated from Seetharam and Stuart, 2013.

Gelman-Rubin convergence diagnostic
This test evaluates MCMC convergence by analyzing the difference between several Markov chains. Specifically, we compare the estimated between-chains and within-chain variances for each parameter of the model. Large differences between these variances indicate non-convergence (Gelman and Rubin, 1992). Let be a model parameter of interest and m f g N t¼1 be the m th simulated chain, m ¼ 1; 2; :::; M. Denote, m andŝ 2 m be the sample posterior mean and variance of the m th chain. If ¼ 1 M P M m¼1 m is the overall posterior mean estimator, the between-chains (B) and within-chain (W) variances are given by: In Gelman and Rubin, 1992, it is shown that the following weighted average of W and B is an unbiased estimator of the marginal posterior variance of : The ratioV=W should get close to one as the M chains converge to the target distribution with N ! ¥. In reference (Brooks and Gelman, 1998) this ratio known as the Potential Scale Reduction Factor (PSRF) was corrected to account for the the sampling variability using R c ¼

Deviance information criterion (DIC)
The DIC is used as a Bayesian model selection criteria in problems where there is hierarchical structure to the underlying models and where the correct effective number of parameters is difficult to ascertain (Spiegelhalter et al., 2002). These aspects are often found in models like ours where the posterior distributions have been sampled using Markov Chain Monte Carlo. The DIC is defined as follows: DðÞ ¼ E pðjyÞ ½DðÞ; where DðÞ ¼ À2 log Pðy j Þ þ 2 log f ðyÞ is called the deviance (f ðyÞ denotes a function of the data alone and Pðy j Þ corresponds to the likelihood of the model under evaluation). Hence, the posterior mean, DðÞ, can be considered as a Bayesian measure of fit. P D represents the effective number of parameters, where Dð Þ is the deviance evaluated at the posterior mean of the parameters . Note that (i) both quantities needed to calculate DIC, DðÞ, and Dð Þ, can be readily estimated from the samples generated by MCMC, and (ii) alternatively, we can also re-write DIC ¼ Dð Þ þ 2P D . This is similar in form to the better-known Akaike Information criterion (AIC) (Akaike, 1973), for models with negligible prior information or for large data sets where the likelihood dominates over the prior.

Comparing the focused trait and full repertoire models
To build a model where behavioral traits evolve independently from each other, we fit each a single trait GLMM for each behavior j: where l j denotes the logarithm of the behavioral trait P j , j is the logarithm of the mean behavior of the common ancestor (treated as the fixed effect of this model), and j andẽ j are the random effects corresponding to the phylogenetic and individual variability, respectively. Similar to the multiresponse model, these random effects are normally distributed from N ð0; A s j Þ and N ð0; A a j Þ with s j and a j (single numbers) corresponding to the phylogenetic and individual inferred variances and A the phylogenetic matrix defined in the main text. Prior distributions for the variances are given by inverse-Wishart distributions with 1.002 degrees of freedom and scale parameter equal to the variance of the logarithm of the corresponding behavioral trait. We fit these models using 10 bootstrapped data sets and obtained an average DIC value of ð230 AE 2Þ Â 10 3 . Note that in the single-trait model, since each behavior is treated independently, the likelihood gets factorized in terms of the individual likelihoods corresponding to each behavioral trait: Pðl 1 ; l 2 ; :::; l K jÞ ¼ Q K i¼1 Pðl i j i Þ. Therefore, the DIC (estimated in terms of the log-likelihood) is given by DIC ¼ P K i¼1 DIC i , where DIC i is calculated for each single trait GLMM. In contrast, the complete GLMM model (described in the main text and in the section above) had a significantly lower average DIC value of ð114 AE 2Þ Â 10 3 (calculated over 10 bootstrapped data sets as well).

Information-based clustering
The information-based clustering approach used in this article (originally introduced in Slonim et al., 2005) minimizes the distance between elements within clusters, while also compressing the original representation as much as possible. More precisely, the method minimizes the functional PðCÞdðCÞ, and dðCÞ is the average distance of elements chosen out of a single cluster: Pði 1 j CÞPði 2 j CÞdði 1 ; i 2 Þ; with dði 1 ; i 2 Þ being the distance measure between a pair of elements and Pði j CÞ being the probability to find element i in cluster C:T is a Lagrange multiplier that modulates the relative importance of minimizing the average within-cluster distance and simplifying the clustering. Given jCj ¼ N c , T and a random initial condition for PðC j iÞ, a solution is obtained by iterating a set of self-consistent equations (Slonim et al., 2005) until the convergence criteria F t ÀF tþ1 F t <10 À5 is satisfied. We chose 40; 000 different initial conditions for PðCjiÞ, along with randomly chosen values of T 2 ½0:1; 1000 and N c 2 f2; 3; . . . ; 20g. For each set of initial conditions and parameters, we performed the optimization until the convergence criterion was met. We defined the Pareto front as the set of solutions PðC j iÞ such that no other solution presents a smaller hdi and a smaller IðC; iÞ, and we only kept solutions that were along this front (eliminating duplicates). Finally, for each number of clusters we selected the solution with the lowest hdi.
For each number of clusters, we assess the modularity of the found solution by comparing hdi for the solution to the average distance corresponding to random cluster assignments. These assignments are made in such a way that the amount of elements per cluster is conserved by randomly shuffling the vector that assigns each behavior to a particular cluster. The values presented in the main text correspond to the mean and standard deviation of hdi over 50 different random trials.

Deterministic Information Bottleneck
We use the Deterministic Information Bottleneck (DIB) method (Strouse and Schwab, 2017) to find coarse-grainings of the behavioral space that optimally predict future states. Inspired by the Information Bottleneck (IB) (Tishby et al., 1999), given two measured variables, X and Y, the DIB method finds a clustering, Z, of X, where Z is maximally informative of Y, but is as simple as possible. Specifically, we minimize the functional: with respect to pðz 2 Zjx 2 XÞ. Here, g is a Lagrange multiplier that modulates the relative importance of the two terms, with larger values of g resulting in simpler representations. In practice, to compute this minimum for a given value of g and an initial condition for pðzjxÞ, we minimize J ðaÞ ¼ gHðZÞ À aHðZ j XÞ À IðY; ZÞ with respect to pðz 2 Zjx 2 XÞ and take the limit as a ! 0, following the self-consistent equation procedure described in Strouse and Schwab, 2017.
To apply DIB to the behavioral dynamics, we count time in units of the transitions between states, providing a discrete time series of behaviors: bðnÞ can thus be one of N ¼ 134 different integer values at each discrete time n. Here, we relate the joint distributions of bðnÞ (X in Equation 10) and bðn þ t Þ (Y) through a coarse-grained clustering of the behavioral states (Z). Similar to our approach with information-based clustering (see previous section), we chose 10,000 different pairs of random values for g between 0.1 and 10 4 and N c between 2 and 30 clusters. Given N c , g and a random initial condition for pðt j xÞ, we find a solution by iterating through a set of self-consistent equations (Strouse and Schwab, 2017) until the convergence criteria (an absolute change in the function of less than 10 À6 ) is satisfied. If any cluster has its probability become zero at any iteration, then that cluster is dropped for all future iterations. Thus, N c is the maximum number of clusters that can be returned. Of these 10; 000 solutions, we keep all solutions that are on the Pareto front (i.e., no other solution has both a higher IðY; ZÞ and a smaller HðZÞ). The displayed clusters are the solutions on the Pareto front with the largest IðY; ZÞ for a given number of clusters.

Weighted similarity index
We quantify the similarity between clustering partitions using the Weighted Similarity Index (WSI), a modification of the Rand Index (Rand, 1971) such that behaviors contribute the index according to their overall probability. Specifically, where S a ðS b Þ is the set of pairs of behaviors that belong to the same (different) cluster in the two partitions and P k is the probability of observing behavior k. Additional files

Supplementary files
. Source data 1. Fly behavior source data.
. Transparent reporting form

Data availability
All behavioral region information is submitted with the article and is posted on GitHub (https:// github.com/bermanlabemory/behavioral-evolution, copy archived at https://archive.softwareheritage.org/swh:1:rev:b01a6e3a2c7da193f38631dfe925c65229494d74). The original video data are too large to post (tens of TB), but will be made available upon request.