示例#1
0
    def sample_nu(self, use_prior=False):
        '''
        Update self._nu with one slice sampling draw

        To ensure stationarity, the upper bound for sampling nu(x,y) is set to be 1/tau(y).

        Return the draw of self._nu
        '''
        def sample_func_nu(nu):
            self.set_nu(nu, x, y)
            if use_prior:
                return self.log_likelihood() + self.log_prior()
            else:
                return self.log_likelihood()

        for x in xrange(self._A):
            for y in xrange(self._A):
                if self._non_diagonal:
                    if x==y: 
                        assert self._nu[x,y]==0
                        continue
                upper_bound_nu = 1.0 / self._tau[y] - (sum(self._nu[:,y]) - self._nu[x,y])
                slice_sample(self._nu[x,y], sample_func_nu, window_size=self._nu_window_size, L_bound=0, R_bound=upper_bound_nu, step_out_limit=4)

        return self.get_nu()
示例#2
0
    def sample_tau_and_nu_revised(self, use_prior=False):
        '''
        Update self._tau and self._nu with one slice sampling round
        The ordering is specified as
        for x in range(A):
            sample tau[x]
            for y in range(A):
                sample nu[y, x]
        Return one draw of self._tau, self._nu
        '''

        ## closure
        def sample_func_tau(tau):
            self.set_tau(tau, x)
            if use_prior:
                return self.log_likelihood() + self.log_prior()
            else:
                return self.log_likelihood()

        def sample_func_nu(nu):
            ## !! [y,x] not [x,y] !!
            self.set_nu(nu, y, x)
            if use_prior:
                return self.log_likelihood() + self.log_prior()
            else:
                return self.log_likelihood()

        for x in range(self._A):
            upper_bound_tau = 1.0 / sum(self._nu[:, x])

            # TODO: remove the assert
            assert self._tau[
                x] <= upper_bound_tau, "tau = %f, upper_bound_tau = %f" % (
                    self._tau[x], upper_bound_tau)
            # slice sampling with inf stepping out limit
            slice_sample(self._tau[x],
                         sample_func_tau,
                         window_size=1.0,
                         L_bound=0,
                         R_bound=upper_bound_tau,
                         step_out_limit=4)

            for y in range(self._A):
                # TODO: remove the assert
                upper_bound_nu = 1.0 / self._tau[x] - (sum(self._nu[:, x]) -
                                                       self._nu[y, x])
                ## TODO: remove
                upper_bound_nu = min(upper_bound_nu, 10)
                assert self._nu[
                    y, x] <= upper_bound_nu, "nu = %f, upper_bound_nu = %f" % (
                        self._nu[y, x], upper_bound_nu)
                # slice sampling with inf stepping out limit
                slice_sample(self._nu[y, x],
                             sample_func_nu,
                             window_size=5.0,
                             L_bound=0,
                             R_bound=upper_bound_nu,
                             step_out_limit=4)

        return self.get_tau(), self.get_nu()
示例#3
0
    def sample_tau(self, use_prior=False):
        '''
        Update self._tau of shape (self._A) with one slice sampling draw.
        To ensure stationarity, which suffices to be ensured by tau(x) * nu(y,x) <1 for every (x,y). 
        The upper bound for sampling tau(x) is set to be min(1/nu(y,x)) among y.
        Return the draw of self._tau
        '''

        ## closure
        def sample_func_tau(tau):
            self.set_tau(tau, x)
            if use_prior:
                return self.log_likelihood() + self.log_prior()
            else:
                return self.log_likelihood()

        for x in range(self._A):
            ## constraining spectral radius by the max col sum
            upper_bound_tau = 1.0 / sum(self._nu[:, x])
            slice_sample(self._tau[x],
                         sample_func_tau,
                         window_size=self._tau_window_size,
                         L_bound=0.1,
                         R_bound=upper_bound_tau,
                         step_out_limit=4)

        return self.get_tau()
示例#4
0
    def sample_word_concentration(self, window_size=200, step_out_limit=0):
        def sample_func(word_concentration):
            self.set_word_concentration(word_concentration, x)
            return self.log_prob()

        for x in range(self._A):
            slice_sample(self._word_concentration[x],
                         sample_func,
                         window_size=window_size,
                         L_bound=1e-4,
                         R_bound=self._word_concentration_upper_bound,
                         step_out_limit=step_out_limit)

        return self._word_concentration.copy()
示例#5
0
    def sample_time_decay(self, window_size=100, step_out_limit=0):
        def sample_func(time_decay):
            self.set_time_decay(time_decay, x)
            return self.log_prob()

        for x in range(self._A):
            slice_sample(self._time_decay[x],
                         sample_func,
                         window_size=window_size,
                         L_bound=0,
                         R_bound=self._time_decay_upper_bound,
                         step_out_limit=step_out_limit)

        return self._time_decay.copy()
示例#6
0
    def sample_word_pseudocounts(self, window_size=10, step_out_limit=0):
        def sample_func(word_pseudocount):
            self.set_word_pseudocounts(word_pseudocount, x, v)
            return self.log_prob()

        for x in range(self._A):
            for v in range(self._V):
                slice_sample(self._word_pseudocounts[x, v],
                             sample_func,
                             window_size=window_size,
                             L_bound=0,
                             R_bound=self._word_pseudocount_upper_bound,
                             step_out_limit=step_out_limit)

        return self._word_pseudocounts.copy()
    def sample_tau_and_nu_revised(self, use_prior=False):
        '''
        Update self._tau and self._nu with one slice sampling round

        The ordering is specified as

        for x in xrange(A):
            sample tau[x]
            for y in xrange(A):
                sample nu[y, x]

        Return one draw of self._tau, self._nu
        '''
        ## closure
        def sample_func_tau(tau):
            self.set_tau(tau, x)
            if use_prior:
                return self.log_likelihood() + self.log_prior()
            else:
                return self.log_likelihood()

        def sample_func_nu(nu):
            ## !! [y,x] not [x,y] !!
            self.set_nu(nu, y, x)
            if use_prior:
                return self.log_likelihood() + self.log_prior()
            else:
                return self.log_likelihood()        

        for x in xrange(self._A):
            upper_bound_tau = 1.0 / sum(self._nu[:,x])

            # TODO: remove the assert
            assert self._tau[x] <= upper_bound_tau, "tau = %f, upper_bound_tau = %f" % (self._tau[x], upper_bound_tau)
            # slice sampling with inf stepping out limit
            slice_sample(self._tau[x], sample_func_tau, window_size=1.0, L_bound=0, R_bound=upper_bound_tau, step_out_limit=4)

            for y in xrange(self._A):
                # TODO: remove the assert
                upper_bound_nu = 1.0 / self._tau[x] - (sum(self._nu[:,x]) - self._nu[y,x])
                ## TODO: remove
                upper_bound_nu = min(upper_bound_nu, 10)
                assert self._nu[y,x] <= upper_bound_nu, "nu = %f, upper_bound_nu = %f" % (self._nu[y,x], upper_bound_nu)
                # slice sampling with inf stepping out limit
                slice_sample(self._nu[y,x], sample_func_nu, window_size=5.0, L_bound=0, R_bound=upper_bound_nu, step_out_limit=4)

        return self.get_tau(), self.get_nu()
示例#8
0
    def sample_influence(self, window_size=10, step_out_limit=2):
        def sample_func(influence):
            self.set_influence(influence, x, y)
            return self.log_prob()

        for x in range(self._A):
            for y in range(self._A):
                if self._non_diagonal:
                    if x == y:
                        continue
                slice_sample(self._influence[x, y],
                             sample_func,
                             window_size=window_size,
                             L_bound=0,
                             R_bound=self._influence_upper_bound,
                             step_out_limit=step_out_limit)

        return self._influence.copy()
示例#9
0
    def sample_baseline_rate(self, use_prior=False):
        '''
        Update self.baseline_rate with one slice sampling draw

        Return the draw of self._baseline_rate
        '''
        def sample_func_baseline_rate(baseline_rate):
            self.set_baseline_rate(baseline_rate, x)
            if use_prior:
                return self.log_likelihood() + self.log_prior()
            else:
                return self.log_likelihood()

        for x in xrange(self._A):
            # slice sampling with inf stepping out limit
            slice_sample(self._baseline_rate[x], sample_func_baseline_rate, window_size=self._baseline_rate_window_size, L_bound=0, step_out_limit=4)

        return self.get_baseline_rate()
示例#10
0
    def sample_tau(self, use_prior=False):
        '''
        Update self._tau of shape (self._A) with one slice sampling draw.

        To ensure stationarity, which suffices to be ensured by tau(x) * nu(y,x) <1 for every (x,y). 
        The upper bound for sampling tau(x) is set to be min(1/nu(y,x)) among y.

        Return the draw of self._tau
        '''
        ## closure
        def sample_func_tau(tau):
            self.set_tau(tau, x)
            if use_prior:
                return self.log_likelihood() + self.log_prior()
            else:
                return self.log_likelihood()

        for x in xrange(self._A):
            ## constraining spectral radius by the max col sum
            upper_bound_tau = 1.0 / sum(self._nu[:,x])
            slice_sample(self._tau[x], sample_func_tau, window_size=self._tau_window_size, L_bound=0.1, R_bound=upper_bound_tau, step_out_limit=4)

        return self.get_tau()
示例#11
0
import scipy.stats
import matplotlib as plt
from pylab import *
import slice_sampler
import mh_sampler

def plot_hist(x_l, file_path):
    figure()
    n, bins, patches = hist(x_l, 20, histtype='bar')
    plt.savefig(file_path)

def plot_trace(x_l, n_l, file_path):
    figure()
    plot(n_l,x_l)
    plt.savefig(file_path)


data = np.array([3.7, 3.4, 5.5, 5.0, 5.4, 6.6, 4.8, 4.4, 5.1, 5.4])

# MH Sampling
x_chain, n_chain, acc = mh_sampler.mh_sample(10000,data,6.0,2.0)
print acc
plot_hist(x_chain[:acc+1], '../Figures/Prob4/mu_hist.pdf')
plot_trace(x_chain[:acc+1], n_chain[:acc+1], '../Figures/Prob4/mu_trace.pdf')

# Slice Sampling
n = 10000
x_chain = slice_sampler.slice_sample(n,data,10.0)
n_chain = np.arange(1,n+2,1)
plot_hist(x_chain, '../Figures/Prob5/mu_hist.pdf')
plot_trace(x_chain, n_chain, '../Figures/Prob5/mu_trace.pdf')
示例#12
0
import slice_sampler
import mh_sampler


def plot_hist(x_l, file_path):
    figure()
    n, bins, patches = hist(x_l, 20, histtype='bar')
    plt.savefig(file_path)


def plot_trace(x_l, n_l, file_path):
    figure()
    plot(n_l, x_l)
    plt.savefig(file_path)


data = np.array([3.7, 3.4, 5.5, 5.0, 5.4, 6.6, 4.8, 4.4, 5.1, 5.4])

# MH Sampling
x_chain, n_chain, acc = mh_sampler.mh_sample(10000, data, 6.0, 2.0)
print acc
plot_hist(x_chain[:acc + 1], '../Figures/Prob4/mu_hist.pdf')
plot_trace(x_chain[:acc + 1], n_chain[:acc + 1],
           '../Figures/Prob4/mu_trace.pdf')

# Slice Sampling
n = 10000
x_chain = slice_sampler.slice_sample(n, data, 10.0)
n_chain = np.arange(1, n + 2, 1)
plot_hist(x_chain, '../Figures/Prob5/mu_hist.pdf')
plot_trace(x_chain, n_chain, '../Figures/Prob5/mu_trace.pdf')