Wednesday, July 23, 2008

Condom failure rates and the spread of HIV

After tackling the effect of condom failure rates on population growth, let's look at the failure rates on the spread of HIV.

I'll pose it as an ACM-type programming question. Ready?

Problem: Given a population of 100 swingers who have sexual intercourse once a week with different partners from the pool, assuming a perfectly fair round-robin distribution (i.e., no repeat partners until after they have had intercourse with everyone else in the pool), and assuming they use condoms; how many weeks until all 100 swingers have been infected with HIV, assuming that one of them is HIV-positive at the beginning?

Use a condom failure rate of 15% and average the result over 1,000 iterations.

Now, you can't use a spreadsheet to solve for this as it's a little more involved. You'd need to use a program. It's tricky, but not impossible. Below is my solution, in Python:


#!/usr/bin/python

import pairing
import array
import random
import sys

def swinger_infection_estimate(population,failure_rate):
 #initialize the swingers
 swingers=array.array('i')
 for i in range(population):
   swingers.append(0)
   swingers[0]=1

 # arrange the schedule
 schedule=pairing.roundRobin(range(population))


 #initialize counter
 week=0

 while(1):
   for i in range(len(schedule)):
     week=week+1
     for j in range(len(schedule[i])):
       if swingers[schedule[i][j][0]] ^ swingers[schedule[i][j][1]]:
         if random.random() < failure_rate:
           swingers[schedule[i][j][0]]=1
           swingers[schedule[i][j][1]]=1
         try:
           swingers.index(0)
         except ValueError:
           return week

estimate_ctr=0
for i in range(1000):
 estimate_ctr=estimate_ctr+swinger_infection_estimate(100,0.02)

print estimate_ctr/1000



The pairing algorithm I am using is adapted from the ActiveState round robin generator cookbook:


def roundRobin(units, sets=None):
   """ Generates a schedule of "fair" pairings from a list of units """
   if len(units) % 2:
       units.append(None)
   count    = len(units)
   sets     = sets or (count - 1)
   half     = count / 2
   schedule = []
   for turn in range(sets):
       pairings = []
       for i in range(half):
           pairings.append([units[i],units[count-i-1]])
       units.insert(1, units.pop())
       schedule.append(pairings)
   return schedule




Please check the logic of my code to see if it covers everything I've said. I would like to hear from you if you have a different opinion.

The result of these calculations: given the above assumptions, the entire population of 100 swingers will all be HIV-infected in anywhere from 81 to 83 weeks, or just a little over one and a half years. Individual sampling show full-infection estimates of up to 120 weeks (two years and four months) to as little as 63 weeks (one year and two months).

Consider now the results with no condom usage at all, i.e., assuming 100% failure rate. How long does it take to infect the entire population? It takes 50 weeks, or about a year. Under these assumptions, condoms delay the onset of full infection by 33 weeks.

Assuming a more generous 2% failure rate (unrealistic, in my view), it takes much longer to infect the entire population, around 510 weeks, or a little less than ten years. However: using the 2% failure rate of condoms in a monogamous relationship in which one partner is HIV-infected, the risk of infection is one in every fifty encounters. Assuming a weekly sexual encounter, infection is likely within a year.

Note: Please understand that I do not pose these questions to make fun of or condemn HIV victims. I do so because I believe that the claims around safe sex are fallacious, and that its advocates are grossly misrepresenting the dangers and condoning risky behavior.

Update: Roy, in the comments section below, points out an error in my assumptions with regard to transmission. At best, my approach shows the situations in which condom failure will occur, not necessarily transmission.

As far as I have searched, there are no established and proven probability rates for transmission in encounters.

A CDC document on the subject examines the issue in greater detail.