{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "456e11ff",
   "metadata": {},
   "source": [
    "# ① 재정패널 소득분위별 가정용 주류지출 — 역진성 패널\n",
    "**주세 정책이 경제·민생에 미치는 영향 연구** · 재정패널(NaSTaB) 1~17차(2008~2024) 가구데이터\n",
    "\n",
    "가정용 주류비 = `cd002`(맥주·소주·양주) + `cd004`(와인·막걸리·전통주), 월평균 만원.\n",
    "가구 횡단가중치 `h{차}cwt` 적용. 결측(-9 등) 제외, 미구입=0.\n",
    "\n",
    "> 원자료는 KIPF 신청·다운로드분이며 공개 대상이 아니다. 본 노트북은 재현 절차 문서용이다."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d147f2ed",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-08T19:06:34.897034Z",
     "iopub.status.busy": "2026-07-08T19:06:34.897034Z",
     "iopub.status.idle": "2026-07-08T19:06:41.899802Z",
     "shell.execute_reply": "2026-07-08T19:06:41.897827Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd, numpy as np, csv, os\n",
    "D=r\"D:\\Project\\2026_2\\주류세\\data\\nastab\"; OUT=r\"D:\\Project\\2026_2\\주류세\\data\"\n",
    "MISS={-1,-9,-8,-99,-999,999999}\n",
    "def wcols(ww):\n",
    "    with open(os.path.join(D,f\"NaSTaB{ww}H.csv\"),encoding=\"utf-8-sig\") as f: return set(next(csv.reader(f)))\n",
    "def weighted_decile(vals,wts):\n",
    "    d=pd.DataFrame({\"v\":pd.to_numeric(vals,errors=\"coerce\"),\"w\":pd.to_numeric(wts,errors=\"coerce\")},index=vals.index)\n",
    "    d=d[d[\"v\"].notna()&d[\"w\"].notna()&(d[\"w\"]>0)].sort_values(\"v\")\n",
    "    cw=d[\"w\"].cumsum(); frac=(cw-d[\"w\"]*0.5)/d[\"w\"].sum()\n",
    "    d[\"dec\"]=np.clip(np.ceil(frac*10),1,10).astype(int)\n",
    "    return d[\"dec\"].reindex(vals.index)\n",
    "def wmean(x,w):\n",
    "    m=x.notna()&w.notna()\n",
    "    return np.average(x[m],weights=w[m]) if (m.sum() and w[m].sum()) else np.nan"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c8a8d0f",
   "metadata": {},
   "source": [
    "## 17개 차수 순회 — 소득분위별 비중 산출"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "49d5d2c5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-08T19:06:41.904608Z",
     "iopub.status.busy": "2026-07-08T19:06:41.903202Z",
     "iopub.status.idle": "2026-07-08T19:06:46.621515Z",
     "shell.execute_reply": "2026-07-08T19:06:46.620460Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "완료: 17 개 차수\n"
     ]
    }
   ],
   "source": [
    "rows=[]\n",
    "for w in range(1,18):\n",
    "    ww=f\"{w:02d}\"; have=wcols(ww)\n",
    "    inc=f\"h{ww}ca004\" if f\"h{ww}ca004\" in have else None\n",
    "    con=f\"h{ww}ca002\"; cd2=f\"h{ww}cd002\"; cd4=f\"h{ww}cd004\"\n",
    "    wt=f\"h{ww}cwt\" if f\"h{ww}cwt\" in have else None\n",
    "    use=[c for c in [inc,con,cd2,cd4,wt] if c]\n",
    "    df=pd.read_csv(os.path.join(D,f\"NaSTaB{ww}H.csv\"),usecols=use,encoding=\"utf-8-sig\")\n",
    "    for c in use: df[c]=pd.to_numeric(df[c],errors=\"coerce\")\n",
    "    if inc: df[inc]=df[inc].where(~df[inc].isin(MISS)).where(df[inc]>0)\n",
    "    df[con]=df[con].where(~df[con].isin(MISS)).where(df[con]>0)\n",
    "    for c in [cd2,cd4]: df[c]=df[c].where(~df[c].isin(MISS))\n",
    "    W=df[wt] if wt else pd.Series(1.0,index=df.index)\n",
    "    alc=df[cd2].fillna(0)+df[cd4].fillna(0)\n",
    "    rec={\"wave\":w,\"survey_year\":2007+w,\"n\":len(df),\n",
    "         \"alc_share_con_pct_all\":round(wmean(alc,W)/wmean(df[con],W)*100,3),\n",
    "         \"purchase_rate_pct\":round(wmean((alc>0).astype(float),W)*100,1)}\n",
    "    if inc:\n",
    "        sub=df[df[inc].notna()].copy(); sub[\"dec\"]=weighted_decile(sub[inc],W[sub.index])\n",
    "        for d in range(1,11):\n",
    "            g=sub[sub[\"dec\"]==d]; a=wmean(alc[g.index],W[g.index]); c=wmean(g[con],W[g.index])\n",
    "            rec[f\"D{d:02d}_alc_con_pct\"]=round(a/c*100,3) if c else np.nan\n",
    "    rows.append(rec)\n",
    "res=pd.DataFrame(rows); res.to_csv(os.path.join(OUT,\"nastab_alcohol_panel.csv\"),index=False,encoding=\"utf-8-sig\")\n",
    "print(\"완료:\",len(res),\"개 차수\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e04ece94",
   "metadata": {},
   "source": [
    "## 결과 1 — 17차(2024) 소득분위별 비중: **역U자**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8344a32f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-08T19:06:46.625757Z",
     "iopub.status.busy": "2026-07-08T19:06:46.624751Z",
     "iopub.status.idle": "2026-07-08T19:06:46.652814Z",
     "shell.execute_reply": "2026-07-08T19:06:46.651266Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>분위</th>\n",
       "      <th>주류/소비(%)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>D01</td>\n",
       "      <td>0.247</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>D02</td>\n",
       "      <td>0.474</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>D03</td>\n",
       "      <td>0.652</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>D04</td>\n",
       "      <td>0.747</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>D05</td>\n",
       "      <td>0.770</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>D06</td>\n",
       "      <td>0.692</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>D07</td>\n",
       "      <td>0.901</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>D08</td>\n",
       "      <td>0.897</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>D09</td>\n",
       "      <td>0.787</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>D10</td>\n",
       "      <td>0.745</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    분위  주류/소비(%)\n",
       "0  D01     0.247\n",
       "1  D02     0.474\n",
       "2  D03     0.652\n",
       "3  D04     0.747\n",
       "4  D05     0.770\n",
       "5  D06     0.692\n",
       "6  D07     0.901\n",
       "7  D08     0.897\n",
       "8  D09     0.787\n",
       "9  D10     0.745"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r=res[res.wave==17].iloc[0]\n",
    "pd.DataFrame({\"분위\":[f\"D{d:02d}\" for d in range(1,11)],\n",
    "             \"주류/소비(%)\":[r[f\"D{d:02d}_alc_con_pct\"] for d in range(1,11)]})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f795a10",
   "metadata": {},
   "source": [
    "D01(최저) 0.25% → D07 0.90%(정점) → D10(최고) 0.75% — **중산층 최다부담·최고소득 상단 역진**. 절대액은 소득따라 증가하나 비중은 역U자."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09ec94a2",
   "metadata": {},
   "source": [
    "## 결과 2 — 구조적 감소(17년 반감)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "41146f3a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-07-08T19:06:46.658192Z",
     "iopub.status.busy": "2026-07-08T19:06:46.657195Z",
     "iopub.status.idle": "2026-07-08T19:06:46.677184Z",
     "shell.execute_reply": "2026-07-08T19:06:46.675864Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>survey_year</th>\n",
       "      <th>alc_share_con_pct_all</th>\n",
       "      <th>purchase_rate_pct</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2008</td>\n",
       "      <td>1.536</td>\n",
       "      <td>49.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2009</td>\n",
       "      <td>1.093</td>\n",
       "      <td>46.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2010</td>\n",
       "      <td>1.083</td>\n",
       "      <td>45.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2011</td>\n",
       "      <td>0.989</td>\n",
       "      <td>42.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2012</td>\n",
       "      <td>1.024</td>\n",
       "      <td>41.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2013</td>\n",
       "      <td>0.806</td>\n",
       "      <td>37.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2014</td>\n",
       "      <td>0.917</td>\n",
       "      <td>38.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2015</td>\n",
       "      <td>0.841</td>\n",
       "      <td>37.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2016</td>\n",
       "      <td>0.755</td>\n",
       "      <td>36.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>2017</td>\n",
       "      <td>0.817</td>\n",
       "      <td>38.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>2018</td>\n",
       "      <td>0.892</td>\n",
       "      <td>39.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>2019</td>\n",
       "      <td>0.898</td>\n",
       "      <td>41.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>2020</td>\n",
       "      <td>0.959</td>\n",
       "      <td>42.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>2021</td>\n",
       "      <td>0.863</td>\n",
       "      <td>38.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>2022</td>\n",
       "      <td>0.877</td>\n",
       "      <td>38.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>2023</td>\n",
       "      <td>0.879</td>\n",
       "      <td>38.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>2024</td>\n",
       "      <td>0.754</td>\n",
       "      <td>37.4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    survey_year  alc_share_con_pct_all  purchase_rate_pct\n",
       "0          2008                  1.536               49.9\n",
       "1          2009                  1.093               46.7\n",
       "2          2010                  1.083               45.5\n",
       "3          2011                  0.989               42.4\n",
       "4          2012                  1.024               41.0\n",
       "5          2013                  0.806               37.1\n",
       "6          2014                  0.917               38.4\n",
       "7          2015                  0.841               37.4\n",
       "8          2016                  0.755               36.6\n",
       "9          2017                  0.817               38.2\n",
       "10         2018                  0.892               39.5\n",
       "11         2019                  0.898               41.1\n",
       "12         2020                  0.959               42.4\n",
       "13         2021                  0.863               38.4\n",
       "14         2022                  0.877               38.9\n",
       "15         2023                  0.879               38.6\n",
       "16         2024                  0.754               37.4"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res[[\"survey_year\",\"alc_share_con_pct_all\",\"purchase_rate_pct\"]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05b73937",
   "metadata": {},
   "source": [
    "가정용 주류/소비 비중 2008년 1.54% → 2024년 0.75%로 **반감**, 구입가구율 50%→37%. 구조적 수요 감소를 가구패널로 확인."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "name": "py3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
