{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "gpuType": "T4" }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" }, "accelerator": "GPU" }, "cells": [ { "cell_type": "markdown", "source": [ "# WTP space models" ], "metadata": { "id": "NaIEHmukcv9b" } }, { "cell_type": "markdown", "source": [ "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/arteagac/xlogit/blob/master/examples/wtp_space.ipynb)" ], "metadata": { "id": "vtaWwgdMen-3" } }, { "cell_type": "markdown", "source": [ "## Import xlogit" ], "metadata": { "id": "w-W6OfpsRak0" } }, { "cell_type": "markdown", "source": [ "The cell below installs and imports xlogit and check if GPU is available. A GPU is not is not strictly required, but it can speed up computations for models in WTP space with random parameters." ], "metadata": { "id": "YueGuKfoSCM8" } }, { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "MjMa8cK_n9N3", "outputId": "343e0a99-2720-4511-9c9e-19f4b0d3d899" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Requirement already satisfied: xlogit in /usr/local/lib/python3.10/dist-packages (0.2.7)\n", "Requirement already satisfied: numpy>=1.13.1 in /usr/local/lib/python3.10/dist-packages (from xlogit) (1.23.5)\n", "Requirement already satisfied: scipy>=1.0.0 in /usr/local/lib/python3.10/dist-packages (from xlogit) (1.11.4)\n", "1 GPU device(s) available. xlogit will use GPU processing\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 1 } ], "source": [ "!pip install xlogit\n", "from xlogit import MixedLogit\n", "MixedLogit.check_if_gpu_available()" ] }, { "cell_type": "markdown", "source": [ "## Yougurt Dataset" ], "metadata": { "id": "F8qGYPH7SfIP" } }, { "cell_type": "markdown", "source": [ "This dataset comprises revealed preferences data involving 2,412 choices among three yogurt brands. Following a panel structure, it includes multiple choice situations observed across 100 households. Due to variations in the number of choice situations experienced by each household, the panels are imbalanced, a characteristic that xlogit is capable of handling. Originally introduced by Jain et al. (1994), this dataset was ported from the [logitr package for R](https://github.com/jhelvy/logitr/)." ], "metadata": { "id": "88neKwKWShmD" } }, { "cell_type": "markdown", "source": [ "### Read data" ], "metadata": { "id": "b007lcxSWWL2" } }, { "cell_type": "code", "source": [ "import pandas as pd\n", "import numpy as np\n", "df = pd.read_csv(\"https://raw.githubusercontent.com/arteagac/xlogit/master/examples/data/yogurt_long.csv\")\n", "df" ], "metadata": { "id": "J-dXIf9mpA0B", "colab": { "base_uri": "https://localhost:8080/", "height": 424 }, "outputId": "9c8f1603-1dcc-4f53-885f-8d049ca3e665" }, "execution_count": 2, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " id choice feat price chid alt\n", "0 1 0 0 8.1 1 dannon\n", "1 1 0 0 6.1 1 hiland\n", "2 1 1 0 7.9 1 weight\n", "3 1 0 0 10.8 1 yoplait\n", "4 1 1 0 9.8 2 dannon\n", "... ... ... ... ... ... ...\n", "9643 100 0 0 12.2 2411 yoplait\n", "9644 100 0 0 8.6 2412 dannon\n", "9645 100 0 0 4.3 2412 hiland\n", "9646 100 1 0 7.9 2412 weight\n", "9647 100 0 0 10.8 2412 yoplait\n", "\n", "[9648 rows x 6 columns]" ], "text/html": [ "\n", "
\n", " | id | \n", "choice | \n", "feat | \n", "price | \n", "chid | \n", "alt | \n", "
---|---|---|---|---|---|---|
0 | \n", "1 | \n", "0 | \n", "0 | \n", "8.1 | \n", "1 | \n", "dannon | \n", "
1 | \n", "1 | \n", "0 | \n", "0 | \n", "6.1 | \n", "1 | \n", "hiland | \n", "
2 | \n", "1 | \n", "1 | \n", "0 | \n", "7.9 | \n", "1 | \n", "weight | \n", "
3 | \n", "1 | \n", "0 | \n", "0 | \n", "10.8 | \n", "1 | \n", "yoplait | \n", "
4 | \n", "1 | \n", "1 | \n", "0 | \n", "9.8 | \n", "2 | \n", "dannon | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
9643 | \n", "100 | \n", "0 | \n", "0 | \n", "12.2 | \n", "2411 | \n", "yoplait | \n", "
9644 | \n", "100 | \n", "0 | \n", "0 | \n", "8.6 | \n", "2412 | \n", "dannon | \n", "
9645 | \n", "100 | \n", "0 | \n", "0 | \n", "4.3 | \n", "2412 | \n", "hiland | \n", "
9646 | \n", "100 | \n", "1 | \n", "0 | \n", "7.9 | \n", "2412 | \n", "weight | \n", "
9647 | \n", "100 | \n", "0 | \n", "0 | \n", "10.8 | \n", "2412 | \n", "yoplait | \n", "
9648 rows × 6 columns
\n", "