{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Estimate a distribution by maximum likelihood"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this example we are going to estimate the parameters of a parametric by generic numerical optimization of the likelihood."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import openturns as ot\n",
    "import math as m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create data from a gaussian pdf with mu=4, sigma=1.5\n",
    "sample = ot.Normal(4.0, 1.5).getSample(200)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create the search interval of (mu, sigma): the constraint is sigma>0\n",
    "lowerBound = [-1.0, 1.0e-4]\n",
    "upperBound = [-1.0, -1.0]\n",
    "finiteLowerBound = [False, True]\n",
    "finiteUpperBound = [False, False]\n",
    "bounds = ot.Interval(lowerBound, upperBound, finiteLowerBound, finiteUpperBound)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4.912302476828147, 2.811562130153132]\n"
     ]
    }
   ],
   "source": [
    "# Create the starting point of the research\n",
    "# For mu : the first point\n",
    "# For sigma : a value evaluated from the two first data\n",
    "mu0 = sample[0][0]\n",
    "sigma0 = m.sqrt((sample[1][0] - sample[0][0]) * (sample[1][0] - sample[0][0]))\n",
    "startingPoint = [mu0, sigma0]\n",
    "print(startingPoint)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Create the estimator from a parametric pdf\n",
    "pdf = ot.Normal()\n",
    "factory = ot.MaximumLikelihoodFactory(pdf)\n",
    "factory.setOptimizationBounds(bounds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Set the starting point via the solver\n",
    "solver = factory.getOptimizationAlgorithm()\n",
    "solver.setStartingPoint(startingPoint)\n",
    "factory.setOptimizationAlgorithm(solver)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Normal(mu = 3.94055, sigma = 1.48893)\n"
     ]
    }
   ],
   "source": [
    "# Estimate the parametric model\n",
    "distribution = factory.build(sample)\n",
    "print(distribution)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "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.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
