Esempio n. 1
0
class Q0(Q0_Base):
    """ Define prior """
    def __init__(self):
        """ Define prior pdfs over stiffness, damping, and
            noise std.
        """

        self.p_k = Normal_PDF(mean=3, cov=0.5)
        self.p_c = Gamma_PDF(a=1, scale=0.1)
        self.p_sigma = Gamma_PDF(a=1, scale=0.1)

    def logpdf(self, x):

        # Convert to 2D array if currently 1D
        if len(np.shape(x)) == 1:
            x = np.array([x])

        # Calculate logpdf
        logpdf = (self.p_k.logpdf(x[:, 0]) + self.p_c.logpdf(x[:, 1]) +
                  self.p_sigma.logpdf(x[:, 2]))

        return logpdf

    def rvs(self, size):

        k = np.vstack(self.p_k.rvs(size))
        c = np.vstack(self.p_c.rvs(size))
        sigma = np.vstack(self.p_sigma.rvs(size))

        return np.hstack([k, c, sigma])
class Q0(Q0_Base):
    """ Define initial proposal """
    def __init__(self):
        self.pdf = Normal_PDF(mean=np.zeros(2), cov=np.eye(2))

    def logpdf(self, x):
        return self.pdf.logpdf(x)

    def rvs(self, size):
        return self.pdf.rvs(size)
def sample_motion(n_samples, indices, motion_df, img, xweight=None, yweight=None):
    """
    Sampel x and y motion for multivariate normal distribution.
    
    Parameters:
    - - - - -
    n_samples: int
        number of samples to generate
    indices: int, array
        indices to apply motion to
    motion_df: Pandas data frame
        data frame of sampled motion to fit 6d-Gaussian to
        expected column names: 'xrad', 'yrad', 'zrad'
                               'xshift', 'yshift', 'zshift'
    img: float, array
        input image
    xweight: float
        weight to apply to xmotion
    yweight: float
        weight to apply to ymotion
    """

    # get input image dimensions
    [n, p] = img.shape
    
    # get weights for each axis of motion
    if not xweight:
        xweight = 1
    if not yweight:
        yweight = 1

    # fit distribution from which to sample motion from
    mu = motion_df.mean(0)
    cov = motion_df.cov()
    gauss = MVN(mean=mu, cov= cov)

    # sample positions in K-space tracjectory for when
    # motion occurs in X and Y directions
    inds = np.random.choice(indices, size=n_samples, replace=False)
    inds = np.column_stack(np.unravel_index(inds, (n, p)))

    # sample motion for each coordinate
    samples = pd.DataFrame(
            gauss.rvs(size=n_samples),
            columns=motion_df.columns)

    x_motion = np.zeros((n, p))
    x_motion[inds[:, 0], inds[:, 1]] = xweight*np.asarray(samples['xshift'])
    x_coords = np.ravel_multi_index(np.where(x_motion != 0), x_motion.shape)

    y_motion = np.zeros((n, p))
    y_motion[inds[:, 0], inds[:, 1]] = yweight*np.asarray(samples['yshift'])
    y_coords = np.ravel_multi_index(np.where(y_motion != 0), y_motion.shape)

    return [x_motion, x_coords, y_motion, y_coords]
Esempio n. 4
0
 def cdf_w(self, w):
     a = np.sqrt(1 + self.r**2) * self.k * self.s
     b = -self.r * self.s
     rho = -b / np.sqrt(1 + b**2)
     Sigma = np.array([[1, rho], [rho, 1]])
     dist_MVN = MVN(mean=np.repeat(0, 2), cov=Sigma)
     x1 = a / np.sqrt(1 + b**2)
     if isinstance(w, float):
         X = [x1, w]
     else:
         X = np.c_[np.repeat(x1, len(w)), w]
     pval = dist_MVN.cdf(X)
     return pval
Esempio n. 5
0
def compute_JSD(mu_P, Sigma_P, mu_Q, Sigma_Q, N_MC_samples):

    # Jensen-Shannon distance (JSD)

    out_JSD = np.empty_like(mu_P)

    for index in range(mu_P.size):
        RV_p = MVN(mean=mu_P[index], cov=Sigma_P[index, index])
        RV_q = MVN(mean=mu_Q[index], cov=Sigma_Q[index, index])

        x = RV_p.rvs(N_MC_samples)
        p_x = RV_p.pdf(x)
        q_x = RV_q.pdf(x)
        m_x = (p_x + q_x) / 2.

        y = RV_q.rvs(N_MC_samples)
        p_y = RV_p.pdf(y)
        q_y = RV_q.pdf(y)
        m_y = (p_y + q_y) / 2.

        dKL_pm = np.log(p_x / m_x).mean()
        dKL_qm = np.log(q_y / m_y).mean()

        out_JSD[index] = 0.5 * (dKL_pm + dKL_qm)

    return out_JSD
class Target(Target_Base):
    """ Define target """

    def __init__(self):
        self.pdf = Normal_PDF(mean=np.array([3.0, 2.0]), cov=np.eye(2))

    def logpdf(self, x):
        return self.pdf.logpdf(x)
class Target(Target_Base):
    """ Define target """

    def __init__(self):
        self.pdf = Normal_PDF(mean=np.repeat(2, D), cov=0.1*np.eye(D))

    def logpdf(self, x):
        return self.pdf.logpdf(x)
Esempio n. 8
0
    def __init__(self):
        """ Define prior pdfs over stiffness, damping, and
            noise std.
        """

        self.p_k = Normal_PDF(mean=3, cov=0.5)
        self.p_c = Gamma_PDF(a=1, scale=0.1)
        self.p_sigma = Gamma_PDF(a=1, scale=0.1)
Esempio n. 9
0
def hastings_sampler(MN_OBJ, iter=10000):
    from scipy.stats import multivariate_normal as MN
    samples = np.zeros((iter, 2))
    d_old = np.random.rand(2)
    mus = [10, 10]
    sig = [[1, 0.5], [0.5, 1]]
    for i in range(iter):
        delta = np.random.multivariate_normal(mus, sig)
        MN_PRO = MN(mus, sig)
        d_new = d_old + delta
        u = np.random.rand()
        samples[i, :] = d_old
        if u <= (MN_OBJ.pdf(d_new) * MN_PRO.pdf(-1 * delta)) / (
                MN_OBJ.pdf(d_old) * MN_PRO.pdf(delta)):
            samples[i, :] = d_new
            d_old = d_new
        """
        x = p_x_given_y(y, mus, sigmas)
        y = p_y_given_x(x, mus, sigmas)
        samples[i, :] = [x, y]
        """

    return samples
 def __init__(self):
     self.pdf = Normal_PDF(mean=np.zeros(2), cov=np.eye(2))
 def __init__(self):
     self.pdf = Normal_PDF(mean=np.array([3.0, 2.0]), cov=np.eye(2))
Esempio n. 12
0
 def __init__(self):
     self.mean = np.array([3.0, 2.0])
     self.cov = np.eye(2)
     self.pdf = Normal_PDF(self.mean, self.cov)
Esempio n. 13
0
 def __init__(self):
     self.pdf = Normal_PDF(mean=0, cov=3)
 def __init__(self):
     self.pdf = Normal_PDF(mean=np.repeat(2, D), cov=0.1*np.eye(D))