Esempio n. 1
0
theta_grid_max = 3.0
theta_grid_G = 121


def cond_a_nll(X, w):
    return cond_a_nll_b(X, w, sort_by_wopt_var=True)


def cond_a_sample(r, c, w, T=0):
    return cond_a_sample_b(r, c, w, T, sort_by_wopt_var=True)


while True:
    a = Array(M, N)
    alpha_norm(a, 1.0)
    a.new_edge_covariate('x')[:, :] = np.random.normal(0, 1, (M, N))

    d = NonstationaryLogistic()
    d.beta['x'] = theta

    d.match_kappa(a, kappa_target)
    a.generate(d)

    f = NonstationaryLogistic()
    f.beta['x'] = None

    f.fit_conditional(a, T=T_fit, verbose=True)
    abs_err = abs(f.beta['x'] - d.beta['x'])
    if abs_err > min_error:
        print f.beta['x']
        break
def generate_data(case, theta, seed):
    # Advance random seed for parameter and covariate construction
    seed.next()

    case = params['case']
    alpha = beta = kappa = offset = 0
    conditional_sample = False
    if 'fixed_example' in case:
        # Load parameters and covariates
        with open(case['fixed_example'], 'r') as example_file:
            example = json.load(example_file)

            v = np.array(example['nu'])
            M, N = v.shape

            if 'alpha' in example:
                alpha = np.array(example['alpha']).reshape((M,1))
            if 'beta' in example:
                beta = np.array(example['beta']).reshape((1,N))
            if 'kappa' in example:
                kappa = example['kappa']
            if 'offset' in example:
                offset = example['offset']

            if ('r' in example) and ('c' in example):
                conditional_sample = True
                r = example['r']
                c = example['c']

    else:
        # Generate parameters and covariates
        M, N = case['M'], case['N']
        if 'alpha_min' in case:
            alpha = np.random.uniform(size = (M,1)) + case['alpha_min']
        if 'beta_min' in case:
            beta = np.random.uniform(size = (1,N)) + case['beta_min']
        if 'kappa' in case:
            kappa = case['kappa']
        if case['v_discrete']:
            v = np.sign(np.random.random(size = (M,N)) - 0.5) 
        elif case['v_uniform']:
            v = np.random.uniform(size = (M,N))
        elif case['v_normal']:
            v = np.random.normal(size = (M,N))
        if 'v_scale' in case:
            v *= case['v_scale']
        if 'v_loc' in case:
            v += case['v_loc']
        
        if ('r' in case) and ('c' in case):
            conditional_sample = True
            r = case['r']
            c = case['c']

    # Generate Bernoulli probabilities from logistic regression model
    logit_P = np.zeros((M,N)) + kappa
    logit_P += alpha
    logit_P += beta
    logit_P += theta * v
    logit_P += offset

    if conditional_sample:
        arr = Array(M, N)
        arr.new_edge_covariate('x_0')[:] = logit_P
        arr.new_row_covariate('r', dtype = np.int)[:] = r
        arr.new_col_covariate('c', dtype = np.int)[:] = c
        
        base_model = StationaryLogistic()
        base_model.beta['x_0'] = 1.0
        data_model = FixedMargins(base_model)

    while True:
        # Advance random seed for data generation
        seed.next()

        # Generate data for this trial
        if conditional_sample:
            X = data_model.generate(arr, coverage = 100.0)
        else:
            P = 1.0 / (1.0 + np.exp(-logit_P))
            X = np.random.random((M,N)) < P

        yield X, v
def generate_data(case, theta, seed):
    # Advance random seed for parameter and covariate construction
    seed.next()

    case = params["case"]
    alpha = beta = kappa = offset = 0
    conditional_sample = False
    if "fixed_example" in case:
        # Load parameters and covariates
        with open(case["fixed_example"], "r") as example_file:
            example = json.load(example_file)

            v = np.array(example["nu"])
            M, N = v.shape

            if "alpha" in example:
                alpha = np.array(example["alpha"]).reshape((M, 1))
            if "beta" in example:
                beta = np.array(example["beta"]).reshape((1, N))
            if "kappa" in example:
                kappa = example["kappa"]
            if "offset" in example:
                offset = example["offset"]

            if ("r" in example) and ("c" in example):
                conditional_sample = True
                r = example["r"]
                c = example["c"]

    else:
        # Generate parameters and covariates
        M, N = case["M"], case["N"]
        if "alpha_min" in case:
            alpha = np.random.uniform(size=(M, 1)) + case["alpha_min"]
        if "beta_min" in case:
            beta = np.random.uniform(size=(1, N)) + case["beta_min"]
        if "kappa" in case:
            kappa = case["kappa"]
        if case["v_discrete"]:
            v = np.random.random(size=(M, N)) < 0.5
        else:
            v = np.random.uniform(size=(M, N))
        if "v_min" in case:
            v += case["v_min"]

        if ("r" in case) and ("c" in case):
            conditional_sample = True
            r = case["r"]
            c = case["c"]

    # Generate Bernoulli probabilities from logistic regression model
    logit_P = np.zeros((M, N)) + kappa
    logit_P += alpha
    logit_P += beta
    logit_P += theta * v
    logit_P += offset

    if conditional_sample:
        arr = Array(M, N)
        arr.new_edge_covariate("x_0")[:] = logit_P
        arr.new_row_covariate("r", dtype=np.int)[:] = r
        arr.new_col_covariate("c", dtype=np.int)[:] = c

        base_model = StationaryLogistic()
        base_model.beta["x_0"] = 1.0
        data_model = FixedMargins(base_model)

    while True:
        # Advance random seed for data generation
        seed.next()

        # Generate data for this trial
        if conditional_sample:
            X = data_model.generate(arr, coverage=2.0)
        else:
            P = 1.0 / (1.0 + np.exp(-logit_P))
            X = np.random.random((M, N)) < P

        yield X, v
        
        if ('r' in case) and ('c' in case):
            conditional_sample = True
            r = case['r']
            c = case['c']

    # Generate Bernoulli probabilities from logistic regression model
    logit_P = np.zeros((M,N)) + kappa
    logit_P += alpha
    logit_P += beta
    logit_P += theta * v
    logit_P += offset

    if conditional_sample:
        arr = Array(M, N)
        arr.new_edge_covariate('x_0')[:] = logit_P
        arr.new_row_covariate('r', dtype = np.int)[:] = r
        arr.new_col_covariate('c', dtype = np.int)[:] = c
        
        base_model = StationaryLogistic()
        base_model.beta['x_0'] = 1.0
        data_model = FixedMargins(base_model)

    while True:
        # Advance random seed for data generation
        seed.next()

        # Generate data for this trial
        if conditional_sample:
            X = data_model.generate(arr, coverage = 100.0)
        else:
min_error = 0.1
theta_grid_min = 0.0
theta_grid_max = 3.0
theta_grid_G = 121


def cond_a_nll(X, w):
    return cond_a_nll_b(X, w, sort_by_wopt_var = True)

def cond_a_sample(r, c, w, T = 0):
    return cond_a_sample_b(r, c, w, T, sort_by_wopt_var = True)

while True:
    a = Array(M, N)
    alpha_norm(a, 1.0)
    a.new_edge_covariate('x')[:,:] = np.random.normal(0, 1, (M, N))

    d = NonstationaryLogistic()
    d.beta['x'] = theta

    d.match_kappa(a, kappa_target)
    a.generate(d)

    f = NonstationaryLogistic()
    f.beta['x'] = None

    f.fit_conditional(a, T = T_fit, verbose = True)
    abs_err = abs(f.beta['x'] - d.beta['x'])
    if abs_err > min_error:
        print f.beta['x']
        break