Stand up for the facts!

Our only agenda is to publish the truth so you can be an informed participant in democracy.
We need your help.

More Info

I would like to contribute

Coding used by Matt Bruenig to calculate Michael Bloomberg wealth comparison

f = open("scf2016.csv")
data = f.readlines()

# Arrays for tuples of weight and networth
wealth = []

# Array of column names
columns = data[0].rstrip().split(",")

# Fill up the arrays for tuples of weight and networth
for line in data[1:]:
line = line.rstrip().split(",")

# Set variables for this record
wgt = float(line[columns.index('wgt')])
networth = float(line[columns.index('networth')])
vehic = float(line[columns.index('vehic')])

t = (wgt, networth)
wealth.append(t)

# data - tuples containing (weight, value)
# quantile - tuple containing (begin quantile, end quantile)
# returns the total weight and total weighted value in tuple (total_weight,total_value)
def wtotal(data,quantiles=(0,100)):
data.sort(key=lambda tup: tup[1])

total_weight = 0
total_value = 0
for weight, value in data:
total_weight+=weight
total_value+=value*weight

if quantiles[0] == 0 and quantiles[1] == 100:
return((total_weight,total_value))

start = total_weight * quantiles[0] / 100
end = total_weight * quantiles[1] / 100

last_weight = 0
cumulative_weight = 0
share_value = 0
share_weight = 0
counter = 0
for weight, value in data:
cumulative_weight+=weight
if cumulative_weight < start:
continue
elif cumulative_weight >= start and counter == 0:
over_weight = cumulative_weight - start
share_value+=value*over_weight
share_weight+=over_weight
counter = 1
elif cumulative_weight <= end:
share_weight+=weight
share_value+=value*weight
elif cumulative_weight > end and counter == 1:
under_weight = end - last_weight
share_value+=value*under_weight
share_weight+=under_weight
break

last_weight = cumulative_weight

return((share_weight,share_value))

p = (0,38)
bottom = wtotal(wealth,p)
print(bottom[1]) # outputs 11436050176.560202 ($11.4 billion)
print(bottom[0]) # outputs 47873046.2603922 (47.8 million families)