human
Complete this python program:
def _log_post_trace(trace, model, progressbar=False):
assistant
#Here is the Python program you requested.

    """Calculate the elementwise log-posterior for the sampled trace.
    Parameters
    trace : result of MCMC run
    model : PyMC Model
    Optional model. Default None, taken from context.
    progressbar: bool
    Whether or not to display a progress bar in the command line. The
    bar shows the percentage of completion, the evaluation speed, and
    the estimated time to completion
    Returns
    logp : array of shape (n_samples, n_observations)
    The contribution of the observations to the logp of the whole model.
    """
    cached = [(var, var.logp_elemwise) for var in model.observed_RVs]
    def logp_vals_point(pt):
        if (len(model.observed_RVs) == 0):
            return floatX(np.array([], dtype='d'))
        logp_vals = []
        for (var, logp) in cached:
            logp = logp(pt)
            if var.missing_values:
                logp = logp[(~ var.observations.mask)]
            logp_vals.append(logp.ravel())
        return np.concatenate(logp_vals)
    points = (tqdm(trace) if progressbar else trace)
    try:
        logp = (logp_vals_point(pt) for pt in points)
        return np.stack(logp)
    finally:
        if progressbar:
            points.close()