B
    d                 @   s   d dl Zd dl mZmZmZmZmZmZmZm	Z	m
Z
mZ d dlmZ d dlmZ dd Zdd Zd	d
 ZdddZdddZdddZdddZdddZdS )    N)
log1plog	logaddexpdigitizezerosexpconcatenateallcumsumones)uniform)reducec             C   s:   t | |ks$tdt| t|f | tt||    S )Nz*cannot take log of negative number %s - %s)r	   AssertionErrorstrr   r   )xy r   b/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/lalinference/nest2pos.py	logsubexp   s    $r   c             C   sJ   t | dd | dd td }t|dd |dd }t || S )zT
    Trapezoidal integration of given log(func)
    Returns log of the integral
    N      )r   r   r   r   )Zlog_funcZlog_supportZlog_func_sumZlog_dxsr   r   r   log_integrate_log_trap   s    "r   c             C   s>  t tdg| d|  f}| | d }t| jd }ttt|d td|  td|  }tt|}tj	|d< |d td|  |d< x:t
t|d D ]&}|| td||   ||d < qW t |||d gf}t ||f}	t||	}
t|	dd |	dd }|dd |dd  }||
8 }|
|fS )zReturns log_ev, log_wts for the log-likelihood samples in data,
    assumed to be a result of nested sampling with Nlive live points.z-infNr   r   g      r   )r   floatr   shaper
   r   lenr   npZNINFranger   r   )dataNliveZ
start_dataend_datalog_wtsZlog_vols_startZlog_vols_endiZ	log_likesZlog_volslog_evZlog_dXsr   r   r   compute_weights   s     ,
&
r$   Fc                sD   t |}|| }dd |D  tt fddtt }| | S )zDraw points from the given data (of shape (Nsamples, Ndim))
    with associated log(weight) (of shape (Nsamples,)). Draws uniquely so
    there are no repeated samplesc             S   s   g | ]}|t t kqS r   )r   r   ).0nr   r   r   
<listcomp>:   s    z"draw_posterior.<locals>.<listcomp>c                s    |  S )Nr   )r"   )	selectionr   r   <lambda>;       z draw_posterior.<locals>.<lambda>)maxlistfilterr   r   )r   r!   verboseZmaxWtZnormalised_wtsidxr   )r(   r   draw_posterior4   s
    r0   c                sx  ddl }tdd t|D  \}}|r8tdt|  tt|}t|fdd|D |rptdt  fddttD }t|  fd	d|D |rtd
t  dd t||D }|rtdtdd |D   |rtdtdd t|D   g }	x8t|D ]*\}
}t	t|
d|k }|	
|
|  q$W |j|	}|rttdt|  |S )a  Draw samples from the posteriors represented by the
    (Nruns, Nsamples, Nparams)-shaped array datas, each sampled with
    the corresponding Nlive number of live points. Will draw without repetition,
    and weight according to the evidence in each input runr   Nc             S   s   g | ]\}}t |d  |qS )logL)r$   )r%   r   r   r   r   r   r'   E   s    z'draw_posterior_many.<locals>.<listcomp>zComputed log_evidences: %sc                s   g | ]}t |  qS r   )r   )r%   r#   )log_max_evidencer   r   r'   K   s    z#Relative weights of input files: %sc                s    g | ]}| t  |  qS r   )r   )r%   r"   )datasfracsr   r   r'   M   s    c                s   g | ]}|  qS r   r   )r%   r&   )Ntotr   r   r'   O   s    zDRelative weights of input files taking into account their length: %sc             S   s   g | ]\}}}t ||qS r   )r0   )r%   r   logwtlogZr   r   r   r'   R   s    zNumber of input samples: %sc             S   s   g | ]}t |qS r   )r   )r%   r   r   r   r   r'   S   s    z2Expected number of samples from each input file %sc             S   s    g | ]\}}t |t| qS r   )intr   )r%   fpr   r   r   r'   T   s    )sizez$Total number of samples produced: %i)astropyzipprintr   r   r   r+   r   r   r   appendtablevstack)r3   Nlivesr.   r<   log_evsr!   log_total_evidenceNspostsbigpospostfracmaskresultr   )r5   r3   r4   r2   r   draw_posterior_many>   s6     
      rL   c       	      C   s|   |dkrg S t |jd d }td |d< x*t|D ]\}}t|| |||d < q6W tt|d}t||}| |d  S )zo
    Draw N samples from the input data, weighted by log_wt.
    For large N there may be repeated samples
    r   r   inf)r;   )r   r   r   	enumerater   r   r   r   )	r   r!   Nr.   Zlog_cumsumsr"   Zlog_wtusZidxsr   r   r   draw_N_posterior]   s     
rQ   c       	         sd   ddl }tdd t| |D  \}}tt| fdd|D }dd t| ||D }|j|S )a  
    Draw Npost samples from the posteriors represented by the
    (Nruns, Nsamples, Nparams)-shaped array datas, each sampled with
    the corresponding number of live points Nlive. The returned number
    of samples may not be exactly Npost due to rounding
    r   Nc             S   s   g | ]\}}t |d  |qS )r1   )r$   )r%   r   r   r   r   r   r'   v   s    z)draw_N_posterior_many.<locals>.<listcomp>c                s$   g | ]}t t t|  qS r   )r8   roundr   )r%   r#   )NpostrD   r   r   r'   y   s    c             S   s   g | ]\}}}t |||qS r   )rQ   )r%   r   r6   rO   r   r   r   r'   z   s    )r<   r=   r   r   r@   rA   )	r3   rB   rS   r.   r<   rC   r!   rE   rF   r   )rS   rD   r   draw_N_posterior_manym   s    
rT   c       
         s$  ddl }tdd t|D  \}}|r8tdt|  tt|}t|fdd|D |rptdt  fddttD }t|  fd	d|D |rtd
t  g }dd t||D }	|rtdtdd |D   |rtdtdd t|	D   |j	
|S )ab  Draw samples from the posteriors represented by the
    (Nruns, Nsamples, Nparams)-shaped array datas from ROQ runs, each sampled with
    the corresponding Nlive number of live points. First rescales evidence in each
    mass prior bin then will draw without repetition,
    and weight according to the evidence in each input run ala draw_posterior_manyr   Nc             S   s   g | ]\}}t |d  |qS )r1   )r$   )r%   r   r   r   r   r   r'      s    z0draw_posterior_many_ROQ_runs.<locals>.<listcomp>zComputed log_evidences: %sc                s   g | ]}t |  qS r   )r   )r%   r#   )r2   r   r   r'      s    z#Relative weights of input files: %sc                s    g | ]}| t  |  qS r   )r   )r%   r"   )r3   r4   r   r   r'      s    c                s   g | ]}|  qS r   r   )r%   r&   )r5   r   r   r'      s    zDRelative weights of input files taking into account their length: %sc             S   s   g | ]\}}}t ||qS r   )r0   )r%   r   r6   r7   r   r   r   r'      s    zNumber of input samples: %sc             S   s   g | ]}t |qS r   )r   )r%   r   r   r   r   r'      s    z2Expected number of samples from each input file %sc             S   s    g | ]\}}t |t| qS r   )r8   r   )r%   r9   r:   r   r   r   r'      s    )r<   r=   r>   r   r   r   r+   r   r   r@   rA   )
r3   rB   r.   r<   rC   r!   rD   rE   rG   rF   r   )r5   r3   r4   r2   r   draw_posterior_many_ROQ_runs}   s*     
     rU   )F)F)F)F)F)numpyr   r   r   r   r   r   r   r   r	   r
   r   Znumpy.randomr   	functoolsr   r   r   r$   r0   rL   rQ   rT   rU   r   r   r   r   <module>   s   0




