Пример #1
0
def define_model(data):
    # Builds model object
    n = len(data)
    variable_names = ['g', 'sigma_g', 'p_type', 'T']
    known_params = {'sigma_z_g': sigma_z_g}
    hyper_params = {'prior_mu_g': 0*ones(n),
                    'prior_cov_g': 100*eye(n),
                    'alpha_type': (1., 1.),
                    'a_g': 3.,
                    'b_g': 1.}
    priors = {'sigma_g': stats.invgamma(hyper_params['a_g'], scale=hyper_params['b_g']),
              'p_type': dirichlet(hyper_params['alpha_type']),
              'T': iid_dist(categorical((1., 1.)), n),
              'g': mvnorm(hyper_params['prior_mu_g'], hyper_params['prior_cov_g'])}
    #initials = {'g': g[:n],
    #            'sigma_g': sigma_g}
    FCP_samplers = {'g': ground_height_step(),
                    'p_type': p_type_step(),
                    'T': type_step(),
                    'sigma_g': sigma_ground_step()}

    model = Model()
    model.set_variable_names(variable_names)
    model.set_known_params(known_params)
    model.set_hyper_params(hyper_params)
    model.set_priors(priors)
    #model.set_initials(initials)
    model.set_FCP_samplers(FCP_samplers)
    model.set_data(data)

    return model
def define_model(data):
    # Builds model object
    n = len(data)
    z = data.get('z')
    variable_names = ['surfaces', 'T', 'p_type', 'sigma_g', 'sigma_h']
    known_params = {
        'sigma_z_g': sigma_z_g,
        'sigma_z_h': sigma_z_h,
        'mu_h': mu_h,
        'phi': phi
    }
    hyper_params = {
        'alpha_type': array((1., 1., 1.)),
        'prior_mu_g': -25. + zeros(n),
        'prior_cov_g': 100. * eye(n),
        'prior_mu_h': 30. + zeros(n),
        'prior_cov_h': 100. * eye(n),
        'a_g': 11,
        'b_g': .1,
        'a_h': 11,
        'b_h': 40
    }
    initials = {
        'surfaces': [-25, 30] * ones((n, 2)),
        'sigma_g': .1,
        'sigma_h': 1,
        'T': array([(0 if abs(z[i] + 25) > 1 else 1) for i in xrange(n)])
    }
    #initials = {'sigma_g': sigma_g,
    #            'sigma_h': sigma_h,
    #            'T': T[:n],
    #            'g': g[:n],
    #            'h': h[:n]}
    priors = {
        'p_type': dirichlet(hyper_params['alpha_type']),
        'sigma_g': stats.invgamma(hyper_params['a_g'],
                                  scale=hyper_params['b_g']),
        'sigma_h': stats.invgamma(hyper_params['a_h'],
                                  scale=hyper_params['b_h']),
        'T': iid_dist(categorical(hyper_params['alpha_type']), n)
    }
    FCP_samplers = {
        'p_type': p_type_step(),
        'surfaces': surfaces_step(),
        'sigma_g': sigma_ground_step(),
        'sigma_h': sigma_height_step(),
        'T': type_step()
    }

    model = Model()
    model.set_variable_names(variable_names)
    model.set_known_params(known_params)
    model.set_hyper_params(hyper_params)
    model.set_priors(priors)
    model.set_initials(initials)
    model.set_FCP_samplers(FCP_samplers)
    model.set_data(data)

    return model
Пример #3
0
def define_model(data):
    # Builds model object
    m = 3
    n_points = len(data)
    n_shots = len(set(data['shot_id']))
    variable_names = ['g', 'h', 'T', 'p_type', 'sigma_g', 'sigma_h']
    known_params = {'sigma_z_g': sigma_z_g,
                    'sigma_z_h': sigma_z_h,
                    'mu_h': mu_h,
                    'phi': phi}
    hyper_params = {'alpha_type': array((0, 1., 1.)),
                    'prior_mu_g': -25.+zeros(n_shots),
                    'prior_cov_g': 100.*eye(n_shots),
                    'prior_mu_h': 30.+zeros(n_shots),
                    'prior_cov_h': 100.*eye(n_shots),
                    'a_g': 6,
                    'b_g': 1,
                    'a_h': 6,
                    'b_h': 1}
    initials = {}
    #initials = {'sigma_g': sigma_g,
    #            'sigma_h': sigma_h,
    #            'T': T[:n_shots],
    #            'g': g[:n_shots],
    #            'h': h[:n_shots]}
    priors = {'p_type': dirichlet(hyper_params['alpha_type']),
              'sigma_g': stats.invgamma(hyper_params['a_g'], scale=hyper_params['b_g']),
              'sigma_h': stats.invgamma(hyper_params['a_h'], scale=hyper_params['b_h']),
              'g': mvnorm(hyper_params['prior_mu_g'], hyper_params['prior_cov_g']),
              'h': mvnorm(hyper_params['prior_mu_h'], hyper_params['prior_cov_h']),
              'T': iid_dist(categorical(hyper_params['alpha_type']/sum(hyper_params['alpha_type'])), n_points)}
    FCP_samplers = {'p_type': p_type_step(),
                    'g': ground_height_step(),
                    'h': canopy_height_step(),
                    'sigma_g': sigma_ground_step(),
                    'sigma_h': sigma_height_step(),
                    'T': type_step()}

    model = Model()
    model.set_variable_names(variable_names)
    model.set_known_params(known_params)
    model.set_hyper_params(hyper_params)
    model.set_priors(priors)
    model.set_initials(initials)
    model.set_FCP_samplers(FCP_samplers)
    model.set_data(data)

    return model
def define_model(data):
    # Builds model object
    n = len(data)
    z = data.get('z')
    variable_names = ['surfaces', 'T', 'p_type', 'sigma_g', 'sigma_h']
    known_params = {'sigma_z_g': sigma_z_g,
                    'sigma_z_h': sigma_z_h,
                    'mu_h': mu_h,
                    'phi': phi}
    hyper_params = {'alpha_type': array((1., 1., 1.)),
                    'prior_mu_g': -25.+zeros(n),
                    'prior_cov_g': 100.*eye(n),
                    'prior_mu_h': 30.+zeros(n),
                    'prior_cov_h': 100.*eye(n),
                    'a_g': 11,
                    'b_g': .1,
                    'a_h': 11,
                    'b_h': 40}
    initials = {'surfaces': [-25, 30]*ones((n,2)),
                'sigma_g': .1,
                'sigma_h': 1,
                'T': array([(0 if abs(z[i]+25)>1 else 1) for i in xrange(n)])}
    #initials = {'sigma_g': sigma_g,
    #            'sigma_h': sigma_h,
    #            'T': T[:n],
    #            'g': g[:n],
    #            'h': h[:n]}
    priors = {'p_type': dirichlet(hyper_params['alpha_type']),
              'sigma_g': stats.invgamma(hyper_params['a_g'], scale=hyper_params['b_g']),
              'sigma_h': stats.invgamma(hyper_params['a_h'], scale=hyper_params['b_h']),
              'T': iid_dist(categorical(hyper_params['alpha_type']), n)}
    FCP_samplers = {'p_type': p_type_step(),
                    'surfaces': surfaces_step(),
                    'sigma_g': sigma_ground_step(),
                    'sigma_h': sigma_height_step(),
                    'T': type_step()}

    model = Model()
    model.set_variable_names(variable_names)
    model.set_known_params(known_params)
    model.set_hyper_params(hyper_params)
    model.set_priors(priors)
    model.set_initials(initials)
    model.set_FCP_samplers(FCP_samplers)
    model.set_data(data)

    return model
Пример #5
0
    def sample(self, model, evidence):
        g, p_type, z, sigma_z_g = [evidence[var]
                for var in ['g', 'p_type', 'z', 'sigma_z_g']]
        n = len(g)
        m = len(p_type)
        T = zeros(n)
        noise_rv = stats.uniform(z.min(), z.max() - z.min())
        for i in xrange(n):
            l = zeros(m)
            l[0] = p_type[0]*noise_rv.pdf(z[i])
            g_norm = stats.norm(g[i], sigma_z_g)
            l[1] = p_type[1]*g_norm.pdf(z[i])
            p = l/sum(l)
            type_rv = categorical(p)
            T[i] = type_rv.rvs()

        return T
Пример #6
0
    def sample(self, model, evidence):
        g, p_type, z, sigma_z_g = [
            evidence[var] for var in ['g', 'p_type', 'z', 'sigma_z_g']
        ]
        n = len(g)
        m = len(p_type)
        T = zeros(n)
        noise_rv = stats.uniform(z.min(), z.max() - z.min())
        for i in xrange(n):
            l = zeros(m)
            l[0] = p_type[0] * noise_rv.pdf(z[i])
            g_norm = stats.norm(g[i], sigma_z_g)
            l[1] = p_type[1] * g_norm.pdf(z[i])
            p = l / sum(l)
            type_rv = categorical(p)
            T[i] = type_rv.rvs()

        return T
Пример #7
0
    def sample(self, model, evidence):
        g, h, p_type, z, sigma_z_g, sigma_z_h = [evidence[var]
                for var in ['g', 'h', 'p_type', 'z', 'sigma_z_g', 'sigma_z_h']]
        N = len(z)
        m = len(p_type)
        T = zeros(N)
        noise_rv = stats.uniform(z.min(), z.max() - z.min())
        for i in xrange(N):
            l = zeros(m)
            l[0] = p_type[0]*noise_rv.pdf(z[i])
            g_norm = stats.norm(g[i], sigma_z_g)
            l[1] = p_type[1]*g_norm.pdf(z[i])
            if z[i] > g[i]:
                h_norm = stats.norm(h[i], sigma_z_h)
                l[2] = p_type[2]*h_norm.pdf(z[i] - g[i])
            p = l/sum(l)
            T[i] = categorical(p).rvs()

        return T
Пример #8
0
    def sample(self, model, evidence):
        g, h, p_type, z, sigma_z_g, sigma_z_h = [
            evidence[var]
            for var in ['g', 'h', 'p_type', 'z', 'sigma_z_g', 'sigma_z_h']
        ]
        N = len(z)
        m = len(p_type)
        T = zeros(N)
        noise_rv = stats.uniform(z.min(), z.max() - z.min())
        for i in xrange(N):
            l = zeros(m)
            l[0] = p_type[0] * noise_rv.pdf(z[i])
            g_norm = stats.norm(g[i], sigma_z_g)
            l[1] = p_type[1] * g_norm.pdf(z[i])
            if z[i] > g[i]:
                h_norm = stats.norm(h[i], sigma_z_h)
                l[2] = p_type[2] * h_norm.pdf(z[i] - g[i])
            p = l / sum(l)
            T[i] = categorical(p).rvs()

        return T
Пример #9
0
def define_model(data):
    # Builds model object
    n = len(data)
    variable_names = ['g', 'sigma_g', 'p_type', 'T']
    known_params = {'sigma_z_g': sigma_z_g}
    hyper_params = {
        'prior_mu_g': 0 * ones(n),
        'prior_cov_g': 100 * eye(n),
        'alpha_type': (1., 1.),
        'a_g': 3.,
        'b_g': 1.
    }
    priors = {
        'sigma_g': stats.invgamma(hyper_params['a_g'],
                                  scale=hyper_params['b_g']),
        'p_type': dirichlet(hyper_params['alpha_type']),
        'T': iid_dist(categorical((1., 1.)), n),
        'g': mvnorm(hyper_params['prior_mu_g'], hyper_params['prior_cov_g'])
    }
    #initials = {'g': g[:n],
    #            'sigma_g': sigma_g}
    FCP_samplers = {
        'g': ground_height_step(),
        'p_type': p_type_step(),
        'T': type_step(),
        'sigma_g': sigma_ground_step()
    }

    model = Model()
    model.set_variable_names(variable_names)
    model.set_known_params(known_params)
    model.set_hyper_params(hyper_params)
    model.set_priors(priors)
    #model.set_initials(initials)
    model.set_FCP_samplers(FCP_samplers)
    model.set_data(data)

    return model
Пример #10
0
mu_h = options.mu_h
sigma_g = options.sigma_g
sigma_z_g = options.sigma_z_g
sigma_h = options.sigma_h
sigma_z_h = options.sigma_z_h

p = options.noise_proportion
q = options.ground_proportion
r = 1 - p - q
proportions = (p, q, r)

transition_g_rv = stats.norm(0, sigma_g)
transition_h_rv = stats.norm(0, sigma_h)
observation_g_rv = stats.norm(0, sigma_z_g)
observation_h_rv = stats.norm(0, sigma_z_h)
type_rv = categorical(proportions)
noise_rv = stats.uniform(loc=-60, scale=120)

T = zeros((n, ), dtype=int)
g = zeros((n, ))
h = zeros((n, ))
g[0] = stats.norm(mu_g_0, sigma_g_0).rvs()
h[0] = stats.norm(mu_h_0, sigma_h_0).rvs()
T[0] = type_rv.rvs()[0]
if T[0] == 0:
    z = noise_rv.rvs()
if T[0] == 1:
    z = g[0] + observation_g_rv.rvs()
if T[0] == 2:
    z = g[0] + h[0] + observation_h_rv.rvs()
Пример #11
0
mu_h = options.mu_h
sigma_g = options.sigma_g
sigma_z_g = options.sigma_z_g
sigma_h = options.sigma_h
sigma_z_h = options.sigma_z_h

p = options.noise_proportion
q = options.ground_proportion
r = 1-p-q
proportions = (p, q, r)

transition_g_rv = stats.norm(0, sigma_g)
transition_h_rv = stats.norm(0, sigma_h)
observation_g_rv = stats.norm(0, sigma_z_g)
observation_h_rv = stats.norm(0, sigma_z_h)
type_rv = categorical(proportions)
noise_rv = stats.uniform(loc=-60, scale=120)

T = zeros((n,), dtype=int)
g = zeros((n,))
h = zeros((n,))
g[0] = stats.norm(mu_g_0, sigma_g_0).rvs()
h[0] = stats.norm(mu_h_0, sigma_h_0).rvs()
T[0] = type_rv.rvs()[0]
if T[0] == 0:
    z = noise_rv.rvs()
if T[0] == 1:
    z = g[0] + observation_g_rv.rvs()
if T[0] == 2:
    z = g[0] + h[0] + observation_h_rv.rvs()
Пример #12
0
def define_model(data):
    # Builds model object
    # Known values from simulation (can use to initialize sampler/skip burn-in)
    p = pickle.load(open('../data/sims/simulation_delta_0.pkl', 'rb'))
    g = p['g']
    h = p['h']
    T = p['T']
    sigma_g = p['sigma_g']
    sigma_h = p['sigma_h']
    # Also needed for known parameters (so load the right pkl dummy!)
    phi = p['phi']
    mu_h = p['mu_h']
    sigma_z_g = p['sigma_z_g']
    sigma_z_h = p['sigma_z_h']

    n = len(data)
    z = data.get('z')
    variable_names = ['g', 'h', 'T', 'p_type', 'sigma_g', 'sigma_h']
    known_params = {'sigma_z_g': sigma_z_g,
                    'sigma_z_h': sigma_z_h,
                    'mu_h': mu_h,
                    'phi': phi}
    hyper_params = {'alpha_type': array((1., 1., 1.)),
                    'prior_mu_g': -25.+zeros(n),
                    'prior_cov_g': 100.*eye(n),
                    'prior_mu_h': 30.+zeros(n),
                    'prior_cov_h': 100.*eye(n),
                    'a_g': 11,
                    'b_g': .1,
                    'a_h': 11,
                    'b_h': 40}
    #initials = {'g': -25+zeros(n),
    #            'sigma_g': .1,
    #            'sigma_h': 1,
    #            'T': array([(0 if abs(z[i]+25)>1 else 1) for i in xrange(n)])}
    initials = {'sigma_g': sigma_g,
                'sigma_h': sigma_h,
                'p_type': array((0, .5, .5)),
                'T': T[:n],
                'g': g[:n],
                'h': h[:n]}
    priors = {'p_type': dirichlet(hyper_params['alpha_type']),
              'sigma_g': stats.invgamma(hyper_params['a_g'], scale=hyper_params['b_g']),
              'sigma_h': stats.invgamma(hyper_params['a_h'], scale=hyper_params['b_h']),
              'g': mvnorm(hyper_params['prior_mu_g'], hyper_params['prior_cov_g']),
              'h': mvnorm(hyper_params['prior_mu_h'], hyper_params['prior_cov_h']),
              'T': iid_dist(categorical(hyper_params['alpha_type']), n)}
    FCP_samplers = {'p_type': p_type_step(),
                    'g': ground_height_step(),
                    'h': canopy_height_step(),
                    'sigma_g': sigma_ground_step(),
                    'sigma_h': sigma_height_step(),
                    'T': type_step()}

    model = Model()
    model.set_variable_names(variable_names)
    model.set_known_params(known_params)
    model.set_hyper_params(hyper_params)
    model.set_priors(priors)
    model.set_initials(initials)
    model.set_FCP_samplers(FCP_samplers)
    model.set_data(data)

    return model
Пример #13
0
def define_model(data):
    # Builds model object
    # Known values from simulation (can use to initialize sampler/skip burn-in)
    p = pickle.load(open('../data/sims/simulation_delta_0.pkl', 'rb'))
    g = p['g']
    h = p['h']
    T = p['T']
    sigma_g = p['sigma_g']
    sigma_h = p['sigma_h']
    # Also needed for known parameters (so load the right pkl dummy!)
    phi = p['phi']
    mu_h = p['mu_h']
    sigma_z_g = p['sigma_z_g']
    sigma_z_h = p['sigma_z_h']

    n = len(data)
    z = data.get('z')
    variable_names = ['g', 'h', 'T', 'p_type', 'sigma_g', 'sigma_h']
    known_params = {
        'sigma_z_g': sigma_z_g,
        'sigma_z_h': sigma_z_h,
        'mu_h': mu_h,
        'phi': phi
    }
    hyper_params = {
        'alpha_type': array((1., 1., 1.)),
        'prior_mu_g': -25. + zeros(n),
        'prior_cov_g': 100. * eye(n),
        'prior_mu_h': 30. + zeros(n),
        'prior_cov_h': 100. * eye(n),
        'a_g': 11,
        'b_g': .1,
        'a_h': 11,
        'b_h': 40
    }
    #initials = {'g': -25+zeros(n),
    #            'sigma_g': .1,
    #            'sigma_h': 1,
    #            'T': array([(0 if abs(z[i]+25)>1 else 1) for i in xrange(n)])}
    initials = {
        'sigma_g': sigma_g,
        'sigma_h': sigma_h,
        'p_type': array((0, .5, .5)),
        'T': T[:n],
        'g': g[:n],
        'h': h[:n]
    }
    priors = {
        'p_type': dirichlet(hyper_params['alpha_type']),
        'sigma_g': stats.invgamma(hyper_params['a_g'],
                                  scale=hyper_params['b_g']),
        'sigma_h': stats.invgamma(hyper_params['a_h'],
                                  scale=hyper_params['b_h']),
        'g': mvnorm(hyper_params['prior_mu_g'], hyper_params['prior_cov_g']),
        'h': mvnorm(hyper_params['prior_mu_h'], hyper_params['prior_cov_h']),
        'T': iid_dist(categorical(hyper_params['alpha_type']), n)
    }
    FCP_samplers = {
        'p_type': p_type_step(),
        'g': ground_height_step(),
        'h': canopy_height_step(),
        'sigma_g': sigma_ground_step(),
        'sigma_h': sigma_height_step(),
        'T': type_step()
    }

    model = Model()
    model.set_variable_names(variable_names)
    model.set_known_params(known_params)
    model.set_hyper_params(hyper_params)
    model.set_priors(priors)
    model.set_initials(initials)
    model.set_FCP_samplers(FCP_samplers)
    model.set_data(data)

    return model
Пример #14
0
def define_model(data):
    # Builds model object
    m = 3
    n_points = len(data)
    n_shots = len(set(data['shot_id']))
    variable_names = ['g', 'h', 'T', 'p_type', 'sigma_g', 'sigma_h']
    known_params = {
        'sigma_z_g': sigma_z_g,
        'sigma_z_h': sigma_z_h,
        'mu_h': mu_h,
        'phi': phi
    }
    hyper_params = {
        'alpha_type': array((0, 1., 1.)),
        'prior_mu_g': -25. + zeros(n_shots),
        'prior_cov_g': 100. * eye(n_shots),
        'prior_mu_h': 30. + zeros(n_shots),
        'prior_cov_h': 100. * eye(n_shots),
        'a_g': 6,
        'b_g': 1,
        'a_h': 6,
        'b_h': 1
    }
    initials = {}
    #initials = {'sigma_g': sigma_g,
    #            'sigma_h': sigma_h,
    #            'T': T[:n_shots],
    #            'g': g[:n_shots],
    #            'h': h[:n_shots]}
    priors = {
        'p_type':
        dirichlet(hyper_params['alpha_type']),
        'sigma_g':
        stats.invgamma(hyper_params['a_g'], scale=hyper_params['b_g']),
        'sigma_h':
        stats.invgamma(hyper_params['a_h'], scale=hyper_params['b_h']),
        'g':
        mvnorm(hyper_params['prior_mu_g'], hyper_params['prior_cov_g']),
        'h':
        mvnorm(hyper_params['prior_mu_h'], hyper_params['prior_cov_h']),
        'T':
        iid_dist(
            categorical(hyper_params['alpha_type'] /
                        sum(hyper_params['alpha_type'])), n_points)
    }
    FCP_samplers = {
        'p_type': p_type_step(),
        'g': ground_height_step(),
        'h': canopy_height_step(),
        'sigma_g': sigma_ground_step(),
        'sigma_h': sigma_height_step(),
        'T': type_step()
    }

    model = Model()
    model.set_variable_names(variable_names)
    model.set_known_params(known_params)
    model.set_hyper_params(hyper_params)
    model.set_priors(priors)
    model.set_initials(initials)
    model.set_FCP_samplers(FCP_samplers)
    model.set_data(data)

    return model