mò
	ÏVFc           @   sZ   d  k  Z  d  k Z d k Td e f d „  ƒ  YZ d „  Z e d j o e e  i Œ  n d  S(   N(   t   *t   Histc           B   sh   t  Z d  Z g  d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 e
 d „ Z e
 d	 „ Z RS(
   s{   a histogram is a dictionary that maps from each key (x) to the
    number of times the key has appeared (frequency, f)
    c         C   s"   x | D] } |  i | ƒ q Wd S(   s5   create a new histogram starting with the items in seqN(   t   seqt   xt   selft   count(   R   R   R   (    (    t   /home/downey/cm/code/Hist.pyt   __init__   s      c         C   s   |  i | d ƒ d |  | <d S(   s+   increment the counter associated with key xi    i   N(   R   t   getR   (   R   R   (    (    R   R      s     c         C   sn   |  i ƒ  } | i ƒ  g  } | D] \ } } | | q! ~ } g  } | D] \ } } | | qH ~ } | | f S(   sb   return a sorted list of keys (xs) and the corresponding
        list of frequencies (fs).
        N(	   R   t   itemst   tt   sortt   _[1]R   t   ft   xst   fs(   R   R   R   R   R
   R   R   (    (    R   t   pdf   s     
''c         C   s?   |  i ƒ  \ } } x& t | | ƒ D] \ } } | G| GHq" Wd S(   s5   print the sorted keys and the corresponding fractionsN(   R   R   R   R   t   zipR   R   (   R   R   R   R   R   (    (    R   t	   print_pdf   s
      c         O   sP   |  i ƒ  \ } } t | | | | Ž } t d ƒ t	 d ƒ t
 d ƒ t ƒ  d S(   sY   plot the pdf as a bar chart.  args and kwds are passed
        along to the bar function.t   keyt	   frequencyR   N(   R   R   t   leftt   heightt   bart   argst   kwdst   patchest   xlabelt   ylabelt   titlet   show(   R   R   R   R   R   R   (    (    R   t   plot_pdf%   s     


c   	         s‘   t  |  i ƒ  ƒ ‰ d } |  i ƒ  } | i ƒ  g  ‰ g  ‰  ‡ ‡ ‡  d †  } x8 | D]0 \ } } | | | ƒ | | 7} | | | ƒ qS Wˆ ˆ  f S(   st  return a sorted list of keys (xs) and the corresponding
        list of cumulative fractions (the fraction of keys less
        than or equal to x).  This is the empirical CDF of the keys
        in the Hist.

        Note: the cdf makes more sense if the keys in the Hist
        are numeric, but this function works for any data type that
        can be sorted.
        i    c            s.   t  | ƒ ˆ } ˆ  i |  ƒ ˆ i | ƒ d S(   s7   compute the cumulative fraction and append to the listsN(   t   floatt   runsumt   totalt   cfR   t   appendR   t   cfs(   R   R!   R#   (   R   R"   R%   (    R   R$   B   s     N(   t   sumR   t   valuesR"   R!   R	   R
   R   R   R%   R$   R   R   (	   R   R   R$   R%   R
   R   R   R"   R!   (    (   R%   R   R"   R   t   cdf/   s    	 
 
c         C   s?   |  i ƒ  \ } } x& t | | ƒ D] \ } } | G| GHq" Wd S(   sW   print the keys (in increasing order) and the corresponding
        cumulative fractionsN(   R   R(   R   R   R   R   R   (   R   R   R   R   R   (    (    R   t	   print_cdfR   s
      c         O   sP   |  i ƒ  \ } } | | | | | Ž } t d ƒ t	 d ƒ t
 d ƒ t ƒ  d S(   só   plot the cdf as a step function; the optional second
        parameter is one of the line plotting functions in pylab,
        either plot, semilogx, semilogy or loglog.

        args and kwds are passed along to the plotting function
        R   s   cumulative fractionR(   N(   R   R(   R   R   t	   plot_funcR   R   R   R   R   R   R   (   R   R*   R   R   R   R   R   (    (    R   t   plot_cdfY   s     


c   	      O   su   |  i ƒ  \ } } g  } | D] } | d | q ~ } | | | | | Ž } t
 d ƒ t d ƒ t d ƒ t ƒ  d  S(   Ni   R   s!   complementary cumulative fractiont   ccdf(   R   R(   R   R   R   R   R*   R   R   R   R   R   R   R   (	   R   R*   R   R   R   R   R   R   R   (    (    R   t	   plot_ccdfg   s    %


(   t   __name__t
   __module__t   __doc__R   R   R   R   R   R(   R)   t   plotR+   R-   (    (    (    R   R      s    				
	#	c         C   sw   d d „ } t ƒ  } x- t d ƒ D] } | d ƒ } | i | ƒ q" W| i ƒ  | i ƒ  | i	 ƒ  | i
 d d ƒ d  S(   Ni   c         C   s=   g  } t |  ƒ D] } | t i d | ƒ q ~ } t | ƒ S(   s?   roll (dice) dice with (sides) sides and return the sum
        i   N(	   R   t   ranget   diceR   t   randomt   randintt   sidesR
   R&   (   R3   R6   R   R
   R   (    (    R   t	   roll_diceq   s     3id   i   t	   linewidthi   (   R7   R   t   hR2   t   it   sR   R   R   R)   R+   (   t   scriptR:   R9   R7   R;   (    (    R   t   mainp   s    	 


t   __main__(   t   syst   stringt   pylabt   dictR   R=   R.   t   argv(   R   R@   R?   R=   (    (    R   t   ?   s   		j	