{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Uma Simulação Simples e o Problema de Monty Hall\n", "### Nelson Kuhl - IME/USP" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np, matplotlib.pyplot as plt # o mínimo\n", "import scipy.stats as st # Há várias ferramentas estatísticas no Python. O módulo stats do scipy integra bem com o\n", " # numpy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exemplo de Simulação com Inteiros\n", "O objeto *randint* do módulo scipy.stats é usado para a distribuição uniforme discreta. Aqui usaremos apenas o *método* rvs, que sorteia valores identicamente distribuidos entre um conjunto discreto de pontos: dados naturais $i$ e $j$ com $i < j-1$ e um inteiro positivo $n$, o comando
\n", "$$\n", "scipy.stats.randint.rvs(i, j, size=n)\n", "$$\n", "gera $n$ números distribuidos uniformemente no conjunto $\\{i, i+1,\\dots,j-1\\}$. Faremos um teste simples para verificar a distribuição uniforme." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.321 0.336 0.343]\n" ] } ], "source": [ "n = 1000 # Tamanho da amostra\n", "rs = st.randint.rvs(1, 4, size=n) # n sorteios de números uniformemente distribuidos no conjunto {1, 2, 3}\n", "y = np.array([sum(rs==1), sum(rs==2), sum(rs==3)])/n # array contendo a frequência dos resultados de cada valor\n", "print(y)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAXLklEQVR4nO3dfXBU9b3H8fcXCSI+QEuwOgQI9mp5BiWg4pTiExOwIjNSSyZQY9VY8aGO3nuLYkEpaFHb6+BVkT7ZanxAtB1E0BYrY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