And why you should even try…
As you may know, friendships are a huge part of a good life. They influence your well being (Pezirkianidis et al. 2023), but most importantly, your routine. They impact decisions like the places you go (Cho et al. 2011), when you go and the activities you do (Toole et al. (2015)). Now, suppose you work a the municipality and want to know where to put a new public shared space so people can get together. Should you place it near to work locations, or home locations, somewhere in between and what times of the day should you promote it. In this case, knowing when the people of your city like to get together and what they want to do is essential. But how to know this?
Well… There are social networks, like Instagram, which can help you see these friendships based on who is tagging each other, the places they are going, and stuff like that. Unfortunately, this data is expensive, has privacy issues, lots of noise and is biased to certain groups with public profiles. Meanwhile, data from bus cards transactions and cellular data records (CDR), when you connect to a mobile tower to get internet, are more accessible, general and can be more easily analyzed on aggregated scales, better preserving privacy. The problem is how do you find friendships in this kind of mobility data?
In this blog, I’m going to show you how to find friendships in sparse mobility data using the RECAST algorithm (Vaz de Melo et al. (2015) with Fastkit-Mobility, and investigate the friendship behavior of 100.000 individuals in Japan through 75 days, understanding how many friends they have, how much they meet each other, what they do together and how their friendship fit their daily routines.
For this analysis, I’m going to use the YJMOB100k dataset (Yabe et al. (2024)), which “contains 100,000 individuals’ mobility trajectories across a 75 day period from an undisclosed, highly populated metropolitan area in Japan. The location pings are discretized into 500m x 500m grid cells and the timestamps are rounded up into 30-min bins.” The data comes from mobile phone location data provided by Yahoo Japan Corporation.
While the original dataset used an undisclosed region, researchers eventually found out (Abhishek Kumar et al. (2026)) that the city was Nagoya, Japan, and then we can match the data to the actual city.
To estimate the social network, we are going to use the RECAST (Random rElationship ClASsifier sTrategy), proposed by Melo et al (2015), which separates random encounters from social ties, using two social features: frequent interactions and shared acquaintances.
Before using the RECAST algorithm, we need to identify from the sparse ping data of YJMOB100K the points, the stay points, and then use it to identify the co-presence of people. The figure below describes it:
With this pre-processing ready, we can proceed to the RECAST algorithm. The RECAST algorithm first builds a daily graph, where the the set of nodes are the individuals and the edges represent the pairwise contacts made during that day. In our case, a contact is made if two users are at the same cell at the same 30-min bin on that day.
Then it joins all the daily graphs on a single graph, and we count how many times the same edge between a pair of nodes appeared for the regularity computation. In the figure below, we repeat the edges so the count is more clear:
To distinguish actual social ties from random encounters, the algorithm now produces random graphs which are going to act as comparison. It can generate the final aggregated graph, preserving the node, edge and degree distribution, but assigning the edges randomly between nodes, or generate the daily graphs preserving the topology and then doing the temporal aggregation - we are going to using this version where the graphs are generated daily and then aggregated.
To now, classify the social ties, we use the two features. We use the regularity, which is defined as the days the two individuals meet divided by all the days considered. And we use the topological overlap, which is defined as the ratio of neighbors shared by two nodes. Because the random graphs assigns edges randomly, we expect real friends to have more regularity and topological overlap.
Finally, the algorithm uses a parameter to estimate the values of regularity and topological overlap that define random encounters. For example, with a , the algorithm may return a indicating that only 1 in 1000 edges in the random network has a regularity higher than , which means that values higher than this may indicate a regular relationship. The same threshold logic is used for the topological overlap. Take a look at the following image which describes this with the YJMob100k data:
If the regularity and topological overlap of an edge in the graph are higher than the threshold, then they are defined as friends. If only the regularity is higher than the threshold, then they are defined as bridges, people who meet often (like work), but don’t share friend groups (familiar strangers). If only the topological overlap is higher than the threshold, then they are defined as acquaintances, people who see each other in birthday parties and other events from their common friends. Otherwise, they are classified as random encounters.
I can easily apply this to the YJMOB100k dataset using the open-source Fastkit-Mobility, which implements the algorithm in a fancy fast way:
# pip install fastmob==0.3.4
from fastmob import Locations, Staypoints
from fastmob.social import recast_from_staypoints
result = recast_from_staypoints(
Staypoints(stays),
Locations(cells, scope="global", scheme="external"),
min_minutes_for_encounter=30,
p_rnd=1e-3, random_replicates=5, seed=42)
In this case, the stays data table has the uid, location_id, started_at and finished_at columns. The cells has the location_id, center_lat and center_lng.
The result has the shape: source_users, target_users, edge_persistence, topological_overlap, and classes, that is, the dataframe representing the social network. For our data, it had 12,032,513 edges with 709,285 (5,8%) being classified as friends. Fastkit-Mobility was able to process all of this in 43.4 seconds, from the daily graph creation, aggregation, random replica construction and the classification. For reference, raw YJMOB has 111M pings, which are processed into 12M staypoints.
Now, looking to this result set, we can verify how many friends each person has, and how it varies based on the parameter:
Overall, people have a median of 4 friends, with 30% of them having no friends, and only a few having more than 20 friends. If we vary the parameter, we can see that more relationships can be classified as friends or less, if we require a behavior really distinct from random ones.
Looking to how much people meet, we can see that for our 709,285 friend pairs, meet around 7 days over the 74 days, spending an average of 1.3 hours together, which looks kind of realistic:
Now, when we look to where people are together, we compare the percentage of time that people spend at different locations, home, work and other, and notice that in they tend to spend more time at home and at work with friends than alone, and when in neither home nor work, they spend more time alone. It is important to note that the data is spare, and the algorithm prioritizes relationships that are regular such as living in the same neighborhood and working in the same location.
Finally, we study how friends fit in the daily routines of people. For example, 7.6% of the pairs had the pattern of meeting at work, spending most of the day together. 7.1% stayed with friends at home in days where they just stayed at home, usually roomates or family. There are routines where people go to work, but have no friends there, only at home. And people who has friends in places outside of home and work.