In [39]:
### Leah Libresco
### Leah.Libresco@fivethirtyeight.com

In [40]:
import numpy as np
import pandas as pd

In [41]:
# List of 12 U.S. religious groups stuided by Pew
religions = ['Buddhist', 'Catholic', 'Evangel Prot', 'Hindu', 'Hist Black Prot', 'Jehovahs Witness', 'Jewish', 'Mainline Prot', 'Mormon', 'Muslim', 'Orthodox Christian', 'Unaffiliated']

In [42]:
# Create a .csv file with a function to write to it 
csv = open("current.csv", 'w')
csv.truncate()

def write_row(matrix):
  arr = np.asarray(matrix[0])[0]
  row = ','.join([str(a) for a in arr]) + '\n'
  csv.write(row)

In [43]:
# Intitial distribution of religions in US
first = np.matrix([.007, .208, .254, .007, .065, .008, .019, .147, .016, .009, .005, .228])

# Normed to sum to 100%
current = first / np.sum(first)
t0 = current
write_row(current)

In [44]:
# Transition matrix 
trans = np.matrix(((0.390296314, 0.027141947, 0.06791021, 0.001857564, 0, 0, 0.011166082, 0.059762879, 0, 0, 0, 0.396569533),
                   (0.005370791, 0.593173325, 0.103151608, 0.000649759, 0.010486747, 0.005563864, 0.002041424, 0.053825329, 0.004760476, 0.001130529, 0.000884429, 0.199488989),
                   (0.00371836, 0.023900817, 0.650773331, 0.000250102, 0.016774503, 0.003098214, 0.001865491, 0.122807467, 0.004203107, 0.000186572, 0.002123778, 0.151866648),
                   (0, 0, 0.0033732, 0.804072618, 0, 0.001511151, 0, 0.01234639, 0, 0.00209748, 0, 0.17659916),
                   (0.002051357, 0.016851659, 0.09549708, 0, 0.699214315, 0.010620473, 0.000338804, 0.024372871, 0.000637016, 0.009406884, 0.000116843, 0.129892558),
                   (0, 0.023278276, 0.109573979, 0, 0.077957568, 0.336280578, 0, 0.074844833, 0.007624035, 0, 0, 0.35110361),
                   (0.006783201, 0.004082693, 0.014329604, 0, 0, 0.000610585, 0.745731278, 0.009587587, 0, 0, 0.002512334, 0.184058682),
                   (0.005770357, 0.038017215, 0.187857555, 0.000467601, 0.008144075, 0.004763516, 0.003601208, 0.451798506, 0.005753587, 0.000965543, 0.00109818, 0.25750798),
                   (0.007263135, 0.01684885, 0.06319935, 0.000248467, 0.0059394, 0, 0.001649896, 0.03464334, 0.642777489, 0.002606278, 0, 0.208904711),
                   (0, 0.005890381, 0.023573308, 0, 0.011510643, 0, 0.005518343, 0.014032084, 0, 0.772783807, 0, 0.15424369),
                   (0.004580353, 0.042045841, 0.089264134	, 0, 0.00527346, 0, 0, 0.061471387, 0.005979218, 0.009113978, 0.526728084, 0.243246723),
                   (0.006438308, 0.044866331, 0.1928814, 0.002035375, 0.04295005, 0.010833621, 0.011541439, 0.09457963, 0.01365141, 0.005884336, 0.002892072, 0.525359211)))

# Fertility array
fert = np.matrix(((2.1, 2.3, 2.3, 2.1, 2.5, 2.1, 2, 1.9, 3.4, 2.8, 2.1, 1.7)))

# Create data frame for printing later
religionDataFrame = pd.DataFrame()

In [45]:
# Run model
for x in range(0,100):

    ### beginning of conversion step
        
    # apply transition matrix to current distribution
    current = current * trans
    
    ### beginning of fertility step
    
    # divide by two for couple number
    current = current/2
    
    # adjust by fertility
    
    current = np.multiply(fert, current)
    
    # normalize to 100%
    
    current = current / np.sum(current)
    
    write_row(current)
    
    # add to data frame
    religionDataFrame = religionDataFrame.append(pd.DataFrame(current), ignore_index=True)

csv.close()

In [47]:
# Print data frame with results
religionDataFrame.columns = religions
religionDataFrame


Out[47]:
Buddhist Catholic Evangel Prot Hindu Hist Black Prot Jehovahs Witness Jewish Mainline Prot Mormon Muslim Orthodox Christian Unaffiliated
0 0.007924 0.170263 0.309520 0.006728 0.080042 0.008986 0.018492 0.128123 0.028069 0.013271 0.004466 0.224115
1 0.007981 0.140118 0.331889 0.006234 0.090308 0.008980 0.017417 0.120477 0.039642 0.017382 0.004070 0.215501
2 0.007856 0.119514 0.340401 0.005752 0.097890 0.008785 0.016412 0.117260 0.051127 0.021558 0.003812 0.209631
3 0.007710 0.105453 0.341544 0.005303 0.103242 0.008572 0.015526 0.115117 0.062488 0.025770 0.003629 0.205648
4 0.007583 0.095775 0.338780 0.004895 0.106781 0.008375 0.014768 0.113124 0.073661 0.029978 0.003485 0.202793
5 0.007482 0.088996 0.334004 0.004532 0.108869 0.008197 0.014130 0.111103 0.084574 0.034138 0.003364 0.200612
6 0.007402 0.084125 0.328280 0.004213 0.109811 0.008031 0.013595 0.109070 0.095151 0.038205 0.003258 0.198860
7 0.007339 0.080515 0.322217 0.003934 0.109868 0.007874 0.013148 0.107080 0.105324 0.042139 0.003162 0.197401
8 0.007288 0.077748 0.316165 0.003692 0.109259 0.007723 0.012773 0.105176 0.115035 0.045908 0.003076 0.196158
9 0.007247 0.075553 0.310324 0.003483 0.108167 0.007578 0.012457 0.103386 0.124239 0.049485 0.002997 0.195083
10 0.007214 0.073759 0.304804 0.003302 0.106743 0.007441 0.012190 0.101724 0.132901 0.052850 0.002925 0.194146
11 0.007186 0.072254 0.299659 0.003148 0.105108 0.007310 0.011963 0.100194 0.141003 0.055991 0.002860 0.193323
12 0.007163 0.070967 0.294911 0.003015 0.103356 0.007187 0.011770 0.098797 0.148534 0.058901 0.002800 0.192599
13 0.007144 0.069849 0.290562 0.002901 0.101562 0.007072 0.011605 0.097526 0.155496 0.061578 0.002747 0.191958
14 0.007128 0.068869 0.286601 0.002804 0.099779 0.006965 0.011462 0.096376 0.161900 0.064025 0.002699 0.191392
15 0.007114 0.068004 0.283011 0.002720 0.098049 0.006867 0.011339 0.095339 0.167762 0.066250 0.002656 0.190890
16 0.007102 0.067237 0.279769 0.002649 0.096397 0.006777 0.011232 0.094407 0.173106 0.068260 0.002617 0.190446
17 0.007093 0.066557 0.276852 0.002587 0.094844 0.006695 0.011139 0.093571 0.177959 0.070069 0.002582 0.190053
18 0.007084 0.065953 0.274235 0.002535 0.093398 0.006620 0.011058 0.092823 0.182351 0.071688 0.002551 0.189705
19 0.007077 0.065416 0.271894 0.002489 0.092065 0.006553 0.010988 0.092155 0.186313 0.073130 0.002524 0.189397
20 0.007071 0.064939 0.269805 0.002450 0.090845 0.006492 0.010925 0.091561 0.189877 0.074411 0.002499 0.189125
21 0.007066 0.064516 0.267944 0.002417 0.089735 0.006437 0.010871 0.091033 0.193076 0.075542 0.002477 0.188885
22 0.007062 0.064142 0.266290 0.002387 0.088731 0.006389 0.010823 0.090564 0.195942 0.076539 0.002458 0.188674
23 0.007059 0.063810 0.264823 0.002362 0.087827 0.006345 0.010781 0.090148 0.198503 0.077414 0.002441 0.188487
24 0.007056 0.063517 0.263523 0.002341 0.087016 0.006306 0.010744 0.089781 0.200789 0.078178 0.002425 0.188323
25 0.007053 0.063259 0.262374 0.002322 0.086291 0.006272 0.010711 0.089456 0.202827 0.078845 0.002412 0.188179
26 0.007051 0.063030 0.261359 0.002305 0.085644 0.006242 0.010682 0.089170 0.204642 0.079423 0.002400 0.188052
27 0.007049 0.062829 0.260463 0.002291 0.085068 0.006215 0.010657 0.088917 0.206256 0.079923 0.002390 0.187941
28 0.007048 0.062652 0.259674 0.002279 0.084557 0.006191 0.010635 0.088695 0.207690 0.080353 0.002380 0.187844
29 0.007047 0.062497 0.258980 0.002268 0.084104 0.006170 0.010615 0.088500 0.208965 0.080723 0.002372 0.187759
... ... ... ... ... ... ... ... ... ... ... ... ...
70 0.007056 0.061429 0.254164 0.002197 0.080749 0.006019 0.010457 0.087162 0.219413 0.081818 0.002315 0.187222
71 0.007056 0.061429 0.254164 0.002197 0.080746 0.006019 0.010457 0.087162 0.219435 0.081798 0.002315 0.187223
72 0.007056 0.061429 0.254164 0.002197 0.080744 0.006019 0.010456 0.087163 0.219457 0.081777 0.002315 0.187224
73 0.007056 0.061429 0.254164 0.002197 0.080742 0.006019 0.010456 0.087163 0.219477 0.081758 0.002315 0.187224
74 0.007056 0.061429 0.254165 0.002197 0.080740 0.006018 0.010456 0.087163 0.219496 0.081739 0.002315 0.187225
75 0.007057 0.061429 0.254165 0.002197 0.080738 0.006018 0.010456 0.087163 0.219514 0.081721 0.002315 0.187226
76 0.007057 0.061430 0.254166 0.002197 0.080737 0.006018 0.010455 0.087164 0.219531 0.081703 0.002315 0.187226
77 0.007057 0.061430 0.254167 0.002197 0.080735 0.006018 0.010455 0.087164 0.219548 0.081686 0.002315 0.187227
78 0.007057 0.061430 0.254168 0.002197 0.080734 0.006018 0.010455 0.087165 0.219563 0.081670 0.002315 0.187228
79 0.007057 0.061431 0.254169 0.002197 0.080733 0.006018 0.010455 0.087165 0.219578 0.081654 0.002315 0.187228
80 0.007057 0.061431 0.254170 0.002197 0.080732 0.006018 0.010455 0.087166 0.219592 0.081639 0.002315 0.187229
81 0.007058 0.061431 0.254171 0.002197 0.080731 0.006018 0.010454 0.087166 0.219605 0.081625 0.002315 0.187230
82 0.007058 0.061432 0.254172 0.002197 0.080730 0.006018 0.010454 0.087166 0.219617 0.081610 0.002315 0.187230
83 0.007058 0.061432 0.254173 0.002197 0.080729 0.006018 0.010454 0.087167 0.219629 0.081597 0.002315 0.187231
84 0.007058 0.061432 0.254174 0.002197 0.080728 0.006018 0.010454 0.087167 0.219641 0.081584 0.002315 0.187231
85 0.007058 0.061433 0.254175 0.002197 0.080728 0.006018 0.010454 0.087168 0.219652 0.081571 0.002315 0.187232
86 0.007058 0.061433 0.254176 0.002197 0.080727 0.006018 0.010454 0.087168 0.219662 0.081559 0.002315 0.187232
87 0.007058 0.061433 0.254177 0.002197 0.080727 0.006018 0.010454 0.087169 0.219672 0.081548 0.002315 0.187233
88 0.007059 0.061433 0.254178 0.002197 0.080726 0.006018 0.010453 0.087169 0.219682 0.081536 0.002315 0.187233
89 0.007059 0.061434 0.254179 0.002197 0.080726 0.006018 0.010453 0.087169 0.219691 0.081526 0.002315 0.187234
90 0.007059 0.061434 0.254180 0.002197 0.080725 0.006018 0.010453 0.087170 0.219699 0.081515 0.002315 0.187234
91 0.007059 0.061434 0.254181 0.002197 0.080725 0.006018 0.010453 0.087170 0.219708 0.081505 0.002315 0.187235
92 0.007059 0.061435 0.254182 0.002197 0.080724 0.006018 0.010453 0.087170 0.219716 0.081496 0.002315 0.187235
93 0.007059 0.061435 0.254183 0.002197 0.080724 0.006018 0.010453 0.087171 0.219723 0.081487 0.002315 0.187236
94 0.007059 0.061435 0.254184 0.002197 0.080723 0.006018 0.010453 0.087171 0.219731 0.081478 0.002315 0.187236
95 0.007059 0.061435 0.254185 0.002197 0.080723 0.006018 0.010453 0.087171 0.219738 0.081469 0.002315 0.187236
96 0.007059 0.061436 0.254186 0.002197 0.080723 0.006018 0.010453 0.087172 0.219744 0.081461 0.002315 0.187237
97 0.007059 0.061436 0.254186 0.002197 0.080722 0.006018 0.010452 0.087172 0.219751 0.081453 0.002315 0.187237
98 0.007059 0.061436 0.254187 0.002197 0.080722 0.006018 0.010452 0.087172 0.219757 0.081446 0.002315 0.187237
99 0.007059 0.061436 0.254188 0.002197 0.080722 0.006018 0.010452 0.087173 0.219763 0.081438 0.002315 0.187238

100 rows × 12 columns


In [ ]: