Example #1
0
 def __init__(self, mean=0, sigma=1, name='', model=None):
     super().__init__(name, model)
     self.Var('v1', Normal.dist(mu=mean, sigma=sigma))
     Normal('v2', mu=mean, sigma=sigma)
     Normal('v3', mu=mean, sigma=HalfCauchy('sd', beta=10, testval=1.))
     Deterministic('v3_sq', self.v3 ** 2)
     Potential('p1', tt.constant(1))
Example #2
0
 def __init__(self, mean=0, sigma=1, name="", model=None):
     super().__init__(name, model)
     self.register_rv(Normal.dist(mu=mean, sigma=sigma), "v1")
     Normal("v2", mu=mean, sigma=sigma)
     Normal("v3", mu=mean, sigma=Normal("sd", mu=10, sigma=1, initval=1.0))
     Deterministic("v3_sq", self.v3 ** 2)
     Potential("p1", at.constant(1))
Example #3
0
    def built_hyper_model(self):
        """
        Initialise :class:`pymc3.Model` depending on configuration file,
        geodetic and/or seismic data are included. Estimates initial parameter
        bounds for hyperparameters.
        """

        logger.info('... Building Hyper model ...\n')

        pc = self.config.problem_config

        if len(self.hierarchicals) == 0:
            self.init_hierarchicals()

        point = self.get_random_point(include=['hierarchicals', 'priors'])
        for param in pc.priors.values():
            point[param.name] = param.testvalue

        self.update_llks(point)

        with Model() as self.model:

            self.init_hyperparams()

            total_llk = tt.zeros((1), tconfig.floatX)

            for composite in self.composites.itervalues():
                total_llk += composite.get_hyper_formula(self.hyperparams, pc)

            like = Deterministic('tmp', total_llk)
            llk = Potential(self._like_name, like)
            logger.info('Hyper model building was successful!')
Example #4
0
 def __init__(self, mean=0, sigma=1, name="", model=None):
     super().__init__(name, model)
     self.Var("v1", Normal.dist(mu=mean, sigma=sigma))
     Normal("v2", mu=mean, sigma=sigma)
     Normal("v3", mu=mean, sigma=HalfCauchy("sd", beta=10, testval=1.0))
     Deterministic("v3_sq", self.v3 ** 2)
     Potential("p1", aet.constant(1))
Example #5
0
    def built_model(self):
        """
        Initialise :class:`pymc3.Model` depending on problem composites,
        geodetic and/or seismic data are included. Composites also determine
        the problem to be solved.
        """

        logger.info('... Building model ...\n')

        pc = self.config.problem_config

        with Model() as self.model:

            self.rvs, self.fixed_params = self.get_random_variables()

            self.init_hyperparams()

            total_llk = tt.zeros((1), tconfig.floatX)

            for datatype, composite in self.composites.iteritems():
                if datatype in bconfig.modes_catalog[pc.mode].keys():
                    input_rvs = utility.weed_input_rvs(self.rvs,
                                                       pc.mode,
                                                       datatype=datatype)
                    fixed_rvs = utility.weed_input_rvs(self.fixed_params,
                                                       pc.mode,
                                                       datatype=datatype)

                    if pc.mode == 'ffi':
                        # do the optimization only on the
                        # reference velocity model
                        logger.info("Loading %s Green's Functions" % datatype)
                        data_config = self.config[datatype + '_config']
                        composite.load_gfs(crust_inds=[
                            data_config.gf_config.reference_model_idx
                        ],
                                           make_shared=True)

                    total_llk += composite.get_formula(input_rvs, fixed_rvs,
                                                       self.hyperparams, pc)

            # deterministic RV to write out llks to file
            like = Deterministic('tmp', total_llk)

            # will overwrite deterministic name ...
            llk = Potential(self._like_name, like)
            logger.info('Model building was successful!')
Example #6
0
    def built_model(self):
        """
        Initialise :class:`pymc3.Model` depending on problem composites,
        geodetic and/or seismic data are included. Composites also determine
        the problem to be solved.
        """

        logger.info('... Building model ...\n')

        pc = self.config.problem_config

        with Model() as self.model:

            self.rvs, self.fixed_params = self.get_random_variables()

            self.init_hyperparams()

            total_llk = tt.zeros((1), tconfig.floatX)

            for datatype, composite in self.composites.items():
                if datatype in bconfig.modes_catalog[pc.mode].keys():
                    input_rvs = weed_input_rvs(self.rvs,
                                               pc.mode,
                                               datatype=datatype)
                    fixed_rvs = weed_input_rvs(self.fixed_params,
                                               pc.mode,
                                               datatype=datatype)

                else:
                    input_rvs = self.rvs
                    fixed_rvs = self.fixed_params

                total_llk += composite.get_formula(input_rvs, fixed_rvs,
                                                   self.hyperparams, pc)

            # deterministic RV to write out llks to file
            like = Deterministic('tmp', total_llk)

            # will overwrite deterministic name ...
            llk = Potential(self._like_name, like)
            logger.info('Model building was successful! \n')
Example #7
0
def build_model():
    y = shared(np.array([15, 10, 16, 11, 9, 11, 10, 18], dtype=np.float32))
    with Model() as arma_model:
        sigma = HalfCauchy('sigma', 5)
        theta = Normal('theta', 0, sd=2)
        phi = Normal('phi', 0, sd=2)
        mu = Normal('mu', 0, sd=10)

        err0 = y[0] - (mu + phi * mu)

        def calc_next(last_y, this_y, err, mu, phi, theta):
            nu_t = mu + phi * last_y + theta * err
            return this_y - nu_t

        err, _ = scan(fn=calc_next,
                      sequences=dict(input=y, taps=[-1, 0]),
                      outputs_info=[err0],
                      non_sequences=[mu, phi, theta])

        Potential('like', Normal.dist(0, sd=sigma).logp(err))
        variational.advi(n=2000)
    return arma_model
Example #8
0
    theta = Normal('theta', 0, sd=2)
    phi = Normal('phi', 0, sd=2)
    mu = Normal('mu', 0, sd=10)

    err0 = y[0] - (mu + phi*mu)

    def calc_next(last_y, this_y, err, mu, phi, theta):
        nu_t = mu + phi*last_y + theta*err
        return this_y - nu_t

    err, _ = scan(fn=calc_next,
                  sequences=dict(input=y, taps=[-1,0]),
                  outputs_info=[err0],
                  non_sequences=[mu, phi, theta])

    like = Potential('like', Normal.dist(0, sd=sigma).logp(err))

with arma_model:
    mu, sds, elbo = variational.advi(n=2000)


def run(n=1000):
    if n == "short":
        n = 50
    with arma_model:

        trace = sample(1000)

    burn = n/10

    traceplot(trace[burn:])
Example #9
0
# O is now (nObs, Dd)
# TODO: implement this with sparse matrices
O = np.concatenate([O[:, 0:T[i], i].T for i in range(N)])

#import pdb; pdb.set_trace()

model = Model()
with model:
    #Fails: #pi = Dirichlet('pi', a = as_tensor_variable([0.147026,0.102571,0.239819,0.188710,0.267137,0.054738]), shape=M, testval = np.ones(M)/float(M))
    pi = Dirichlet('pi',
                   a=as_tensor_variable([
                       0.147026, 0.102571, 0.239819, 0.188710, 0.267137,
                       0.054738
                   ]),
                   shape=M)
    pi_min_potential = Potential('pi_min_potential',
                                 TT.switch(TT.min(pi) < .001, -np.inf, 0))

    Q = DiscreteObsMJP_unif_prior('Q', M=M, lower=0.0, upper=1.0, shape=(M, M))

    #S = DiscreteObsMJP('S', pi=pi, Q=Q, M=M, nObs=nObs, observed_jumps=obs_jumps, T=T, shape=(nObs), testval=np.ones(nObs,dtype='int32'))
    S = DiscreteObsMJP('S',
                       pi=pi,
                       Q=Q,
                       M=M,
                       nObs=nObs,
                       observed_jumps=obs_jumps,
                       T=T,
                       shape=(nObs))

    #B0 = Beta('B0', alpha = 1., beta = 1., shape=(K,M), testval=0.2*np.ones((K,M)))
    #B = Beta('B', alpha = 1., beta = 1., shape=(K,M), testval=0.2*np.ones((K,M)))
    def setUp(self):
        #test Claims
        N = 100  # Number of patients
        M = 6  # Number of hidden states
        K = 10  # Number of comorbidities
        D = 721  # Number of claims
        Dd = 80  # Maximum number of claims that can occur at once
        min_obs = 10  # Minimum number of observed claims per patient
        max_obs = 30  # Maximum number of observed claims per patient
        self.M = M
        self.N = N
        self.K = K
        # Load pre-generated data
        from pickle import load

        T = load(open('../../data/X_layer_100_patients_old/T.pkl', 'rb'))
        self.T = T
        obs_jumps = load(
            open('../../data/X_layer_100_patients_old/obs_jumps.pkl', 'rb'))
        S_start = load(open('../../data/X_layer_100_patients_old/S.pkl', 'rb'))
        X_start = load(open('../../data/X_layer_100_patients_old/X.pkl', 'rb'))
        Z_start = load(open('../../data/X_layer_100_patients_old/Z.pkl', 'rb'))
        L_start = load(open('../../data/X_layer_100_patients_old/L.pkl', 'rb'))
        O = load(open('../../data/X_layer_100_patients_old/O_input.pkl', 'rb'))

        self.nObs = nObs = T.sum()
        self.zeroIndices = np.roll(self.T.cumsum(), 1)
        self.zeroIndices[0] = 0
        obs_jumps = np.hstack([np.zeros((N, 1), dtype='int8'), obs_jumps])
        obs_jumps = np.concatenate([obs_jumps[i, 0:T[i]] for i in range(N)])
        O = np.concatenate([O[:, 0:T[i], i].T for i in range(N)])
        S_start = np.concatenate([S_start[i, 0:T[i]] for i in range(N)])
        X_start = np.concatenate([X_start[:, 0:T[i], i].T for i in range(N)])
        anchors = []
        self.Z_original
        mask = np.ones((K, D))
        for anchor in anchors:
            for hold in anchor[1]:
                mask[:, hold] = 0
                mask[anchor[0], hold] = 1
        Z_start = Z_start[mask.nonzero()]

        with Model() as self.model:
            self.pi = Dirichlet('pi',
                                a=as_tensor_variable(
                                    [0.5, 0.5, 0.5, 0.5, 0.5, 0.5]),
                                shape=M)
            pi_min_potential = Potential(
                'pi_min_potential',
                TT.switch(TT.min(self.pi) < .1, -np.inf, 0))
            self.Q = DiscreteObsMJP_unif_prior('Q',
                                               M=M,
                                               lower=0.0,
                                               upper=1.0,
                                               shape=(M, M))
            self.S = DiscreteObsMJP('S',
                                    pi=self.pi,
                                    Q=self.Q,
                                    M=M,
                                    nObs=nObs,
                                    observed_jumps=obs_jumps,
                                    T=T,
                                    shape=(nObs))
            self.B0 = Beta('B0', alpha=1., beta=1., shape=(K, M))
            self.B = Beta('B', alpha=1., beta=1., shape=(K, M))
            self.X = Comorbidities('X',
                                   S=self.S,
                                   B0=self.B0,
                                   B=self.B,
                                   T=T,
                                   shape=(nObs, K))
            #self.Z = Beta('Z', alpha = 0.1, beta = 1., shape=(K,D))
            self.Z = Beta_with_anchors('Z',
                                       anchors=anchors,
                                       K=K,
                                       D=D,
                                       alpha=0.1,
                                       beta=1.,
                                       shape=(K, D))
            self.L = Beta('L', alpha=1., beta=1., shape=D)
            self.testClaims = Claims('O_obs',
                                     X=self.X,
                                     Z=self.Z,
                                     L=self.L,
                                     T=T,
                                     D=D,
                                     O_input=O,
                                     shape=(nObs, Dd),
                                     observed=O)

            self.forS = ForwardS(vars=[self.S],
                                 N=N,
                                 T=T,
                                 nObs=nObs,
                                 observed_jumps=obs_jumps)
            self.forX = ForwardX(vars=[self.X],
                                 N=N,
                                 T=T,
                                 K=K,
                                 D=D,
                                 Dd=Dd,
                                 O=O,
                                 nObs=nObs)

        from scipy.special import logit

        self.Q_raw_log = logit(
            np.array([0.631921, 0.229485, 0.450538, 0.206042, 0.609582]))

        B_lo = logit(
            np.array(
                [[0.000001, 0.760000, 0.720000, 0.570000, 0.700000, 0.610000],
                 [0.000001, 0.460000, 0.390000, 0.220000, 0.200000, 0.140000],
                 [0.000001, 0.620000, 0.620000, 0.440000, 0.390000, 0.240000],
                 [0.000001, 0.270000, 0.210000, 0.170000, 0.190000, 0.070000],
                 [0.000001, 0.490000, 0.340000, 0.220000, 0.160000, 0.090000],
                 [0.000001, 0.620000, 0.340000, 0.320000, 0.240000, 0.120000],
                 [0.000001, 0.550000, 0.390000, 0.320000, 0.290000, 0.150000],
                 [0.000001, 0.420000, 0.240000, 0.170000, 0.170000, 0.110000],
                 [0.000001, 0.310000, 0.300000, 0.230000, 0.190000, 0.110000],
                 [0.000001, 0.470000, 0.340000, 0.190000, 0.190000,
                  0.110000]]))

        B0_lo = logit(
            np.array(
                [[0.410412, 0.410412, 0.418293, 0.418293, 0.429890, 0.429890],
                 [0.240983, 0.240983, 0.240983, 0.240983, 0.240983, 0.240983],
                 [0.339714, 0.339714, 0.339714, 0.339714, 0.339714, 0.339714],
                 [0.130415, 0.130415, 0.130415, 0.130415, 0.130415, 0.130415],
                 [0.143260, 0.143260, 0.143260, 0.143260, 0.143260, 0.143260],
                 [0.211465, 0.211465, 0.211465, 0.211465, 0.211465, 0.211465],
                 [0.194187, 0.194187, 0.194187, 0.194187, 0.194187, 0.194187],
                 [0.185422, 0.185422, 0.185422, 0.185422, 0.185422, 0.185422],
                 [0.171973, 0.171973, 0.171973, 0.171973, 0.171973, 0.171973],
                 [0.152277, 0.152277, 0.152277, 0.152277, 0.152277,
                  0.152277]]))

        Z_lo = logit(Z_start)
        L_lo = logit(L_start)
        #import pdb; pdb.set_trace()
        self.myTestPoint = {
            'Q_ratematrixoneway': self.Q_raw_log,
            'B_logodds': B_lo,
            'B0_logodds': B0_lo,
            'S': S_start,
            'X': X_start,
            'Z_anchoredbeta': Z_lo,
            'L_logodds': L_lo,
            'pi_stickbreaking': np.array([0.5, 0.5, 0.5, 0.5, 0.5, 0.5])
        }
K = Z_start.shape[0]  # Number of comorbidities
D = Z_start.shape[1]  # Number of claims
Dmax = O.shape[1]  # Maximum number of claims that can occur at once

mask = np.ones((K, D))
for anchor in anchors:
    for hold in anchor[1]:
        mask[:, hold] = 0
        mask[anchor[0], hold] = 1
Z_start = Z_start[mask.nonzero()]

model = Model()
with model:
    # pi[m]: probability of starting in disease state m
    pi = Dirichlet('pi', a=as_tensor_variable(pi_start.copy()), shape=M)
    pi_min_potential = Potential('pi_min_potential',
                                 TT.switch(TT.min(pi) < .001, -np.inf, 0))

    # exp(t*Q)[m,m']: probability of transitioning from disease state m to m' after a period of time t
    Q = DiscreteObsMJP_unif_prior('Q', M=M, lower=0.0, upper=1.0, shape=(M, M))

    # S[o]: disease state (between 0 and M-1) at obeservation o
    #
    S = DiscreteObsMJP('S',
                       pi=pi,
                       Q=Q,
                       M=M,
                       nObs=nObs,
                       observed_jumps=obs_jumps,
                       T=T,
                       shape=(nObs))
Example #12
0
    def setUp(self):
        #test Claims
        N = 5  # Number of patients
        self.N = N
        M = 3  # Number of hidden states
        self.M = M
        K = 2  # Number of comorbidities
        D = 20  # Number of claims
        Dd = 4  # Maximum number of claims that can occur at once
        min_obs = 2  # Minimum number of observed claims per patient
        max_obs = 4  # Maximum number of observed claims per patient
        #obs_jumps = np.ones((N,max_obs-1))
        obs_jumps = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1],
                              [1, 1, 1]])
        T = np.array([4, 2, 3, 4, 2])
        self.T = T
        nObs = T.sum()
        obs_jumps = np.hstack([np.zeros((N, 1), dtype='int8'), obs_jumps])
        obs_jumps = np.concatenate([obs_jumps[i, 0:T[i]] for i in range(N)])

        #O(4,4,5)
        #O = np.zeros((nObs,Dd),dtype='int8')
        O = np.zeros((Dd, max_obs, N), dtype='int8')
        #import pdb; pdb.set_trace()
        O[[0, 1, 3, 2, 3, 3], [0, 1, 3, 2, 3, 3], [0, 1, 4, 3, 3, 4]] = 1
        #O[[0,5,11,12],[0,1,2,3]] = 1
        O = np.concatenate([O[:, 0:T[i], i].T for i in range(N)])

        Z_lo = np.array([[
            -2.30258509, -2.30258509, -2.30258509, -2.30258509, -2.30258509,
            -2.30258509, -2.30258509, -2.30258509, -2.30258509, -2.30258509,
            -2.30258509, -2.30258509, -2.30258509, -2.30258509, -2.30258509,
            -2.30258509, -2.30258509, -2.30258509, -2.30258509, -2.30258509
        ],
                         [
                             -2.30258509, -2.30258509, -2.30258509,
                             -2.30258509, -2.30258509, -2.30258509,
                             -2.30258509, -2.30258509, -2.30258509,
                             -2.30258509, -2.30258509, -2.30258509,
                             -2.30258509, -2.30258509, -2.30258509,
                             -2.30258509, -2.30258509, -2.30258509,
                             -2.30258509, -2.30258509
                         ]])

        anchors = []
        mask = np.ones((K, D))
        for anchor in anchors:
            for hold in anchor[1]:
                mask[:, hold] = 0
                mask[anchor[0], hold] = 1
        Z_lo = Z_lo[mask.nonzero()]

        with Model() as self.model:
            self.pi = Dirichlet('pi',
                                a=as_tensor_variable([0.5, 0.5, 0.5]),
                                shape=M)
            pi_min_potential = Potential(
                'pi_min_potential',
                TT.switch(TT.min(self.pi) < .1, -np.inf, 0))
            self.Q = DiscreteObsMJP_unif_prior('Q',
                                               M=M,
                                               lower=0.0,
                                               upper=1.0,
                                               shape=(M, M))
            self.S = DiscreteObsMJP('S',
                                    pi=self.pi,
                                    Q=self.Q,
                                    M=M,
                                    nObs=nObs,
                                    observed_jumps=obs_jumps,
                                    T=T,
                                    shape=(nObs))
            self.B0 = Beta('B0', alpha=1., beta=1., shape=(K, M))
            self.B = Beta('B', alpha=1., beta=1., shape=(K, M))
            self.X = Comorbidities('X',
                                   S=self.S,
                                   B0=self.B0,
                                   B=self.B,
                                   T=T,
                                   shape=(nObs, K))
            #self.Z = Beta('Z', alpha = 0.1, beta = 1., shape=(K,D))
            self.Z = Beta_with_anchors('Z',
                                       anchors=anchors,
                                       K=K,
                                       D=D,
                                       alpha=0.1,
                                       beta=1.,
                                       shape=(K, D))
            self.L = Beta('L', alpha=1., beta=1., shape=D)
            #L = Beta('L', alpha = 0.1, beta = 1, shape=D, transform=None)
            #L = Uniform('L', left = 0.0, right = 1.0, shape=D, transform=None)
            #L = Uniform('L', lower = 0.0, upper = 1.0, shape=D)
            self.testClaims = Claims('O_obs',
                                     X=self.X,
                                     Z=self.Z,
                                     L=self.L,
                                     T=T,
                                     D=D,
                                     O_input=O,
                                     shape=(nObs, Dd),
                                     observed=O)

            self.forS = ForwardS(vars=[self.S],
                                 N=N,
                                 T=T,
                                 nObs=nObs,
                                 observed_jumps=obs_jumps)
            self.forX = ForwardX(vars=[self.X],
                                 N=N,
                                 T=T,
                                 K=K,
                                 D=D,
                                 Dd=Dd,
                                 O=O,
                                 nObs=nObs)

        self.myTestPoint = {
            'Z_anchoredbeta':
            Z_lo,
            'Q_ratematrixoneway':
            np.array([0.1, 0.1]),
            'pi_stickbreaking':
            np.array([0.2, 0.1]),
            'S':
            np.array([[0, 0, 1, 1], [1, 1, 1, 1], [1, 1, 2, 2], [0, 2, 2, 2],
                      [0, 0, 0, 1]],
                     dtype=np.int32),
            'B0_logodds':
            np.array([[0., 1., 0.], [0., 0., 1.]]),
            'X':
            np.array([[[0, 1, 1, 1, 1], [0, 1, 1, 1, 1], [1, 1, 1, 1, 1],
                       [1, 1, 1, 1, 1]],
                      [[1, 1, 0, 0, 1], [1, 1, 0, 1, 1], [1, 1, 1, 1, 1],
                       [1, 1, 1, 1, 1]]],
                     dtype=np.int8),
            'L_logodds':
            np.array([
                0.1, 0.1, 0.1, 0.1, 0.01, 0.01, 0.01, 0.01, 0.0011, 0.0011,
                0.0011, 0.0011, 0.0011, 0., 0.0101, 0.0101, 0.0101, 0.01, 0.01,
                0.01
            ]),
            'B_logodds':
            np.array([[1., 0., 1.], [0., 1., 0.]])
        }
        self.myTestPoint['S'] = np.concatenate(
            [self.myTestPoint['S'][i, 0:T[i]] for i in range(N)])
        self.myTestPoint['X'] = np.concatenate(
            [self.myTestPoint['X'][:, 0:T[i], i].T for i in range(N)])
        stepX_Correct = np.array([[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0],
                                   [0, 0, 0, 0, 0], [0, 0, 0, 0, 1]],
                                  [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0],
                                   [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]]],
                                 dtype=np.int8)

        stepX_Correct = np.array([[[0, 0, 0, 0, 0], [0, 0, 0, 1, 0],
                                   [0, 0, 0, 1, 0], [0, 0, 0, 1, 0]],
                                  [[0, 1, 0, 0, 0], [0, 1, 0, 0, 0],
                                   [0, 1, 0, 0, 0], [0, 1, 0, 0, 1]]],
                                 dtype=np.int8)