ó
[™xTc           @   s�   d  d l  m Z d  d l Z d  d l Z d  d l m Z d  d l Z d d
 d „  ƒ  YZ d e	 f d „  ƒ  YZ
 d „  Z e d	 k rŒ e ƒ  n  d S(   iÿÿÿÿ(   t   divisionN(   t   flext   polynomial_fitc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s¬   | j  ƒ  | j  ƒ  k s t ‚ | | j  ƒ  k  s6 t ‚ | |  _ | |  _ | | d d |  _ | |  _ t j |  j d ƒ |  _ t	 j
 j d |  ƒ |  _ |  j |  _ |  ` d  S(   Ni    g      ð?t   target_evaluator(   t   sizet   AssertionErrort   x_obst   y_obst   w_obst   nR   t   doublet   xt   scitbxt   lbfgst   runt	   minimizert   a(   t   selfR   R   R   R	   (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/examples/bootstrap.pyt   __init__W   s    			c         C   sü   |  j  |  _ t j |  j j ƒ  d ƒ } x; t |  j ƒ D]* } | |  j | t j |  j | ƒ } q7 W|  j	 | } t j
 | | |  j ƒ } t j |  j d ƒ } xI t |  j ƒ D]8 } t j
 d | |  j t j |  j | ƒ ƒ | | <q± W| GH| | f S(   Ni    g       @(   R   R   R   R
   R   R   t   rangeR	   t   powR   t   sumR   (   R   t   y_calct   it   y_difft   ft   g(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/examples/bootstrap.pyt    compute_functional_and_gradientsc   s    (6(   t   __name__t
   __module__R   R   (    (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/examples/bootstrap.pyR   V   s   	t	   fake_datac           B   s   e  Z d  „  Z d „  Z RS(   c         C   sO   | |  _  | |  _ t j t | j ƒ  ƒ ƒ |  _ t j j	 |  j d ƒ |  _
 d  S(   Ni    (   t   x_datat   y_dataR   R
   R   R   t   permutation_referenceR   t   matht   non_parametric_bootstrapt   non_para_bootstrap(   R   R   R    (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/examples/bootstrap.pyR   r   s
    			c         C   sˆ   |  j  j | ƒ } t j ƒ  } x! | D] } | j t | ƒ ƒ q% Wt j t j |  j | ƒ ƒ } t j t j |  j | ƒ ƒ } | | f S(   N(	   R$   t   drawR   t   intt   appendR
   t   selectR   R    (   R   R   t   selection_sett   iselt   elementt   new_xt   new_y(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/examples/bootstrap.pyt   fake_it€   s    (   R   R   R   R.   (    (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/examples/bootstrap.pyR   q   s   	c       
   C   st  t  j t d ƒ ƒ }  t  j d d d g ƒ } t  j d d ƒ } | d | d |  | d |  |  } | t  j d |  j ƒ  ƒ d } x% t d ƒ D] } |  | G| | GHq’ Wt |  | ƒ } t d	 |  d
 | d | d d ƒ } d GHd GHd GHx2 t | j ƒ  ƒ D] } | G| | G| j | GHqÿ Wd GHd GHd GHd d d g }	 d d d g }
 x  t d ƒ D]’ } | j d ƒ \ } } t d	 | d
 | d | d d ƒ } xS t | j ƒ  ƒ D]? } |	 | c | j | 7<|
 | c | j | | j | 7<qªWq[Wx| t d ƒ D]n } |	 | c d <|
 | c d <|
 | c |	 | |	 | 8<t	 j
 |
 | ƒ |
 | <| G|	 | G|
 | GHqþWd  S(   Ni   i   i   i   g      Y@i    R   g      ø?R   R   R   R	   s,   ------------------------------------------- s"          True and fitted coeffcientss*    Bootstrapped mean and standard deviationsid   (   R   R
   R   t   random_doubleR   R   R   R   R.   R"   t   sqrt(   R   R   R   t   y_idealR   t   iit   fakert   fitR   t   meant   stdt   trialt   x_newt   y_new(    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/examples/bootstrap.pyt   exampleŒ   s>    & !!*t   __main__(    (   t
   __future__R    t   scitbx.lbfgsR   t   scitbx.matht   scitbx.array_familyR   R"   R   t   objectR   R:   R   (    (    (    sG   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/examples/bootstrap.pyt   <module>   s   Q	(