import numpy as np
import pandas as pd
from pandas import DataFrame

############################################################
# Life tables
# https://www.ssa.gov/oact/STATS/table4c6.html
############################################################

############################################################
# Male life table
############################################################

# survivors from 100000 births

MlivesArray = [100000, 99348, 99302, 99273, 99252, 99235, 99219, 99205, 99192, 99180,
               99170, 99161, 99151, 99138, 99119, 99091, 99052, 99003, 98943, 98870,
               98785, 98685, 98572, 98449, 98321, 98191, 98060, 97928, 97795, 97659,
               97519, 97376, 97230, 97080, 96927, 96772, 96612, 96448, 96277, 96097,
               95908, 95708, 95493, 95262, 95012, 94739, 94441, 94115, 93759, 93368,
               92940, 92472, 91961, 91406, 90804, 90153, 89450, 88693, 87883, 87022,
               86112, 85147, 84125, 83042, 81899, 80691, 79412, 78054, 76613, 75084,
               73461, 71732, 69889, 67930, 65853, 63657, 61329, 58859, 56249, 53504,
               50629, 47621, 44484, 41233, 37890, 34482, 31040, 27598, 24201, 20896,
               17735, 14768, 12043, 9599, 7463, 5647, 4157, 2977, 2075, 1410,
               935, 605, 380, 232, 137, 78, 43, 23, 11, 5,
               2, 1, 0, 0, 0, 0, 0, 0, 0, 0, ]

MlivesSeries = pd.Series(MlivesArray)

# life expectancy
MLEarray = [76.28, 75.78, 74.82, 73.84, 72.85, 71.87, 70.88, 69.89, 68.9, 67.9,
            66.91, 65.92, 64.92, 63.93, 62.94, 61.96, 60.99, 60.02, 59.05, 58.09,
            57.14, 56.2, 55.27, 54.33, 53.4, 52.47, 51.54, 50.61, 49.68, 48.75,
            47.82, 46.89, 45.96, 45.03, 44.1, 43.17, 42.24, 41.31, 40.38, 39.46,
            38.53, 37.61, 36.7, 35.78, 34.88, 33.98, 33.08, 32.19, 31.32, 30.44,
            29.58, 28.73, 27.89, 27.05, 26.23, 25.41, 24.61, 23.82, 23.03, 22.25,
            21.48, 20.72, 19.97, 19.22, 18.48, 17.75, 17.03, 16.32, 15.61, 14.92,
            14.24, 13.57, 12.92, 12.27, 11.65, 11.03, 10.43, 9.85, 9.28, 8.73,
            8.2, 7.68, 7.19, 6.72, 6.27, 5.84, 5.43, 5.04, 4.68, 4.34,
            4.03, 3.74, 3.47, 3.23, 3.01, 2.82, 2.64, 2.49, 2.36, 2.24,
            2.12, 2.01, 1.9, 1.8, 1.7, 1.6, 1.51, 1.42, 1.34, 1.26,
            1.18, 1.11, 1.04, 0.97, 0.9, 0.84, 0.78, 0.72, 0.67, 0.61,
            ]

MLEseries = pd.Series(MLEarray)

# death probability
MdeathrateArray = [0.006519, 0.000462, 0.000291, 0.000209, 0.000176, 0.000159, 0.000146, 0.000133, 0.000118, 0.000102,
                   0.000091, 0.000096, 0.000128, 0.000195, 0.000288, 0.000389, 0.000492, 0.000607, 0.000735, 0.000869,
                   0.001011, 0.001145, 0.001246, 0.001301, 0.001321, 0.00133, 0.001345, 0.001363, 0.001391, 0.001427,
                   0.001467, 0.001505, 0.001541, 0.001573, 0.001606, 0.001648, 0.001704, 0.001774, 0.001861, 0.001967,
                   0.002092, 0.00224, 0.002418, 0.002629, 0.002873, 0.003146, 0.003447, 0.003787, 0.004167, 0.004586,
                   0.005038, 0.00552, 0.006036, 0.006587, 0.00717, 0.007801, 0.008466, 0.009133, 0.009792, 0.010462,
                   0.011197, 0.012009, 0.012867, 0.013772, 0.014749, 0.015852, 0.017097, 0.018463, 0.019959, 0.021616,
                   0.023528, 0.025693, 0.028041, 0.030567, 0.033347, 0.036572, 0.040276, 0.044348, 0.048797, 0.053739,
                   0.059403, 0.065873, 0.073082, 0.08107, 0.089947, 0.099842, 0.110863, 0.123088, 0.136563, 0.151299,
                   0.167291, 0.18452, 0.202954, 0.222555, 0.243272, 0.263821, 0.283833, 0.302916, 0.320672, 0.336706,
                   0.353541, 0.371218, 0.389779, 0.409268, 0.429732, 0.451218, 0.473779, 0.497468, 0.522341, 0.548458,
                   0.575881, 0.604675, 0.634909, 0.666655, 0.699987, 0.734987, 0.771736, 0.810323, 0.850839, 0.893381,
                   ]
MdeathrateSeries = pd.Series(MdeathrateArray)

MlivesSeries.to_csv('MLivesSeries.csv', index_label='index')
MLEseries.to_csv('MLEseries.csv', index_label='index')
MdeathrateSeries.to_csv('MdeathrateSeries.csv', index_label='index')


############################################################
# Female life table
############################################################

FlivesArray = [100000, 99462, 99425, 99403, 99387, 99373, 99361, 99351, 99341, 99331,
               99322, 99312, 99303, 99291, 99278, 99262, 99243, 99220, 99194, 99165,
               99132, 99095, 99054, 99010, 98963, 98915, 98864, 98811, 98755, 98697,
               98635, 98569, 98500, 98426, 98348, 98265, 98176, 98081, 97979, 97870,
               97753, 97627, 97491, 97343, 97182, 97004, 96810, 96597, 96364, 96109,
               95829, 95524, 95193, 94834, 94449, 94038, 93598, 93126, 92623, 92090,
               91526, 90927, 90287, 89600, 88858, 88054, 87177, 86223, 85187, 84069,
               82864, 81561, 80147, 78616, 76961, 75177, 73244, 71148, 68888, 66467,
               63880, 61114, 58159, 55016, 51694, 48205, 44565, 40796, 36933, 33017,
               29104, 25257, 21542, 18027, 14775, 11839, 9267, 7083, 5285, 3852,
               2745, 1909, 1292, 850, 541, 333, 197, 112, 61, 31,
               15, 7, 3, 1, 0, 0, 0, 0, 0, 0,]

FlivesSeries = pd.Series(FlivesArray)

FLEarray = [81.05, 80.49, 79.52, 78.54, 77.55, 76.56, 75.57, 74.58, 73.58, 72.59,
            71.6, 70.6, 69.61, 68.62, 67.63, 66.64, 65.65, 64.67, 63.68, 62.7,
            61.72, 60.75, 59.77, 58.8, 57.82, 56.85, 55.88, 54.91, 53.94, 52.97,
            52.01, 51.04, 50.08, 49.11, 48.15, 47.19, 46.23, 45.28, 44.33, 43.37,
            42.43, 41.48, 40.54, 39.6, 38.66, 37.73, 36.81, 35.89, 34.97, 34.06,
            33.16, 32.27, 31.38, 30.49, 29.62, 28.74, 27.88, 27.01, 26.16, 25.31,
            24.46, 23.62, 22.78, 21.95, 21.13, 20.32, 19.52, 18.73, 17.95, 17.18,
            16.43, 15.68, 14.95, 14.23, 13.53, 12.83, 12.16, 11.5, 10.86, 10.24,
            9.64, 9.05, 8.48, 7.94, 7.42, 6.92, 6.44, 5.99, 5.57, 5.17,
            4.8, 4.45, 4.13, 3.84, 3.57, 3.34, 3.12, 2.93, 2.76, 2.6,
            2.45, 2.3, 2.17, 2.03, 1.91, 1.78, 1.67, 1.56, 1.45, 1.35,
            1.26, 1.17, 1.08, 1, 0.92, 0.85, 0.78, 0.72, 0.67, 0.61,]

FLEseries = pd.Series(FLEarray)

FdeathrateArray =[0.005377, 0.000379, 0.000221, 0.000162, 0.000133, 0.000119, 0.000109, 0.000101, 0.000096, 0.000093,
                  0.000094, 0.0001, 0.000112, 0.000134, 0.000162, 0.000194, 0.000226, 0.000261, 0.000297, 0.000334,
                  0.000373, 0.000412, 0.000446, 0.000472, 0.000493, 0.000513, 0.000537, 0.000563, 0.000593, 0.000627,
                  0.000664, 0.000705, 0.000748, 0.000794, 0.000845, 0.000903, 0.000968, 0.001038, 0.001113, 0.001196,
                  0.001287, 0.001393, 0.001517, 0.001662, 0.001827, 0.002005, 0.002198, 0.002412, 0.002648, 0.002904,
                  0.003182, 0.003473, 0.003767, 0.004058, 0.004352, 0.004681, 0.00504, 0.0054, 0.005756, 0.006128,
                  0.006545, 0.007034, 0.007607, 0.008281, 0.009057, 0.009953, 0.01095, 0.01201, 0.013124, 0.01433,
                  0.015728, 0.017338, 0.019108, 0.021041, 0.023191, 0.025713, 0.028609, 0.03176, 0.035157, 0.03892,
                  0.043289, 0.048356, 0.054041, 0.060384, 0.067498, 0.075516, 0.084556, 0.094703, 0.106014, 0.118513,
                  0.132206, 0.147092, 0.163154, 0.180371, 0.198714, 0.217264, 0.235735, 0.25381, 0.271155, 0.287424,
                  0.30467, 0.32295, 0.342327, 0.362867, 0.384639, 0.407717, 0.43218, 0.458111, 0.485597, 0.514733,
                  0.545617, 0.578354, 0.613055, 0.649839, 0.688829, 0.730159, 0.771736, 0.810323, 0.850839, 0.893381,]
    
FdeathrateSeries = pd.Series(FdeathrateArray)

def genLifetable(livesSeries, leSeries, ret_age, ret_length):
    """Create frame with life expectancies, lives"""

    # check bounds of lifetable
    # assert start age, end age between the two

    # 2nd version - take DataFrame where everything lines up by age

    end_age = ret_age + ret_length
    survival = livesSeries[ret_age:end_age]
    survival = survival / float(survival[ret_age])
    deathrate = survival - survival.shift(-1)
    deathrate.ix[end_age-1] = 1 - np.sum(deathrate)
    lifetable = DataFrame(survival, columns=['survival'])
    LE = leSeries[ret_age:end_age]
    lifetable['life_expectancy'] = LE
    lifetable['deathrate'] = deathrate
    return lifetable

