示例#1
0
def _eval_bmlingam(n_trials, gen_data_params, hparamss, show_progress,
                   sampling_mode):
    """Evaluate BMLiNGAM model selection. 
    """
    # ---- Loop over trials ----
    n_corrects = 0
    for t in range(n_trials):
        # ---- Generate samples ----
        gen_data_params.seed = t
        data = gen_samples(gen_data_params)
        xs = data['xs']
        causality_true = data['causality_true']

        # ---- Estimate causality ----
        results = find_best_model(xs, hparamss, sampling_mode)
        hparams_best = results[0]
        posterior = results[1]
        causality_est = hparams_best['causality']

        # ---- Check result ----
        if causality_est == causality_true:
            n_corrects += 1

        if show_progress:
            print('causality (true/pred), p(M_MAP|D): (%s/%s), %e' %
                  (str(causality_true), str(causality_est), posterior))

    return n_corrects
示例#2
0
def infer_causality(xs, infer_params, varnames=None, verbose=1):
    """Infer causality based on samples given pair of columns in data.
    """
    assert (type(infer_params) == InferParams)

    if varnames is None:
        varnames = ['var1', 'var2']

    hparamss = define_hparam_searchspace(infer_params)
    sampling_mode = infer_params.sampling_mode
    hparams_best, post_prob, ll, hparams_rev, post_prob_rev, ll_rev = \
        find_best_model(xs, hparamss, sampling_mode)
    causality = hparams_best['causality']

    x1_name = varnames[0]
    x2_name = varnames[1]
    if causality == [1, 2]:
        src, dst = x1_name, x2_name
    else:
        src, dst = x2_name, x1_name

    result = {
        'Infered causality': '{} -> {}'.format(src, dst),
        '2 * log(p(M)) - log(p(M_rev))': '{}'.format(2 * (ll - ll_rev))
    }

    if 1 <= verbose:
        print(json.dumps(result, indent=2, sort_keys=True))

    if 2 <= verbose:
        print('---- Inference for variables "%s" and "%s" ----' %
              (x1_name, x2_name))
        print(
            'Inferred  : %s -> %s (posterior prob: %1.3f, loglikelihood: %1.3f)'
            % (src, dst, post_prob, ll))
        print(
            '(best_rev): %s -> %s (posterior prob: %1.3f, loglikelihood: %1.3f)'
            % (dst, src, post_prob_rev, ll_rev))
        print('')
        print('Hyper parameters of the optimal model:')
        show_hparams(hparams_best)
        print('')
        print('Hyper parameters of the reverse optimal model:')
        show_hparams(hparams_rev)
        print('')

    return {
        'x1_name': x1_name,
        'x2_name': x2_name,
        'xs': xs,
        'causality': causality,
        'causality_str': ('%s -> %s' % (src, dst)),
        'post_prob': post_prob,
        'hparams': hparams_best,
        'post_prob_rev': post_prob_rev,
        'hparams_rev': hparams_rev
    }
示例#3
0
def _estimate_hparams(xs, infer_params):
    assert (type(infer_params) == InferParams)

    sampling_mode = infer_params.sampling_mode
    hparamss = define_hparam_searchspace(infer_params)
    results = find_best_model(xs, hparamss, sampling_mode)
    hparams_best = results[0]
    bf = results[2] - results[5]  # Bayes factor

    return hparams_best, bf
示例#4
0
def test_find_best_model(verbose=False):
    gen_data_params = GenDataParams(n_samples=200,
                                    mu1_dist=5.0,
                                    mu2_dist=10.0,
                                    f1_coef=[1.0, 1.0, 1.5],
                                    f2_coef=[1.0, 2.0, 0.5],
                                    conf_dist=[['laplace'], ['exp'],
                                               ['uniform']],
                                    e1_dist=['laplace'],
                                    e2_dist=['laplace'],
                                    e1_std=3.0,
                                    e2_std=3.0,
                                    fix_causality=False,
                                    seed=0)

    # gen_data_params = deepcopy(gen_data_params_default)
    gen_data_params.n_samples = 200
    gen_data_params.n_confounders = 3
    gen_data_params.dists_e1 = ['laplace']
    gen_data_params.dists_e2 = ['laplace']
    gen_data_params.dist_be1 = 'be1=9.0'
    gen_data_params.dist_be2 = 'be2=9.0'
    gen_data_params.dist_bf1s = '1., 1., 1.5'
    gen_data_params.dist_bf2s = '1., 2., 0.5'
    gen_data_params.dists_conf = [['laplace'], ['exp'], ['uniform']]
    gen_data_params.dist_mu1 = 'mu1=5.0'
    gen_data_params.dist_mu2 = 'mu2=10.0'

    data = gen_artificial_data(gen_data_params)
    xs = data['xs']

    infer_params = infer_params1()
    sampling_mode = infer_params.sampling_mode
    hparamss = define_hparam_searchspace(infer_params)
    result1 = find_best_model(xs, hparamss, sampling_mode)
    print(result1)

    infer_params = infer_params2()
    sampling_mode = infer_params.sampling_mode
    hparamss = define_hparam_searchspace(infer_params)
    result2 = find_best_model(xs, hparamss, sampling_mode)
    print(result2)
示例#5
0
文件: expr1.py 项目: guhjy/bmlingam
def estimate_hparams(xs, infer_params):
    """Estimate hyperparameters with the largest marginal likelihood value.
    """
    assert (type(infer_params) == InferParams)

    sampling_mode = infer_params.sampling_mode
    hparamss = define_hparam_searchspace(infer_params)
    results = find_best_model(xs, hparamss, sampling_mode)
    hparams_best = results[0]
    bf = results[2] - results[5]  # Bayes factor

    return hparams_best, bf
示例#6
0
def _eval_bmlingam(comp_logP_func,
                   n_confounders,
                   n_trials,
                   n_samples,
                   hparamss,
                   rng,
                   show_result,
                   tied_sampling=False,
                   normalize_samples=False):
    """Evaluate BMLiNGAM model selection. 

    This function is invoked from _test_bmlingam_main(). 
    """
    # ---- Loop over trials ----
    n_corrects = 0
    for t in xrange(n_trials):
        # ---- Generate samples ----
        data = gen_samples(n_confounders,
                           n_samples,
                           rng,
                           normalize_samples=normalize_samples)
        xs = data['xs']
        causality_true = data['causality_true']

        # ---- Estimate causality ----
        if tied_sampling:
            causality_est, _ = bayesmixedlingam_np(xs, hparamss, rng)
            posterior = np.nan
        else:
            hparams_best, posterior = find_best_model(xs, hparamss, rng)
            causality_est = hparams_best['causality']

        # ---- Check result ----
        if causality_est == causality_true:
            n_corrects += 1

        if show_result:
            print('causality (true/pred), p(M_MAP|D): (%s/%s), %e' %
                  (str(causality_true), str(causality_est), posterior))

    return n_corrects