title: Python Soc Network Analysis: Calculating 'Chance Network Homophily' by iterating through a list, using a function
body: <p>I am hoping to plug items into an equation <strong>from any list</strong> of dictionary values. The equation is to calculate homophily.</p>
  
  <h2><strong>Background:</strong></h2>
  
  <p>Homophily: the probability that different characteristics will be shared within a social network. For example, the probability of people in a sample population sharing an eye colour.</p>
  
  <p>This is how the equation might work in that example:</p>
  
  <p><strong>(frequency of blue eyes / sum frequency)^2 + (frequency brown eyes / sum frequency)^2</strong> </p>
  
  <p>Bear in mind, this would have to work even if you had more than two characteristics (green, hazel, gray). But, I'm keeping it simple to make it easy to check the result.</p>
  
  <p>So if we had this data in the form of a dictionary:</p>
  
  <pre><code>EYES = {'Kevin':'brown', 'Frank':'blue', 'Alex':'blue', 'Tracey':'brown', 'Milo':'blue'}
  </code></pre>
  
  <p>First I calculate frequency of values in the dictionary, like this:</p>
  
  <pre><code>def frequency(EYES):
      Eyes_freq_dict = Counter(EYES.values())
      return Eyes_freq_dict
  
  Counter({'blue': 3, 'brown': 2})
  </code></pre>
  
  <p>Now I have a second dictionary of the frequencies alone to work with:</p>
  
  <pre><code> Counter({'blue': 3, 'brown': 2})
  </code></pre>
  
  <p>But because dictionaries are unordered, I must put the values into a format that I can do maths on.</p>
  
  <p>I think this <strong>needs</strong> to be a list? Please correct me if I'm wrong:</p>
  
  <pre><code>Eyes_freq_dict = frequency(EYES)
  EYESlist = []
  for v in Eyes_freq_dict.values():
      EYESlist.append(v)
  </code></pre>
  
  <p>I then get the values on their own, as a list:
      EYESlist
      [3, 2]</p>
  
  <h2><strong>Stumbling block:</strong></h2>
  
  <p>I am new to doing maths with Python, so I am now worried that I might have reached a dead end. Is it actually possible for me to plug these components into the above equation?</p>
  
  <p>The desired equation in this case would be:</p>
  
  <p><strong>(EYESlist[0]/ sum(EYESlist))^2 + (EYESlist[1])/sum(EYESlist))^2</strong></p>
  
  <p>Or in English, it works out to:</p>
  
  <p>**3/5 squared + 2/5 squared = 0.52 **</p>
  
  <p>The problem now is: How to iterate over, firstly, this list. And then ANY list (of any length) to get a correct result.</p>
  
  <p>If the route I'm taking is a dud, please be kind but do let me know! I'd be happy if there is a more elegant way of solving this, but I hope it's fun exploring.</p>
  
  <p>Just trying to type up the formula and test it in a pythonic way, I'm confused because I'm getting an error message:</p>
  
  <pre><code> sum(((EYESlist[0]/ sum(EYESlist))**2)((EYESlist[1])/ sum(EYESlist))**2)
  #TypeError: 'float' object is not callable
  </code></pre>
  
  <p>I hope somebody can point me in the right direction! I'd like to get on with the REST of my homework (I have five other problems to work through). </p>
  
  <p>What I will probably do if I can't resolve this quickly, is move onto other questions and then come back to this one. It is an interesting challenge for me.</p>
  
  <p>Best,
  A</p>