Is Emotional Contagion independent of Information ?
In a social network, each node is influenced by it’s neighboring nodes. This influence can be observed primarily in two forms;
- Change in the knowledge state of a node, or
- Change in the emotional state of a node.
It will be interesting to figure out their dependency on one another. However that will a topic for the future. What this post will address is whether the two are independent.
Imagine two people. One in an agitated state, starts to lash out on the other person. In this exchange, few things are transferred between the two. For the information/context things like location, time, past events, future anticipation, and words spoken by the angry person, the topic of the . For the emotion, is arguably ‘anger’.
While there can be anecdotal evidence on how the context information is dependent or independent on the emotion, it’s not concrete. More specifically, the question that I’m asking is what effect does it have on the victim, in terms of his behavior ?
Generally, what has been observed is a person will either go into introspection /empathy if he feels that the other person’s emotion was justified, otherwise vent his frustration onto someone else (irrespective of the context ?).
So how does this translate into a problem ?
well well well, since you asked.
The problem statement is as follows:
Given a social network and the text/opinion data of each node in the network, partitioned w.r.t. time,
Que 1) Does the emotion/sentiment of a node affects the sentiment of it’s neighboring nodes ?
Part 1: Getting the data
Getting a social network. Using the twitter API, it’s possible to get few key information. The individual user, his friends list (people who the user follows), his tweets and all his friend’s tweets with timestamps.
For any twitter app, you first need to access the API. Here’s an great playlist for you to cover your basics.
from tweepy import API
from tweepy import OAuthHandlerauth = OAuthHandler(twitter_credentials.CONSUMER_KEY, twitter_credentials.CONSUMER_SECRET)
auth.set_access_token(twitter_credentials.ACCESS_TOKEN, twitter_credentials.ACCESS_TOKEN_SECRET)api = API(auth)
Once the you have the access, we use the following line of code to access all their friends.
form tweepy import Cursor
friends_ids = 
for page in Cursor(api.friends_ids, screen_name='wadu_hek').pages():
time.sleep(60)screen_names = 
num_statuses = 
for id in friends_ids:
u = api.get_user(id)
Now you have node, with all of its neighbors. For starters(cos’ I am one), I will make an reasonable assumption: A person’s emotional/information state is affected only by their immediate neighbors. Now in order to get the tweets of everyone in this small network. We iterate over the screen_names and pass the those names to the following function.
alltweets = 
new_tweets = api.user_timeline(screen_name=screen_name, count=200)
alltweets.extend(new_tweets) oldest = alltweets[-1].id - 1 while len(new_tweets) > 0:
print('getting tweets before %s' % oldest) new_tweets = api.user_timeline(screen_name=screen_name, count=200, max_id=oldest)
if len(alltweets) > 500:
oldest = alltweets[-1].id - 1 print('%d tweets downloaded so far' % len(alltweets)) out_tweets = [[tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")] for tweet in alltweets] with open('%s_tweets.csv' % screen_name, 'w') as f:
writer = csv.writer(f)
writer.writerow(['id', 'created_at', 'text'])
Now I have the following information.
- The test node, on which we try to predict and observe the effects
- All the neighbors of the test node.
- The tweets of the test node and all of its neighbors with timestamps.
Part 2: Building the sentiment analyzer
Building a twitter sentiment analyzer. This is fairly easy. There are lots of great resources.
Another Twitter sentiment analysis with Python — Part 1
It has been a while since my last post. During my absence in Medium, a lot happened in my life. I finally gathered my…
This will help to figure out the sentiment of any node in any specified time frame.
Now, the next step is to observe whether the sentiment of neighbors affect the sentiment of the test node. I still have to lay down a specific method.
As for now, I’ll do a crude step of averaging out the emotions in each time step of all of it’s neighbors and comparing this will the actual sentiment of the test node. There may be a variations, in terms of emotion not being portrayed in the same time steps, and which will even vary from one test node to another.
So in order to suppress such cases’s effect on the experiment, I will possibly have to scale the social network. Also different neighbors ought to portray, different influence on the test node, this means that each edge will have to have a weight associated with it.
These weights could be inferred from the how the emotional state of the test node vary in respect to each of it’s neighbors. I am still working on the exact methodology to do so.. (future work :) )
A more granular emotional state(happy, sad, afraid/surprised, and angry/disgusted) will help to better answer the questions of emotional contagion and it’s dependency on the information/context, but of all such data sets, I haven’t yet found one large enough to train a sentiment analyzer on.
Part 3: Testing the separation of context and sentiment
If the test node shows the expected behavior, the next step is to test whether this behavior is independent of the context. In order to do so, we need to summarize the tweets at each time step in terms of a ‘topic’.
The following is a good guide to extract topics. I have yet to test it on tweets.
The complete guide for topics extraction in Python
An intro to LDA (Latent Dirichlet Allocation) for topics extraction from a corpus of documents
What I am trying to observe here is whether the topics and emotions of the neighboring nodes and test nodes are independent.
So if a node y (belongs to) neighbors’s sentiment is negative and is talking about sports, does the test node, given the influence of this neighbor.. will the the test node have similar emotion while talking about different topic.
A thing that bothers me is whether emotional contagion occur if the exchange isn’t directed towards the node itself by another node.
I believe scaling the social network to include even more layers of nodes and timely tracking their sentiments could help to ignore more of this kinks that otherwise occur in a small network (a node and neighbor).
More work to be done.. Until next time.