Beispiel #1
0
def test_eight_schools_sample():
    """Compile a simple model."""
    posterior = pystan.compile(program_code, data=schools_data)
    fit = posterior.sample(num_chains=1, num_samples=200, num_warmup=200)
    num_flat_params = schools_data['J'] * 2 + 2
    assert fit.values.shape == (1, 200, num_flat_params)
    df = fit.to_frame()
    assert 'eta.1' in df.columns
    assert len(df['eta.1']) == 200
Beispiel #2
0
def test_normal_sample_chains():
    """Sample from normal distribution with more than one chain."""
    program_code = 'parameters {real y;} model {y ~ normal(0,1);}'
    posterior = pystan.compile(program_code, data={})
    assert posterior is not None
    fit = posterior.sample(num_chains=2)
    assert fit.values.shape == (2, 1000, 1)  # 1 chain, n samples, 1 param
    df = fit.to_frame()
    assert len(df['y']) == 2000
    assert -5 < df['y'].mean() < 5
Beispiel #3
0
def test_normal_sample_args():
    """Sample from normal distribution with compile arguments."""
    program_code = 'parameters {real y;} model {y ~ normal(0,1);}'
    posterior = pystan.compile(program_code, data={}, random_seed=1)
    assert posterior is not None
    fit = posterior.sample(num_samples=350, num_thin=2)
    df = fit.to_frame()
    assert len(df['y']) == 350 // 2
    assert -5 < df['y'].mean() < 5
    assert fit.to_panel().shape == (1, 350 // 2, 1)  # 1 chain, n samples, 1 parameter
    assert fit.to_panel().loc[:, :, 'y'].shape == (350 // 2, 1)
def test_vector_params():
    """Sample from a program with vector params."""
    program_code = """
        parameters {
          vector[3] beta;
        }
        model {
          beta ~ normal(0, 1);
        }
    """
    posterior = pystan.compile(program_code, data={})
    fit = posterior.sample()
    df = fit.to_frame()
    assert all(df.columns == ['beta.1', 'beta.2', 'beta.3'])
    assert len(df['beta.1']) > 100
def test_matrix_params_compile():
    """Sample from a program with matrix-valued params."""
    program_code = """
        data {
          int<lower=2> K;
          int<lower=1> D;
        }
        parameters {
          matrix[K,D] beta;
        }
        model {
          for (k in 1:K)
            for (d in 1:D)
                beta[k,d] ~ normal(0, 1);
        }
    """
    data = {'K': 9, 'D': 5}
    posterior = pystan.compile(program_code, data=data)
    assert posterior is not None
def test_matrix_params_sample():
    """Sample from a program with matrix-valued params."""
    program_code = """
        data {
          int<lower=2> K;
          int<lower=1> D;
        }
        parameters {
          matrix[K,D] beta;
        }
        model {
          for (k in 1:K)
            for (d in 1:D)
                beta[k,d] ~ normal(0, 1);
        }
    """
    data = {'K': 9, 'D': 5}
    posterior = pystan.compile(program_code, data=data)
    fit = posterior.sample()
    df = fit.to_frame()
    assert len(df.columns) == data['K'] * data['D']
    assert len(df['beta.1.1']) > 100
Beispiel #7
0
def test_eight_schools_compile():
    """Compile a simple model."""
    posterior = pystan.compile(program_code, data=schools_data)
    assert posterior is not None
Beispiel #8
0
def test_normal_compile():
    """Compile a simple model."""
    program_code = 'parameters {real y;} model {y ~ normal(0,1);}'
    posterior = pystan.compile(program_code, data={})
    assert posterior is not None