{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "#"
      ],
      "id": "934b7bc2-567c-4126-9d69-7e328408b312"
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np"
      ],
      "id": "9597e5f0-e826-446c-95dc-8e819a180b32"
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {},
      "outputs": [],
      "source": [
        "x = np.array([-1, 3, 7, 10, 12, 27.]) # values in sample space\n",
        "fx = np.array([2, 1, 1, 2, 3, 1]) / 10 # density at those values"
      ],
      "id": "793ca86e-22f4-410d-b38a-094914a08cbf"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Calculate\n",
        "\n",
        "$$\\mathbb{E}[X] = \\sum_{x \\in S} x f(x)$$"
      ],
      "id": "f7afff41-6da1-4a3c-8f8b-2ff9f5a09b1d"
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "9.1"
            ]
          }
        }
      ],
      "source": [
        "m = np.sum(x * fx)\n",
        "m"
      ],
      "id": "fa721759-4a3e-4309-8439-d9a5da087e42"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Calculate\n",
        "\n",
        "$$\\mathbb{V}[X] = \\mathbb{E}[(X - \\mathbb{E}[X])^2] = \\sum_{x \\in S} (x - m)^2 f(x)$$"
      ],
      "id": "ab9096a0-02ac-4729-8f2d-fe25f295897b"
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "59.28999999999999"
            ]
          }
        }
      ],
      "source": [
        "np.sum((x - m) ** 2 * fx)"
      ],
      "id": "fd7edd43-45b0-4313-9ba4-a76b0409bd13"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Calculate\n",
        "\n",
        "$$\\mathbb{P}[X > m] = \\sum_{x \\in S} 1_{\\{10, 12, 27\\}}(x) f(x)$$"
      ],
      "id": "2eb6ca21-e52b-4282-a4ff-a3b75a6ecf11"
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "0.6"
            ]
          }
        }
      ],
      "source": [
        "ind = x > m\n",
        "np.sum(fx[ind])"
      ],
      "id": "54cac47e-21fd-4686-8089-37b23f3d3703"
    }
  ],
  "nbformat": 4,
  "nbformat_minor": 5,
  "metadata": {
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3 (ipykernel)",
      "language": "python"
    },
    "language_info": {
      "name": "python",
      "codemirror_mode": {
        "name": "ipython",
        "version": "3"
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.12"
    }
  }
}