Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created October 25, 2010 08:24
Show Gist options
  • Select an option

  • Save JoshCheek/644599 to your computer and use it in GitHub Desktop.

Select an option

Save JoshCheek/644599 to your computer and use it in GitHub Desktop.
analyze correlation between homicide and atheism
#!/usr/bin/env ruby -Ku
# take countries by atheism http://en.wikipedia.org/wiki/Demographics_of_atheism
# compare to countries by homicide rate http://en.wikipedia.org/wiki/List_of_countries_by_intentional_homicide_rate
# calculations and analysis down below
countries = {
"Sweden"=>{:atheism=>85, :crime=>1.17},
"Denmark"=>{:atheism=>80, :crime=>1.11},
"Norway"=>{:atheism=>72, :crime=>0.78},
"Japan"=>{:atheism=>65, :crime=>0.64},
"Czech Republic"=>{:atheism=>61, :crime=>1.34},
"Finland"=>{:atheism=>60, :crime=>2.75},
"France"=>{:atheism=>54, :crime=>1.64},
"South Korea"=>{:atheism=>52, :crime=>2.18},
"Estonia"=>{:atheism=>49, :crime=>9.4},
"Germany"=>{:atheism=>49, :crime=>1.06},
"Russia"=>{:atheism=>48, :crime=>27.0},
"Hungary"=>{:atheism=>46, :crime=>2.09},
"Netherlands"=>{:atheism=>44, :crime=>1.17},
"United Kingdom"=>{:atheism=>44, :crime=>1.75},
"Belgium"=>{:atheism=>43, :crime=>2.05},
"Bulgaria"=>{:atheism=>40, :crime=>3.09},
"Slovenia"=>{:atheism=>38, :crime=>1.45},
"Israel"=>{:atheism=>37, :crime=>2.62},
"Canada"=>{:atheism=>30, :crime=>1.95},
"Latvia"=>{:atheism=>29, :crime=>8.58},
"Slovakia"=>{:atheism=>28, :crime=>2.27},
"Switzerland"=>{:atheism=>27, :crime=>1.07},
"Austria"=>{:atheism=>26, :crime=>0.72},
"Australia"=>{:atheism=>25, :crime=>1.28},
"Spain"=>{:atheism=>24, :crime=>1.23},
"Iceland"=>{:atheism=>23, :crime=>1.03},
"New Zealand"=>{:atheism=>22, :crime=>2.0},
"Ukraine"=>{:atheism=>20, :crime=>7.42},
"Belarus"=>{:atheism=>17, :crime=>8.31},
"Greece"=>{:atheism=>16, :crime=>0.99},
"Italy"=>{:atheism=>15, :crime=>1.23},
"Armenia"=>{:atheism=>14, :crime=>2.48},
"China"=>{:atheism=>14, :crime=>2.36},
"Lithuania"=>{:atheism=>13, :crime=>9.38},
"Singapore"=>{:atheism=>13, :crime=>0.49},
"Uruguay"=>{:atheism=>12, :crime=>5.64},
"Kazakhstan"=>{:atheism=>12, :crime=>12.1},
"Mongolia"=>{:atheism=>9, :crime=>12.81},
"Portugal"=>{:atheism=>9, :crime=>1.37},
"United States"=>{:atheism=>9, :crime=>5.5},
"Albania"=>{:atheism=>8, :crime=>5.68},
"Argentina"=>{:atheism=>8, :crime=>6.3},
"Kyrgyzstan"=>{:atheism=>7, :crime=>8.01},
"Dominican Republic"=>{:atheism=>7, :crime=>25.25},
"Croatia"=>{:atheism=>7, :crime=>1.83}
}
# calculate coefficient of correlation in accordance with http://mathbits.com/mathbits/tisection/statistics2/correlation.htm
# let x = atheism in or around 2004 (with the bar set at weak/negative atheism, ie not convinced there are gods as opposed to convinced there are no gods)
# let y = crime (intentional homicide rate per 100,000 inhabitants in 2004)
n = countries.size # => 45
Σx = countries.inject(0) { |sum,(country,attributes)| sum + attributes[:atheism] } # => 1411
Σy = countries.inject(0) { |sum,(country,attributes)| sum + attributes[:crime] } # => 200.57
Σxy = countries.inject(0) { |sum,(country,attributes)| sum + attributes[:crime] * attributes[:atheism] } # => 4930.82
Σx² = countries.inject(0) { |sum,(country,attributes)| sum + attributes[:atheism]**2 } # => 64181
Σ²x = countries.inject(0) { |sum,(country,attributes)| sum + attributes[:atheism] }**2 # => 1990921
Σy² = countries.inject(0) { |sum,(country,attributes)| sum + attributes[:crime]**2 } # => 2340.8835
Σ²y = countries.inject(0) { |sum,(country,attributes)| sum + attributes[:crime] }**2 # => 40228.3249
include Math
numerator = n*Σxy - Σx*Σy # => -61117.3700000001
denominator = sqrt(n*Σx² - Σ²x) * sqrt(n*Σy² - Σ²y) # => 241701.344644796
r = numerator / denominator # => -0.252863177446605
puts r
# since r is negative, we know they are negatively correlated
# value of 0.25 is half of 0.5, therefore the correlation is weak
# So the conclusion is that atheism implies lower homicide rates, though not very strongly.
# Uncertainty in this analysis:
# * There are many ways this could be measured, I googled "countries by crime" and found homicide rates,
# but you could also look at violence, arrests, standard of living, etc.
# * data is from 2004, so 6 or 7 years old.
# * homicide rates are from wikipedia, I did not check their sources (though they did cite them)
# * this really correlates weak/negative atheism, for example, Bhuddists are included in this category,
# there is also a comment on the site I got the data from that says refusal to disclose gets lumped in with nonbelief
# * there are approximately 195 countries in the world, but I only have data for 45 of them, that is a significant deficiency
# * in some cases, homicide rates for 2004 did not exist, I selected the next closest year
# * I may have made a mistake, feel free to check my work
# * These countries have data on atheism but not homicide: ["Vietnam", "Taiwan", "North Korea", "Cuba"]
# though Taiwan's status as a country is questionable.
# also, atheism data listed Britain, I changed that to United Kingdom
#!/usr/bin/env ruby -Ku
# this file was used to scrape the data, see other file for calculations, analysis, conclusion
# as of 2004
atheism = <<-ATHEISM
Sweden 8,986,000 46 – 85% 4,133,560 – 7,638,100
Vietnam 82,690,000 81% 66,978,900
Denmark 5,413,000 43 – 80% 2,327,590 – 4,330,400
Norway 4,575,000 31 – 72% 1,418,250 – 3,294,000
Japan 127,333,000 64 – 65% 81,493,120 – 82,766,450
Czech Republic 10,246,100 54 – 61% 5,328,940 – 6,250,121
Finland 5,215,000 28 – 60% 1,460,200 – 3,129,000
France 60,424,000 43 – 54% 25,982,320 – 32,628,960
South Korea 48,598,000 30 – 52% 14,579,400 – 25,270,960
Estonia 1,342,000 49% 657,580
Germany 82,425,000 41 – 49% 33,794,250 – 40,388,250
Russia 143,782,000 24 – 48% 34,507,680 – 69,015,360
Hungary 10,032,000 32 – 46% 3,210,240 – 4,614,720
Netherlands 16,318,000 39 – 44% 6,364,020 – 7,179,920
United Kingdom 60,271,000 31 – 44% 18,684,010 – 26,519,240
Belgium 10,348,000 42 – 43% 4,346,160 – 4,449,640
Bulgaria 7,518,000 34 – 40% 2,556,120 – 3,007,200
Slovenia 2,011,000 35 – 38% 703,850 – 764,180
Israel 6,199,000 15 – 37% 929,850 – 2,293,630
Canada 32,508,000 19 – 30% 6,176,520 – 9,752,400
Latvia 2,306,000 20 – 29% 461,200 – 668,740
Slovakia 5,424,000 10 – 28% 542,400 – 1,518,720
Switzerland 7,451,000 17 – 27% 1,266,670 – 2,011,770
Austria 8,175,000 18 – 26% 1,471,500 – 2,125,500
Australia 19,913,000 24 – 25% 4,779,120 – 4,978,250
Taiwan 22,750,000 24% 5,460,000
Spain 40,281,000 15 – 24% 6,042,150 – 9,667,440
Iceland 294,000 16 – 23% 47,040 – 67,620
New Zealand 3,994,000 20 – 22% 798,800 – 878,680
Ukraine 47,732,000 20% 9,546,400
Belarus 10,311,000 17% 1,752,870
Greece 10,648,000 16% 1,703,680
North Korea 22,698,000 15%* 3,404,700
Italy 58,057,000 6 – 15% 3,483,420 – 8,708,550
Armenia 2,991,000 14% 418,740
China 1,298,848,000 8 – 14%* 103,907,840 – 181,838,720
Lithuania 3,608,000 13% 469,040
Singapore 4,354,000 13% 566,020
Uruguay 3,399,000 12% 407,880
Kazakhstan 15,144,000 11 – 12% 1,665,840 – 1,817,280
Mongolia 2,751,000 9% 247,590
Portugal 10,524,000 4 – 9% 420,960 – 947,160
United States 293,028,000 3 – 9% 8,790,840 – 26,822,520
Albania 3,545,000 8% 283,600
Argentina 39,145,000 4 – 8% 1,565,800 – 3,131,600
Kyrgyzstan 5,081,000 7% 355,670
Dominican Republic 8,834,000 7% 618,380
Cuba 11,309,000 7%* 791,630
Croatia 4,497,000 7% 314,790
ATHEISM
# country, source, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, most recent
violence = <<-VIOLENCE
El Salvador [6][7][8][9] 37 35 31 33 41 55 55 49 52 71 71
Honduras [10][11][12][13] 50 54 56 34 32 35 43 50 58 67 67
Jamaica [14][15] 34 44 40 36 54 58 49 58 58 58
Guatemala [8][9][16] 26 25 31 35 36 42 45 45 49 52 52
Venezuela [17][18][19] 33 32 38 44 37 37 45 48 52 49 49
Trinidad and Tobago [20] 10 13 15 20 22 33 31 33 46 43 43
Colombia [21][22][23][24][25] 63 65 66 53 44 39 40 39 36 35 35
South Africa [26][27] 50 48 48 43 40 40 41 39 37 34 34
Belize [28][29] 16.3 24.9 33.1 24.9 28.7 30.0 33.0 30.8 33.4 33.4
Brazil [30][31][32] 26.7 27.8 28.4 28.9 27.4 26.6 25.7 25.2 25.2
Dominican Republic [33] 13.09 12.49 14.51 18.73 25.25 26.41 23.56 23.57
Puerto Rico [34][35][36][37][38][39][40][41][42][43] 18 19 20 20 20 20 19 19 20 23 23
Ecuador [44][45][46] 15.07 18.33 18.07 16.24 16.90 19 19
Mexico [47][48][49][50] 14.11 13.94 13.04 10 11 11 11 10 12 15 15
Russia [51][52] 28 30 31 29 27 25 20 17.9 16.5 14.9 14.9
Swaziland [45][47] 88.61 13.05 13.63 13.63
Panama [48][53] 10.1 10.56 9.56 10.8 9.7 11.2 11.3 12.9 12.9
Paraguay [44][47] 12.05 15.02 12.33 12.33
Madagascar [54] 12 12
Nicaragua [55][56] 9 10 11 12 11 12 12 12
Mongolia [44][45] 13.51 12.81 12.01 11.94 11.94
Kazakhstan [44] 12.1 11.5 11.5
Suriname [45] 15.10 10.30 10.30
Papua New Guinea [47] 9.06 9.06
Kyrgyzstan [44][45][47] 8.40 8.15 8.01 9.44 8.48 8.48
Zimbabwe [45][47] 7.24 7.99 8.44 8.44
Lithuania [44][45][47][48] 10.01 10.14 8.45 9.96 9.38 10.77 8.13 8.13
Thailand [44][47] 8.47 8.16 7.92 7.92
Zambia [47] 7.89 7.89
Costa Rica [45][48][57] 6.34 6.64 6.44 6.99 6.23 7.81 7.68 7.68
Belarus [44][45][47][48] 10.13 9.72 9.96 8.91 8.31 8.53 7.53 7.53
Barbados [47] 7.49 7.49
Seychelles [47] 7.39 7.39
Uganda [45] 7.95 7.37 7.37
Georgia [44][45][47] 4.76 6.62 6.22 9.01 7.29 7.29
Estonia [58][59] 10.42 10.02 10.43 13.86 9.40 11.58 8.85 8.19 7.76 7.09 7.09
Ukraine [45][47][60] 8.93 8.51 7.42 7.04 7.04
Turkey (excluding attempts) [58] 9.96 8.60 8.26 7.61 7.05 6.94 6.94
Pakistan [47][61] 6.86 6.86
Sri Lanka [45] 6.42 6.69 6.69
Namibia [48] 6.33 6.35 6.35
Kenya [44] 6.50 5.72 5.72
Albania [48] 6.64 5.68 5.68
Peru [45][48] 4.91 4.25 4.85 5.54 5.54
Argentina [47][48][62][63] 7.17 8.43 9.47 7.9 6.3 5.8 5.27 5.26 5.80 5.45 5.45
United States [64] 5.5 5.6 5.6 5.7 5.5 5.6 5.7 5.6 5.4 5.0 5.0
Latvia (including attempts) [65] 9.19 9.00 8.76 9.44 8.58 5.51 6.45 5.13 5.24 4.82 4.82
Moldova [44][45][47][48] 8.13 8.36 7.99 7.94 6.71 5.83 4.80 4.80
Uruguay [45][47][48] 4.61 6.31 6.46 5.30 5.64 4.3 4.3
Montenegro [44] 3.62 4.16 4.16
Côte d'Ivoire [47] 4.07 4.07
Mauritius [45][47] 2.19 2.95 2.51 2.98 4.00 4.00
Yemen [47] 3.98 3.98
Palestine [44][45] 2.70 4.04 3.85 3.85
Philippines [44][45][47][48] 7.59 7.47 8.20 4.97 4.31 3.83 3.82 3.82
Iran [45] 2.64 2.93 2.93
Bolivia [48] 3.74 2.82 2.82
India [44] 2.88 2.82 2.82
Dominica [47] 2.74 2.74
Bangladesh [44] 2.24 2.64 2.64
Bulgaria (excluding attempts) [45][47][58] 4.07 3.90 3.25 3.17 3.09 2.54 2.54
Romania (excluding attempts) [45][48][58] 2.50 2.66 2.52 2.52 2.37 2.09 2.76 2.51 2.51
Armenia [44][45][47] 3.34 2.50 2.48 1.82 2.49 2.49
Azerbaijan [45][47][48] 2.81 2.69 2.59 2.20 2.41 2.41
China [66] 2.36 2.36
Malaysia [44][47] 2.36 1.94 2.31 2.31
South Korea [44][45][47] 2.02 2.19 2.18 2.18
Finland (excluding attempts) [45][47][48][60] 2.86 3.01 2.54 1.97 2.75 2.17 2.17
Macedonia (excluding attempts) [44][58] 2.52 2.71 2.94 3.46 2.41 2.16 2.01 2.01
New Zealand [47][48][60] 1.17 1.16 1.29 2.00 2.00
Slovakia (excluding attempts) [58] 2.65 2.40 2.38 2.71 2.27 1.97 1.97
Israel [45][67] 3.01 2.62 2.68 1.87 1.87
Bosnia and Herzegovina [44] 1.79 1.86 1.86
Nepal [44][48] 2.56 3.42 2.08 1.84 1.84
Canada [45][47][48][68][69][70] 1.59 1.67 1.67 1.74 1.95 2.05 1.86 1.80 1.83 1.83
Jordan [44] 1.21 1.75 1.75
Chile [45][47][71] 1.9 1.9 1.8 1.7 1.9 1.9 1.9 1.6 1.7 1.7
Cyprus (excluding attempts) [44][45][48][58] 1.16 1.00 0.43 2.10 2.05 2.14 1.66 1.66
Croatia (excluding attempts) [44][45][48][58] 2.47 1.86 1.79 1.53 1.83 1.49 1.62 1.62
France (excluding attempts) [45][47][58][60] 1.79 1.76 1.87 1.64 1.64 1.60 1.60
Bermuda [45] 3.14 1.56 1.56
Belgium (excluding attempts) [58] 2.00 2.35 2.23 1.83 2.05 1.65 1.81 1.85 1.49 1.49
Serbia [44] 1.44 1.46 1.46
Hungary (excluding attempts) [45][47][48][60] 2.05 2.49 2.00 2.33 2.09 1.64 1.47 1.38 1.38
Czech Republic (excluding attempts) [44][45][47][48][58] 1.69 1.45 1.36 1.56 1.34 1.02 1.33 1.33
Poland (excluding attempts) [44][45][48][58] 2.21 2.01 1.87 1.72 1.64 1.45 1.28 1.28
Maldives [45][48] 2.50 2.79 1.28 1.28
United Kingdom (excluding attempts) [58][72] 1.70 1.78 2.06 1.76 1.75 1.49 1.28 1.28
Portugal (excluding attempts) [58] 1.14 1.02 1.15 1.43 1.37 1.26 1.26
Tunisia [47][48] 1.18 1.26 1.22 1.22
Australia [45][45][47][47][60][73][74] 1.57 1.53 1.28 1.45 1.42 1.2 1.2 1.2
Spain (excluding attempts) [58][75] 1.38 1.43 1.38 1.41 1.23 1.20 1.20 1.20
Syria [45] 1.04 1.14 1.14
Ireland (excluding attempts) [58][76] 1.48 1.51 1.51 1.31 1.11 1.46 1.59 1.84 1.01 1.12 1.12
Senegal [77][78] 0.33 1.1 1.1
Italy (excluding attempts) [44][45][47][48] 1.29 1.23 1.12 1.24 1.23 1.04 1.06 1.06
Indonesia [47] 1.05 1.05
Denmark (excluding attempts) [58][79] 1.48 1.16 1.08 1.52 1.11 1.29 0.84 0.97 1.01 1.01
Switzerland (excluding attempts) [58] 0.96 1.19 1.19 1.00 1.07 1.01 1.01
Slovenia (excluding attempts) [58] 1.81 0.75 1.45 1.05 1.45 1.00 1.00
Malta (excluding attempts) [58] 1.05 1.28 1.27 0.00 1.75 0.99 0.99
Kuwait [48] 1.71 0.99 0.99
Greece (excluding attempts) [44][47][58] 1.45 1.27 0.98 1.11 0.99 1.14 0.98 0.98
Bahrain [44][45] 0.43 0.98 0.55 0.95 0.95
Sweden (excluding attempts) [58] 1.00 0.95 1.04 0.91 1.17 0.92 0.92
United Arab Emirates [45] 1.12 0.63 1.36 0.92 0.92
Saudi Arabia [47][48] 0.51 0.87 0.92 0.92
Netherlands (excluding attempts) [80] 1.13 1.26 1.21 1.25 1.17 1.07 0.78 0.87 0.91 0.92 0.92
Luxembourg (excluding attempts) [58] 0.92 1.37 0.90 0.70 0.44 0.87 0.87
Germany (excluding attempts) [44][45][47][48][81] 1.25 1.14 1.17 1.06 1.06 1.06 0.98 0.92 0.88 0.86 0.86
Qatar [45][47] 0.17 0.55 0.77 0.77
Austria (excluding attempts) [44][58] 1.02 0.87 0.81 0.62 0.72 0.65 0.73 0.73
Algeria [44][45] 2.04 1.39 0.62 0.64 0.64
Norway (excluding attempts) [44][58] 1.07 0.82 1.02 1.12 0.78 0.71 0.72 0.64 0.69 0.60 0.60
Oman [48] 0.52 0.59 0.59
Lebanon [44] 2.34 0.57 0.57
Morocco [44][45][48] 0.44 0.48 0.50 0.47 0.52 0.53 0.53
Brunei [44][45] 0.56 1.37 0.00 0.50 0.50
Hong Kong [45][47][60] 0.56 0.73 0.63 0.49 0.49
Japan [44][47][60] 0.50 0.53 0.59 0.64 0.44 0.44
Singapore [44][45][47][60] 0.92 0.57 0.49 0.48 0.39 0.38 0.38
Iceland (excluding attempts) [58] 1.79 0.35 1.40 0.00 1.03 1.02 0.31 0.63 0.31 0.31
Liechtenstein (excluding attempts) [58] 0.00 0.00 0.00 0.00 2.92 0.00 0.00
VIOLENCE
countries = Hash.new
atheism.split("\n").each do |line|
country = line[/^[^\d]*/].strip
percent = line[/[0-9.]*%/].to_i
countries[country] = Hash[ :atheism , percent ]
end
to_delete = Array.new
countries.each do |country,attributes|
unless line = violence[/^.*#{country}.*$/i]
to_delete << country
next
end
results = line.gsub(/\(.*?\)/,'').gsub(/\[.*?\]/,'').split("\t")
results.shift
results.shift
p results
raise "incorrect number of columns #{results.inspect}" unless results.size == 11
_2000 , _2001 , _2002 , _2003 , _2004 , _2005 , _2006 , _2007 , _2008 , _2009 , recent = results
attributes[:crime] = [_2004,_2005,_2003,_2006,_2002,_2007,_2001,_2008,_2000,_2009,recent].find { |rate| !rate.empty? }.to_f
puts "#{country} = #{attributes.inspect}"
end
puts
puts "DELETING: #{to_delete.inspect}"
puts
countries.delete_if { |country,attributes| to_delete.include? country }
require 'pp'
pp countries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment