{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57ececfa-0ae0-4acb-b85d-7c6a6ca8d3db",
   "metadata": {
    "tags": [
     "hide"
    ]
   },
   "outputs": [],
   "source": [
    "import seaborn.objects as so\n",
    "from seaborn import load_dataset\n",
    "diamonds = load_dataset(\"diamonds\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "03c64256-8daf-4b32-87bd-b425e27a7823",
   "metadata": {},
   "source": [
    "The default behavior is to compute the mean and 95% confidence interval (using bootstrapping):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46017dc7-7c3c-4dcf-9232-2e3ac490d980",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "p = so.Plot(diamonds, \"clarity\", \"carat\")\n",
    "p.add(so.Range(), so.Est())"
   ]
  },
  {
   "cell_type": "raw",
   "id": "1bf04e8d-998e-4a47-9375-ddcde76e3914",
   "metadata": {},
   "source": [
    "Other estimators may be selected by name if they are pandas methods:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea394c55-8fa6-4fb0-8665-42c03ef3576e",
   "metadata": {},
   "outputs": [],
   "source": [
    "p.add(so.Range(), so.Est(\"median\"))"
   ]
  },
  {
   "cell_type": "raw",
   "id": "9c5f3c91-fecb-4e75-b045-b30870154083",
   "metadata": {},
   "source": [
    "There are several options for computing the error bar interval, such as (scaled) standard errors:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9c350af5-d549-4cce-b3f2-e9bef33aef36",
   "metadata": {},
   "outputs": [],
   "source": [
    "p.add(so.Range(), so.Est(errorbar=\"se\"))"
   ]
  },
  {
   "cell_type": "raw",
   "id": "8c8d321b-5e73-418c-8c71-4b91cf187e57",
   "metadata": {},
   "source": [
    "The error bars can also represent the spread of the distribution around the estimate using (scaled) standard deviations:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fd2cd9dc-e4c9-4ba1-ac79-38806cf1e009",
   "metadata": {},
   "outputs": [],
   "source": [
    "p.add(so.Range(), so.Est(errorbar=\"sd\"))"
   ]
  },
  {
   "cell_type": "raw",
   "id": "6dba074b-881c-40df-b42e-458e4a26e23d",
   "metadata": {},
   "source": [
    "Because confidence intervals are computed using bootstrapping, there will be small amounts of randomness. Reduce the random variability by increasing the nubmer of bootstrap iterations (although this will be slower), or eliminate it by seeding the random number generator:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d6b450e1-8b1f-411f-aa01-bbb46ab3b6ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "p.add(so.Range(), so.Est(seed=0))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df807ef8-b5fb-4eac-b539-1bd4e797ddc2",
   "metadata": {},
   "source": [
    "To compute a weighted estimate (and confidence interval), assign a `weight` variable in the layer where you use the stat:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e4a0594-e1ee-4f72-971e-3763dd626e8b",
   "metadata": {},
   "outputs": [],
   "source": [
    "p.add(so.Range(), so.Est(), weight=\"price\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d0c34d7-fb76-44cf-9079-3ec7f45741d0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py310",
   "language": "python",
   "name": "py310"
  },
  "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.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
