Ejemplo n.º 1
0
def nonparam_qqplot(name=None, axins=True):
    print("Nonparam QQplot")
    fig, ax = newfig(1.0)
    Q = np.load("../results/gmrf_prec.npy")
    X = tr.to_normal(df.values)
    pl.QQplot(X, inv(np.cov(X.T)), pos=[50, 120, 10, 120], ax=ax, color='red', axins=axins)
    if name:
        savefig(name)
    else:
        savefig('nonparam_qqplot')
Ejemplo n.º 2
0
def main(method, transform, temporal, layout, hybrid, threshold, output):
    variables = getattr(layouts, layout)

    K = list(variables.keys())

    df = utils.prep_dataframe(keep=K)

    if temporal:
        df_shifted = utils.create_shifted_features(df)
        df = df.join(df_shifted, how="outer")
        df = df.dropna()

    if transform:
        print("* Tranform data")
        X = tr.to_normal(df.values)
        df = pd.DataFrame(X, index=df.index.values, columns=df.columns.values)

    X = df.values

    if hybrid:
        model = HRF(k=5, k_star=10, variables_names=df.columns.values)
    else:
        model = GMRF(method=method[0])

    model.fit(X)

    if not hybrid:
        print("* Selected alpha = {}".format(model.alpha_))
    else:
        print("* Selected k = {}, k star = {}".format(model.k, model.k_star))

    if threshold:
        Q = model.precision_.copy()
        ts = np.arange(0., 1., 0.001)

        bics = np.empty(len(ts))
        connectivity = np.empty(len(ts))

        n = Q.shape[0]
        gmrf_test = GMRF()
        gmrf_test.mean_ = np.mean(X, axis=0)
        for i, t in enumerate(ts):
            Q[Q < t] = 0
            gmrf_test.precision_ = Q
            bics[i], _ = gmrf_test.bic(X)
            connectivity[i] = 1 - np.size(np.where(Q == 0)[0]) / (n * n)

        fig, (ax, ax1) = plt.subplots(2, 1)
        ax.plot(connectivity)
        ax1.plot(bics)

    if output:
        results_name = os.path.join(os.path.dirname(__file__), "../results/")
        if hybrid:
            BNs = np.empty(len(model.variables_names), dtype=object)
            for i in range(len(BNs)):
                BNs[i] = (model.bns[i].variables_names, model.bns[i].nodes, model.bns[i].edges)
            np.save(results_name + output + "_bns", BNs)
        else:
            np.save(results_name + output + "_prec", model.precision_)
            np.save(results_name + output + "_mean", model.mean_)
            np.save(results_name + output + "_bic_scores", model.bic_scores)

    if not hybrid:
        plt.figure()
        plt.plot(model.bic_scores)

        fig, ax = plt.subplots(1, 1)
        pl.bin_precision_matrix(model.precision_, df.columns.values, ax)

        plt.show()
Ejemplo n.º 3
0
def main(alpha, transform, temporal, layout, steps, output, hybrid):
    variables = getattr(layouts, layout)

    K = list(variables.keys())

    df = utils.prep_dataframe(keep=K)

    if temporal:
        df_shifted = utils.create_shifted_features(df)
        df = df.join(df_shifted, how="outer")
        df = df.dropna()

        names = list(filter(lambda x: 'l1_' not in x, df.columns.values))

    if transform:
        print("* Tranform data")
        X = tr.to_normal(df.values)
        df = pd.DataFrame(X, index=df.index.values, columns=df.columns.values)

    if hybrid:
        model = HRF(variables_names=df.columns.values, k=5, k_star=10)
    else:
        model = GMRF(variables_names=df.columns.values, alpha=alpha)

    kf = KFold(df.shape[0], n_folds=5, shuffle=False)

    pool = mp.Pool(processes=5)

    print("* Scoring")
    kf_scores = [pool.apply_async(scoring,
                 args=(df, model, names, train, test, steps, id))
                 for id, (train, test) in enumerate(kf)]

    results = [p.get() for p in kf_scores]
    results = [np.array(t) for t in zip(*results)]

    r2 = results[0]
    kf_scores = results[1]
    #variances = results[2]

    r2 = np.sum(r2, axis=0) / len(kf)
    scores = np.sum(kf_scores, axis=0) / len(kf)
    #var = np.sum(variances, axis=0) / len(kf)

    if output:
        results_name = os.path.join(os.path.dirname(__file__),
                                    "../results/")
        np.save(results_name + output + str(steps) + "_kf_scores", kf_scores)
        np.save(results_name + output + str(steps) + "_scores", scores)
        np.save(results_name + output + str(steps) + "_r2", r2)
       # np.save(results_name + output + str(steps) + "_var", var)

    labels = df.columns.values
    labels = list(filter(lambda x: 'ahu' not in x, labels))

    if steps == 1:
        plt.figure()
        plt.boxplot(scores)
        plt.xticks(np.arange(1, 40), labels, rotation=90)
    else:
        plt.figure()
        plt.plot(scores)
        plt.figure()
        plt.plot(r2)

    plt.show()
Ejemplo n.º 4
0
def main(layout, model, transform, output):
    variables = getattr(layouts, layout)

    K = list(variables.keys())

    df = utils.prep_dataframe(keep=K)

    df_shifted = utils.create_shifted_features(df)
    df = df.join(df_shifted, how="outer")
    df = df.dropna()

    # print(list(enumerate(df.columns.values)))
    # assert False

    if transform:
        print("* Tranform data")
        X = tr.to_normal(df.values)
        df = pd.DataFrame(X, index=df.index.values, columns=df.columns.values)

    X = df.values

    if model[0] == 'gmrf':
        model = GMRF(variables_names=df.columns.values, alpha=0.1)
    elif model[0] == 'hybrid':
        model = HRF(k=5, k_star=10, variables_names=df.columns.values)

    lim = int(X.shape[0] * 0.75)
    X_train = X[:lim]
    X_test = X[lim:]

    model.fit(X_train)
    print("* Model Fitted")

    # controls_vars = ['ahu_1_outlet', 'ahu_2_outlet', 'ahu_3_outlet', 'ahu_4_outlet']
    controls_vars = ['ahu_3_outlet']
    controller = Controller(6, 15, 30)
    mdp = MDP(model, 1000, reward, 0.8, feature_creator, controller,
              controls_vars, n_jobs=3)
    mdp.learn()

    # plt.figure()
    # plt.hist(test[:, 38:42].ravel(), bins=5, range=(5, 30))

    # plt.figure()
    # plt.plot(test[:, 38:42])

    actions, states = run_simulation(X_test, controls_vars, mdp, model, controller)

    print(actions)
    actions_values_one = [None] * len(controls_vars)
    actions_values_two = [None] * len(controls_vars)
    for i in range(len(controls_vars)):
        actions_values_one[i] = [(j, a[i][1]) for j, a in enumerate(actions)
                                if a[i][0] == 0]
        actions_values_two[i] = [(j, a[i][1]) for j, a in enumerate(actions)
                                if a[i][0] == 1]

        actions_values_one[i] = list(zip(*actions_values_one[i]))
        actions_values_two[i] = list(zip(*actions_values_two[i]))

    for i in range(len(controls_vars)):
        plt.figure()
        if len(actions_values_one[i]) != 0:
            plt.plot(list(actions_values_one[i][0]), list(actions_values_one[i][1]), 'b')
        if len(actions_values_two[i]) != 0:
            plt.plot(list(actions_values_two[i][0]), list(actions_values_two[i][1]), 'g')
        plt.title(controls_vars[i])

    max_states = np.amax(states, axis=1)
    mean_states = np.mean(states, axis=1)
    min_states = np.amin(states, axis=1)

    plt.figure()
    plt.plot(max_states, 'r')
    plt.plot(mean_states, 'g')
    plt.plot(min_states, 'b')

    # plt.figure()
    # plt.plot(actions[:, 0], label="1")
    # plt.plot(actions[:, 1], label="2")
    # plt.plot(actions[:, 2], label="3")
    # plt.plot(actions[:, 3], label="4")
    # plt.legend(loc=0)

    # print(np.mean(actions, axis=0))

    plt.show()