{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":10445,"databundleVersionId":200747,"sourceType":"competition"}],"dockerImageVersionId":30558,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"※少しずつ更新していきます。","metadata":{}},{"cell_type":"markdown","source":"## 生存関数\n\n生存関数は次のように表されます：\n\n$$ S(t) = P(T > t)$$\n\n解釈: 時刻 $t$ までに、ある事象が発生しない確率。\n\n","metadata":{}},{"cell_type":"markdown","source":"$T ~~(T \\geq 0)$ を確率変数、$S(t)=1-F(t)$をその生存関数とするとき、\n$$E[T]=\\int^{\\infty}_0S(t)dt$$\nが成り立ちます。","metadata":{}},{"cell_type":"markdown","source":"## 指数分布の場合\n\nパラメータ$\\lambda$の指数分布の確率密度関数は\n$$f\\left( x\\right) =\\begin{cases}\\lambda e^{-\\lambda x}\\enspace \\left( x\\geqq 0\\right) \\\\ 0 \\enspace \\left( x <0\\right) \\end{cases}$$\nであり、\n$$F(x)=\\int^x_0\\lambda e^{-\\lambda x}dx=1-e^{-\\lambda x}$$\nが成り立つので、瞬間故障率は\n$$\\frac{f(x)}{1-F(x)}=\\lambda$$\nとなります。","metadata":{}},{"cell_type":"markdown","source":"## 生存時間解析の種類\n\n生存時間解析には3つの種類があります:\n\nパラメトリック: 生存時間が特定の確率分布に従う → ワイブル分布、指数分布、対数正規分布など。\n\nセミパラメトリック: コックス回帰。\n\nノンパラメトリック: 生存確率が時間に関連する → カプランマイヤー法など。","metadata":{}},{"cell_type":"markdown","source":"## メディアン生存時間(MST, median survival time)\n\n生存率曲線において，累積生存率が 50% になるときの時間。","metadata":{}},{"cell_type":"markdown","source":"## カプランマイヤー法\nカプランマイヤー法とは、打ち切りのあるデータにおいて生存時間の分布を図示する方法であり、カプランマイヤー法を用いてプロットした生存曲線をカプランマイヤー曲線といいます。 各時点の累積生存率を示しており、対象集団における生存の状況を視覚的に把握することができます。","metadata":{}},{"cell_type":"markdown","source":"カプランマイヤープロットは、第$i$番目の観測時点$t_j (j=1, \\cdots, N)$でのリスク集合のサイズを$n_j$とし、故障数をd_jとしたとき\n\n$$S(t)=\\prod^N_{j=1}\\left( 1-\\frac{d_j}{n_j}\\right)$$\n\nによって求められます。","metadata":{}},{"cell_type":"code","source":"!pip install lifelines","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-12-13T13:08:14.799086Z","iopub.execute_input":"2023-12-13T13:08:14.800006Z","iopub.status.idle":"2023-12-13T13:08:32.206627Z","shell.execute_reply.started":"2023-12-13T13:08:14.799962Z","shell.execute_reply":"2023-12-13T13:08:32.204583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install lifetimes","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-12-13T13:08:32.209061Z","iopub.execute_input":"2023-12-13T13:08:32.209493Z","iopub.status.idle":"2023-12-13T13:08:44.497957Z","shell.execute_reply.started":"2023-12-13T13:08:32.209453Z","shell.execute_reply":"2023-12-13T13:08:44.496944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install japanize-matplotlib\nimport japanize_matplotlib","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-12-13T13:08:44.499211Z","iopub.execute_input":"2023-12-13T13:08:44.499551Z","iopub.status.idle":"2023-12-13T13:08:59.556994Z","shell.execute_reply.started":"2023-12-13T13:08:44.499521Z","shell.execute_reply":"2023-12-13T13:08:59.555714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-12-13T13:08:59.559566Z","iopub.execute_input":"2023-12-13T13:08:59.559918Z","iopub.status.idle":"2023-12-13T13:08:59.977396Z","shell.execute_reply.started":"2023-12-13T13:08:59.559886Z","shell.execute_reply":"2023-12-13T13:08:59.976433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from lifelines import KaplanMeierFitter\n\n# サンプルデータ。（イベントまでの時間。）\ntime_data = [5, 10, 15, 20, 25, 30, 35, 40, 45]\nevent_occurred = [1, 1, 0, 1, 1, 0, 1, 1, 0]\n\n# Kaplan-Meier推定器を作成。\nkmf = KaplanMeierFitter()\n\n# データに推定器を適合させる。\nkmf.fit(durations=time_data, event_observed=event_occurred)\n\n# 生存関数をプロットする。\nkmf.plot_survival_function()\nplt.title('Kaplan-Meier 生存関数')\nplt.xlabel('時間')\nplt.ylabel('生存確率')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-13T13:08:59.978616Z","iopub.execute_input":"2023-12-13T13:08:59.979344Z","iopub.status.idle":"2023-12-13T13:09:00.990676Z","shell.execute_reply.started":"2023-12-13T13:08:59.979307Z","shell.execute_reply":"2023-12-13T13:09:00.989330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Cox比例ハザードモデル\n\nCox比例ハザードモデルは以下の式で表されます。\n\n$$λ(t|X)=λ_0(t)exp(X^Tβ)$$\n \n$λ_0(t)$はベースラインハザード関数、$\\beta$は回帰係数ベクトルです。","metadata":{}},{"cell_type":"code","source":"from lifelines import CoxPHFitter\n\n# サンプルデータの作成。\nnp.random.seed(123)\ndata = pd.DataFrame({\n    'time': np.random.exponential(scale=0.1, size=100),\n    'status': np.random.choice([0, 1], size=100, replace=True),\n    'covariate1': np.random.normal(size=100),\n    'covariate2': np.random.normal(size=100)\n})\n\n# Cox比例ハザードモデルの適合。\ncph = CoxPHFitter()\ncph.fit(data, duration_col='time', event_col='status', formula='covariate1 + covariate2')\n\n# モデルの可視化。\ncph.plot_partial_effects_on_outcome(covariates=['covariate1'], values=[-2, 0, 2], cmap='coolwarm', figsize=(10, 6))\nplt.title('Partial Effects of covariate1 on Survival')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-13T13:09:00.992159Z","iopub.execute_input":"2023-12-13T13:09:00.992533Z","iopub.status.idle":"2023-12-13T13:09:01.404909Z","shell.execute_reply.started":"2023-12-13T13:09:00.992499Z","shell.execute_reply":"2023-12-13T13:09:01.403409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2023-12-13T13:09:01.406973Z","iopub.execute_input":"2023-12-13T13:09:01.407639Z","iopub.status.idle":"2023-12-13T13:09:01.418845Z","shell.execute_reply.started":"2023-12-13T13:09:01.407587Z","shell.execute_reply":"2023-12-13T13:09:01.417669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 引数nrowsを使うと、最初n行だけを読み込むことができる。\ndf1 = pd.read_csv('../input/elo-merchant-category-recommendation/historical_transactions.csv',\n                  nrows = 500,\n                 usecols = ['card_id','purchase_date','purchase_amount'])\ndf1['purchase_date'] = pd.to_datetime(df1['purchase_date'])\ndf1","metadata":{"execution":{"iopub.status.busy":"2023-12-13T13:09:01.420170Z","iopub.execute_input":"2023-12-13T13:09:01.420548Z","iopub.status.idle":"2023-12-13T13:09:01.462868Z","shell.execute_reply.started":"2023-12-13T13:09:01.420518Z","shell.execute_reply":"2023-12-13T13:09:01.461762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from lifetimes.utils import calibration_and_holdout_data\n\nrfm_cal_holdout = calibration_and_holdout_data(transactions=df1,\n                                                customer_id_col='card_id',\n                                             datetime_col='purchase_date',\n                                             monetary_value_col = 'purchase_amount',                                         \n                                             freq='D',\n                                                calibration_period_end='2017-12-31',\n                                               observation_period_end='2018-02-28' )\n\nrfm_cal_holdout.head(3)","metadata":{"execution":{"iopub.status.busy":"2023-12-13T13:09:01.464217Z","iopub.execute_input":"2023-12-13T13:09:01.464579Z","iopub.status.idle":"2023-12-13T13:09:01.564285Z","shell.execute_reply.started":"2023-12-13T13:09:01.464550Z","shell.execute_reply":"2023-12-13T13:09:01.563163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 顧客別CLTV（Individual CLTV）","metadata":{}},{"cell_type":"markdown","source":"「CLTV」は「Customer LifeTime Value」の略です。顧客生涯価値（Life Time Value）とは、顧客と企業とが、ビジネスをする間の時間に生み出された利益の総額（価値）のことです。 ","metadata":{}},{"cell_type":"code","source":"from lifetimes import BetaGeoFitter\n\nbgf = BetaGeoFitter(penalizer_coef = 0.9)\n\nbgf.fit(frequency=rfm_cal_holdout['frequency_cal'], \n        recency=rfm_cal_holdout['recency_cal'], \n        T=rfm_cal_holdout['T_cal'])","metadata":{"execution":{"iopub.status.busy":"2023-12-13T13:09:01.567760Z","iopub.execute_input":"2023-12-13T13:09:01.568329Z","iopub.status.idle":"2023-12-13T13:09:01.736156Z","shell.execute_reply.started":"2023-12-13T13:09:01.568295Z","shell.execute_reply":"2023-12-13T13:09:01.734687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}