File size: 8,366 Bytes
48a75b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# -*- coding: utf-8 -*-
"""Long-Term-Care-Aggregated-Data.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/14YdgB8b4TtNetbHpTstGH7W4DLa3Yyxq
"""

from datasets import GeneratorBasedBuilder, DownloadManager, DatasetInfo, Array3D, BuilderConfig, SplitGenerator, Version
from datasets.features import Features, Value, Sequence
import datasets
import pandas as pd
import json
import zipfile
from PIL import Image
import numpy as np
import io
import csv
import json
import os
from typing import List
import datasets
import logging

_CITATION = """\
@misc{long_term_care_aggregated_dataset,
title = {Long-Term Care Aggregated Dataset},
author = {Kao, Hsuan-Chen (Justin)},
year = {2024},
publisher = {Hugging Face},
url = {https://github.com/justinkao44/STA663_Project_1},
}
"""

_DESCRIPTION = """\
The Long-Term Care Aggregated Dataset is a collection of insurance data for 'incidence' and 'termination' categories.
It is compiled from Long Term Care insurance policies data, providing insights into trends and patterns in insurance claims
and terminations. This dataset can be used for actuarial analysis, risk assessment, and to inform insurance product development.
"""

_HOMEPAGE = "https://github.com/justinkao44/STA663_Project_1"

_LICENSE = "Apache-2.0"

_URLS = {
    "train_incidence": "https://github.com/justinkao44/STA663_Project_1/raw/main/train_filtered_incidence_df.zip",
    "train_termination": "https://github.com/justinkao44/STA663_Project_1/raw/main/train_filtered_termination_df.zip",
    "validation_incidence": "https://github.com/justinkao44/STA663_Project_1/raw/main/validation_filtered_incidence_df.zip",
    "validation_termination": "https://github.com/justinkao44/STA663_Project_1/raw/main/validation_filtered_termination_df.zip",
}


class LongTermCareAggregatedData(datasets.GeneratorBasedBuilder):
    """Dataset for insurance 'incidence' and 'termination' data."""

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(name="incidence", version=datasets.Version("1.0.0"), description="This part of the dataset includes incidence features"),
        datasets.BuilderConfig(name="termination", version=datasets.Version("1.0.0"), description="This part of the dataset includes termination features"),
    ]

    def _info(self):
        if self.config.name == "incidence":
            features = datasets.Features({
                "Group_Indicator": datasets.Value("string"),
                "Gender": datasets.Value("string"),
                "Issue_Age_Bucket": datasets.Value("string"),
                "Incurred_Age_Bucket": datasets.Value("string"),
                "Issue_Year_Bucket": datasets.Value("string"),
                "Policy_Year": datasets.Value("string"),
                "Marital_Status": datasets.Value("string"),
                "Premium_Class": datasets.Value("string"),
                "Underwriting_Type": datasets.Value("string"),
                "Coverage_Type_Bucket": datasets.Value("string"),
                "Tax_Qualification_Status": datasets.Value("string"),
                "Inflation_Rider": datasets.Value("string"),
                "Rate_Increase_Flag": datasets.Value("string"),
                "Restoration_of_Benefits": datasets.Value("string"),
                "NH_Orig_Daily_Ben_Bucket": datasets.Value("string"),
                "ALF_Orig_Daily_Ben_Bucket": datasets.Value("string"),
                "HHC_Orig_Daily_Ben_Bucket": datasets.Value("string"),
                "NH_Ben_Period_Bucket": datasets.Value("string"),
                "ALF_Ben_Period_Bucket": datasets.Value("string"),
                "HHC_Ben_Period_Bucket": datasets.Value("string"),
                "NH_EP_Bucket": datasets.Value("string"),
                "ALF_EP_Bucket": datasets.Value("string"),
                "HHC_EP_Bucket": datasets.Value("string"),
                "Region": datasets.Value("string"),
                "Active_Exposure": datasets.Value("float64"),
                "Total_Exposure": datasets.Value("float64"),
                "Claim_Count": datasets.Value("int32"),
                "Count_NH": datasets.Value("int32"),
                "Count_ALF": datasets.Value("int32"),
                "Count_HHC": datasets.Value("int32"),
                "Count_Unk": datasets.Value("int32"),
            })
        elif self.config.name == "termination":
            features = datasets.Features({
                "Gender": datasets.Value("string"),
                "Incurred_Age_Bucket": datasets.Value("string"),
                "Incurred_Year_Bucket": datasets.Value("string"),
                "Claim_Type": datasets.Value("string"),
                "Region": datasets.Value("string"),
                "Diagnosis_Category": datasets.Value("string"),
                "Claim_Duration": datasets.Value("string"),
                "Exposure": datasets.Value("string"),
                "Deaths": datasets.Value("string"),
                "Recovery": datasets.Value("string"),
                "Terminations": datasets.Value("string"),
                "Benefit_Expiry": datasets.Value("string"),
                "Others_Terminations": datasets.Value("string"),
            })
        else:
            raise ValueError(f"BuilderConfig name not recognized: {self.config.name}")

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            supervised_keys=None,
            homepage="https://www.soa.org/resources/experience-studies/2020/2000-2016-ltc-aggregate-database/",
            citation="Please cite this dataset as: Society of Actuaries (SOA). (2020). Long Term Care Insurance Aggregate Experience Data, 2000-2016."
        )


    def _split_generators(self, dl_manager):
        downloaded_files = dl_manager.download(_URLS)

        train_file = downloaded_files[f"train_{self.config.name}"]
        validation_file = downloaded_files[f"validation_{self.config.name}"]

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "data_file": train_file,
                    "split": self.config.name,
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "data_file": validation_file,
                    "split": self.config.name,
                },
            ),
        ]

    def _generate_examples(self, data_file, split):
        with zipfile.ZipFile(data_file, 'r') as zip_ref:
            # Assume each zip contains only one CSV file
            csv_filename = zip_ref.namelist()[0]
            with zip_ref.open(csv_filename) as csv_file:
                dataframe = pd.read_csv(io.TextIOWrapper(csv_file))

        feature_columns = self._get_feature_columns_by_config()

        for idx, row in dataframe.iterrows():
            feature_dict = {column: row[column] for column in feature_columns if column in dataframe.columns}
            yield idx, feature_dict

    def _get_feature_columns_by_config(self):
        if self.config.name == "incidence":
            return [
                "Group_Indicator", "Gender", "Issue_Age_Bucket", "Incurred_Age_Bucket",
                "Issue_Year_Bucket", "Policy_Year", "Marital_Status", "Premium_Class",
                "Underwriting_Type", "Coverage_Type_Bucket", "Tax_Qualification_Status",
                "Inflation_Rider", "Rate_Increase_Flag", "Restoration_of_Benefits",
                "NH_Orig_Daily_Ben_Bucket", "ALF_Orig_Daily_Ben_Bucket", "HHC_Orig_Daily_Ben_Bucket",
                "NH_Ben_Period_Bucket", "ALF_Ben_Period_Bucket", "HHC_Ben_Period_Bucket",
                "NH_EP_Bucket", "ALF_EP_Bucket", "HHC_EP_Bucket", "Region",
                "Active_Exposure", "Total_Exposure", "Claim_Count", "Count_NH", "Count_ALF", "Count_HHC", "Count_Unk",
            ]
        elif self.config.name == "termination":
            return [
                "Gender", "Incurred_Age_Bucket", "Incurred_Year_Bucket", "Claim_Type",
                "Region", "Diagnosis_Category", "Claim_Duration", "Exposure", "Deaths",
                "Recovery", "Terminations", "Benefit_Expiry", "Others_Terminations",
            ]
        else:
            raise ValueError(f"Config name not recognized: {self.config.name}")