{
 "metadata": {
  "name": "",
  "signature": "sha256:41b66ed6dbdebdc3dd934ef963f179576dd8d1497050c4ff934c3909d90dbee3"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 12: Probability"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example Problem 12.1, Page Number 283"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example Problem 12.2, Page Number 284"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from fractions import Fraction\n",
      "\n",
      "#Variable Declaration\n",
      "n = 52          #Total cards\n",
      "nheart = 13     #Number of cards with hearts\n",
      "\n",
      "#Calculations\n",
      "Pe = Fraction(nheart,n)\n",
      "\n",
      "#Results\n",
      "print 'Probability of one (heart)card picked from a std. stack of %d cards is'%n,Pe"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Probability of one (heart)card picked from a std. stack of 52 cards is 1/4\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example Problem 12.3, Page Number 285"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example Problem 12.4, Page Number 285"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#Variable Declaration\n",
      "n1 = 2         #Two spin states for 1st electron in orbit 1\n",
      "n2 = 2         #Two spin states for 2nd electron in orbit 2\n",
      "\n",
      "#Calculation\n",
      "M = n1*n1\n",
      "\n",
      "#Results\n",
      "print 'Possible spin states for excited state are %2d'%M"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Possible spin states for excited state are  4\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example Problem 12.5, Page Number 286"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from math import factorial\n",
      "\n",
      "#Variable Declaration\n",
      "n = 12         #Total Number of players \n",
      "j = 5          #Number player those can play match\n",
      "\n",
      "#Calculation\n",
      "P = factorial(n)/factorial(n-j)\n",
      "\n",
      "#Results\n",
      "print 'Maximum Possible permutations for 5 player to play are %8d'%P"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Maximum Possible permutations for 5 player to play are    95040\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example Problem 12.6, Page Number 287"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from math import factorial\n",
      "\n",
      "#Variable Declaration\n",
      "n = 52         #Number of cards in std . pack\n",
      "j = 5          #Number of cards in subset\n",
      "\n",
      "#Calculation\n",
      "C = factorial(n)/(factorial(j)*factorial(n-j))\n",
      "\n",
      "#Results\n",
      "print 'Maximum Possible 5-card combinations are %8d'%C"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Maximum Possible 5-card combinations are  2598960\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example Problem 12.7, Page Number 288"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from math import factorial\n",
      "\n",
      "#Variable Declaration\n",
      "x = 6          #Number of electrons\n",
      "n = 2          #Number of states\n",
      "\n",
      "#Calculation\n",
      "P = factorial(x)/(factorial(n)*factorial(x-n))\n",
      "\n",
      "#Results\n",
      "print 'Total number of quantum states are %3d'%P"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Total number of quantum states are  15\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example Problem 12.8, Page Number 289"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from math import factorial\n",
      "from fractions import Fraction\n",
      "\n",
      "#Variable Declaration\n",
      "n = 50          #Number of separate experiments\n",
      "j1 = 25          #Number of sucessful expt with heads up\n",
      "j2 = 10          #Number of sucessful expt with heads up\n",
      "\n",
      "#Calculation\n",
      "C25 = factorial(n)/(factorial(j1)*factorial(n-j1))\n",
      "PE25 = Fraction(1,2)**j1\n",
      "PEC25 = (1-Fraction(1,2))**(n-j1)\n",
      "P25 = C25*PE25*PEC25\n",
      "\n",
      "C10 = factorial(n)/(factorial(j2)*factorial(n-j2))\n",
      "PE10 = Fraction(1,2)**j2\n",
      "PEC10 = (1-Fraction(1,2))**(n-j2)\n",
      "P10 = C10*PE10*PEC10\n",
      "\n",
      "#Results\n",
      "print 'Probability of getting 25 head out of 50 tossing is %4.3f'%(float(P25))\n",
      "print 'Probability of getting 10 head out of 50 tossing is %4.3e'%(float(P10))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Probability of getting 25 head out of 50 tossing is 0.112\n",
        "Probability of getting 10 head out of 50 tossing is 9.124e-06\n"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example Problem 12.9, Page Number 290"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from math import factorial, log\n",
      "#Variable Declaration\n",
      "N = [10,50,100]       #Valures for N\n",
      "\n",
      "#Calculations\n",
      "print '  N        ln(N!)       ln(N!)sterling      Error'\n",
      "for i in N:\n",
      "    lnN = log(factorial(i))\n",
      "    lnNs = i*log(i)-i\n",
      "    err = abs(lnN-lnNs)\n",
      "    print '%3d       %5.2f        %5.2f              %4.2f'%(i,lnN,lnNs, err)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "  N        ln(N!)       ln(N!)sterling      Error\n",
        " 10       15.10        13.03              2.08\n",
        " 50       148.48        145.60              2.88\n",
        "100       363.74        360.52              3.22\n"
       ]
      }
     ],
     "prompt_number": 37
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example Problem 12.10, Page Number 293"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from fractions import Fraction\n",
      "\n",
      "#Variable Declaration\n",
      "fi = 1       #Probability of receiving any card\n",
      "n = 52       #Number od Cards\n",
      "\n",
      "#Calculations\n",
      "sum = 0\n",
      "for i in range(52):\n",
      "    sum = sum + fi\n",
      "\n",
      "Pxi = Fraction(fi,sum)\n",
      "\n",
      "#Results\n",
      "print 'Probability of receiving any card', Pxi"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Probability of receiving any card 1/52\n"
       ]
      }
     ],
     "prompt_number": 39
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example Problem 12.11, Page Number 295"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from math import exp\n",
      "from scipy import integrate\n",
      "#Variable Declaration\n",
      "\n",
      "#Calculations\n",
      "fun = lambda x: exp(-0.05*x)\n",
      "Pt = 0\n",
      "for i in range(0,101):\n",
      "    Pt = Pt + fun(i)\n",
      "    \n",
      "Ptot = integrate.quad(fun, 0.0, 100.)\n",
      "\n",
      "#Results\n",
      "print 'Sum of Px considering it as discrete function %4.1f'%Pt\n",
      "print 'Sum of Px considering it as contineous function %4.1f'%Ptot[0]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "(19.865241060018292, 2.205484801456136e-13)\n",
        "Sum of Px considering it as discrete function 20.4\n",
        "Sum of Px considering it as contineous function 19.9\n"
       ]
      }
     ],
     "prompt_number": 47
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example Problem 12.12, Page Number 296"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from sympy import *\n",
      "\n",
      "#Variable Declaration\n",
      "r = Symbol('r')      #Radius of inner circle\n",
      "C = [5,2,0]\n",
      "#Calculations\n",
      "A1 = pi*r**2\n",
      "A2 = pi*(2*r)**2 - A1\n",
      "A3 = pi*(3*r)**2 - (A1 + A2)\n",
      "At = A1 + A2 + A3\n",
      "f1 = A1/At\n",
      "f2 = A2/At\n",
      "f3 = A3/At\n",
      "sf = f1 + f2 + f3\n",
      "\n",
      "ns = (f1*C[0]+f2*C[1]+f3*C[2])/sf\n",
      "\n",
      "#Results\n",
      "print 'A1, A2, A3: ', A1, A2, A3\n",
      "print 'f1, f2, f3: ', f1,f2,f3\n",
      "print 'Average payout $', round(float(ns),2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "A1, A2, A3:  pi*r**2 3*pi*r**2 5*pi*r**2\n",
        "f1, f2, f3:  1/9 1/3 5/9\n",
        "Average payout $ 1.22\n"
       ]
      }
     ],
     "prompt_number": 60
    }
   ],
   "metadata": {}
  }
 ]
}