{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":50160,"databundleVersionId":7602123,"sourceType":"competition"}],"dockerImageVersionId":30646,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### Thanks to @at7459 for an interesting observation in the metric problem that is used in the competition.\n\nIn this notebook, I want to show you clearly how you can use a hack in metrics based on a very large penalty for the slope of the curve **0.88 * min(0, a)**.\n\nBe sure to check out the [original post](https://www.kaggle.com/competitions/home-credit-credit-risk-model-stability/discussion/476449), and upvote it if this idea was useful to you!","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2024-02-12T15:33:44.082164Z","iopub.execute_input":"2024-02-12T15:33:44.082579Z","iopub.status.idle":"2024-02-12T15:33:44.584372Z","shell.execute_reply.started":"2024-02-12T15:33:44.082545Z","shell.execute_reply":"2024-02-12T15:33:44.581456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def score_plot(week_nums, ginis):\n    a, b = np.polyfit(week_nums, ginis, 1)\n    ginis_hat = pd.Series(a*week_nums + b, index=week_nums)\n    df = pd.concat([ginis, ginis_hat], axis=1)\n    df.columns = [\"ginis\", \"ginis_hat\"]\n\n    metric_value = np.mean(ginis) + 0.88*min(0, a) - np.std(ginis - ginis_hat)\n\n    df.plot(\n        title=f\"score={metric_value:.3f}, mean_gini={np.mean(ginis):.3f}\",\n        xlabel=\"week_num\",\n        ylabel=\"gini\",\n    )","metadata":{"execution":{"iopub.status.busy":"2024-02-12T15:33:44.586600Z","iopub.execute_input":"2024-02-12T15:33:44.587308Z","iopub.status.idle":"2024-02-12T15:33:44.597699Z","shell.execute_reply.started":"2024-02-12T15:33:44.587268Z","shell.execute_reply":"2024-02-12T15:33:44.596167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Let's create synthetic values of the gini metric, which drops every week for 10 weeks.","metadata":{}},{"cell_type":"code","source":"ginis_values = [0.45, 0.42, 0.40, 0.39, 0.385, 0.3825, 0.3812, 0.3806, 0.3803, 0.3801]\nweek_nums = np.arange(92, 92 + len(ginis_values))\nginis = pd.Series(ginis_values, index=week_nums)","metadata":{"execution":{"iopub.status.busy":"2024-02-12T15:33:44.601490Z","iopub.execute_input":"2024-02-12T15:33:44.601998Z","iopub.status.idle":"2024-02-12T15:33:44.613182Z","shell.execute_reply.started":"2024-02-12T15:33:44.601958Z","shell.execute_reply":"2024-02-12T15:33:44.611149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score_plot(week_nums, ginis)","metadata":{"execution":{"iopub.status.busy":"2024-02-12T15:33:44.617139Z","iopub.execute_input":"2024-02-12T15:33:44.617818Z","iopub.status.idle":"2024-02-12T15:33:45.013605Z","shell.execute_reply.started":"2024-02-12T15:33:44.617787Z","shell.execute_reply":"2024-02-12T15:33:45.012479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Let's reduce the first three points of the synthetic values of the gini metric","metadata":{}},{"cell_type":"code","source":"ginis_worse = ginis.copy()\n\nginis_worse.loc[92] -= 0.06\nginis_worse.loc[93] -= 0.02\nginis_worse.loc[94] -= 0.005\n\nscore_plot(week_nums, ginis_worse)","metadata":{"execution":{"iopub.status.busy":"2024-02-12T15:33:45.015350Z","iopub.execute_input":"2024-02-12T15:33:45.015781Z","iopub.status.idle":"2024-02-12T15:33:45.348532Z","shell.execute_reply.started":"2024-02-12T15:33:45.015742Z","shell.execute_reply":"2024-02-12T15:33:45.347141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### As you can see, despite the fact that the average value of the metric has become worse, the final metric has begun to take on greater values\n\n\n| **Metric** | **Score Before** | **Score After** |\n| --- | --- | --- |\n| **Casual Gini** | **0.395** | 0.386 |\n| **Competition Gini** | 0.377 | **0.381** |","metadata":{}}]}