def run_test(training_size,
             scoring_function,
             parameter_bounds,
             corr_kernel,
             n_cluster,
             prior='GCP',
             log=True):

    x_training = []
    y_training = []
    for i in range(training_size):
        x = [
            np.random.uniform(parameter_bounds[j][0], parameter_bounds[j][1])
            for j in range(parameter_bounds.shape[0])
        ]
        x_training.append(x)
        y_training.append(scoring_function(x)[0])

    if (prior == 'GP'):
        gp = GaussianProcess(theta0=.1 * np.ones(parameter_bounds.shape[0]),
                             thetaL=0.001 * np.ones(parameter_bounds.shape[0]),
                             thetaU=10. * np.ones(parameter_bounds.shape[0]),
                             random_start=5,
                             nugget=nugget)
        gp.fit(x_training, y_training)
        likelihood = gp.reduced_likelihood_function_value_
    else:
        gcp = GaussianCopulaProcess(nugget=nugget,
                                    corr=corr_kernel,
                                    random_start=5,
                                    normalize=True,
                                    coef_latent_mapping=0.4,
                                    n_clusters=n_clusters)
        gcp.fit(x_training, y_training)
        likelihood = gcp.reduced_likelihood_function_value_

    if not log:
        likelihood = np.exp(likelihood)

    return likelihood
def run_test(training_size, scoring_function, parameter_bounds, corr_kernel, n_cluster, prior="GCP", log=True):

    x_training = []
    y_training = []
    for i in range(training_size):
        x = [
            np.random.uniform(parameter_bounds[j][0], parameter_bounds[j][1]) for j in range(parameter_bounds.shape[0])
        ]
        x_training.append(x)
        y_training.append(scoring_function(x)[0])

    if prior == "GP":
        gp = GaussianProcess(
            theta0=0.1 * np.ones(parameter_bounds.shape[0]),
            thetaL=0.001 * np.ones(parameter_bounds.shape[0]),
            thetaU=10.0 * np.ones(parameter_bounds.shape[0]),
            random_start=5,
            nugget=nugget,
        )
        gp.fit(x_training, y_training)
        likelihood = gp.reduced_likelihood_function_value_
    else:
        gcp = GaussianCopulaProcess(
            nugget=nugget,
            corr=corr_kernel,
            random_start=5,
            normalize=True,
            coef_latent_mapping=0.4,
            n_clusters=n_clusters,
        )
        gcp.fit(x_training, y_training)
        likelihood = gcp.reduced_likelihood_function_value_

    if not log:
        likelihood = np.exp(likelihood)

    return likelihood
Beispiel #3
0
def run_test(training_size,
             prediction_size,
             function_name,
             corr_kernel,
             n_cluster,
             prior='GCP'):

    scoring_function = functions[function_name]
    parameter_bounds = all_parameter_bounds[function_name]

    x_training = []
    y_training = []
    for i in range(training_size):
        x = [
            np.random.uniform(parameter_bounds[j][0], parameter_bounds[j][1])
            for j in range(parameter_bounds.shape[0])
        ]
        x_training.append(x)
        y_training.append(scoring_function(x)[0])
    if (isInt):
        x_training, y_training = compute_unique2(
            np.asarray(x_training, dtype=np.int32), np.asarray(y_training))

    candidates = []
    real_y = []
    for i in range(prediction_size):
        x = [
            np.random.uniform(parameter_bounds[j][0], parameter_bounds[j][1])
            for j in range(parameter_bounds.shape[0])
        ]
        candidates.append(x)
        real_y.append(scoring_function(x)[0])
    real_y = np.asarray(real_y)
    if (isInt):
        candidates = np.asarray(candidates, dtype=np.int32)

    if (prior == 'GP'):
        gp = GaussianProcess(theta0=.1 * np.ones(parameter_bounds.shape[0]),
                             thetaL=0.001 * np.ones(parameter_bounds.shape[0]),
                             thetaU=10. * np.ones(parameter_bounds.shape[0]),
                             random_start=5,
                             nugget=nugget)
        gp.fit(x_training, y_training)
        pred = gp.predict(candidates)
        likelihood = gp.reduced_likelihood_function_value_

    else:
        gcp = GaussianCopulaProcess(nugget=nugget,
                                    corr=corr_kernel,
                                    random_start=5,
                                    normalize=True,
                                    coef_latent_mapping=coef_latent_mapping,
                                    n_clusters=n_clusters)
        gcp.fit(x_training, y_training)
        likelihood = gcp.reduced_likelihood_function_value_

        if not (integratedPrediction):
            pred = gcp.predict(candidates)
        else:
            pred, _, _, _ = gcp.predict(candidates,
                                        eval_MSE=True,
                                        eval_confidence_bounds=True,
                                        integratedPrediction=True)

    mse = np.mean((pred - real_y)**2.)
    # Normalize
    mse = mse / (np.std(real_y)**2.)
    likelihood = np.exp(likelihood)
    return [mse, likelihood]
Beispiel #4
0
	        return False,0

	return True,idx_val[0]



params,output,m_o,std_o = get_exp_data(4001,495)
rand_candidates = utils.sample_random_candidates(1000,parameter_bounds,None,isInt=np.ones(5))

gcp = GaussianCopulaProcess(nugget = 1e-10,
                                corr= 'squared_exponential',
                                random_start=5,
                                n_clusters=1,
                                coef_latent_mapping = 0.1,
                                try_optimize=True)
gcp.fit(params,m_o,output,obs_noise=std_o)

gp = GaussianProcess(theta0= 0.1 ,
                     thetaL = 0.001,
                     random_start=1,
                     thetaU = 10.,
                     nugget=1e-10)
gp.fit(params,m_o)
gp_pred,sigma = gp.predict(rand_candidates,eval_MSE=True)
gp_bu = np.asarray(gp_pred) + 1.96*np.sqrt(sigma)
print gp.theta_

predictions,mse,bl,bu = gcp.predict(rand_candidates,eval_MSE=True,eval_confidence_bounds=True,integratedPrediction= False,coef_bound=.5)

params_bis = (10*params[:,3] +  5* params[:,0] + params[:,1]) / 10.
params_bis2 = params[:,2] + (3.* params[:,4] / 10. )
Beispiel #5
0
def find_best_candidate_with_GCP(X,
                                 raw_Y,
                                 mean_Y,
                                 std_Y,
                                 args,
                                 rand_candidates,
                                 verbose,
                                 acquisition_function='Simple'):
    corr_kernel = args[0]
    n_clusters = args[1]
    GCP_mapWithNoise = args[2]
    GCP_useAllNoisyY = args[3]
    GCP_model_noise = args[4]
    nugget = args[5]
    GCP_upperBound_coef = args[6]

    mean_gcp = GaussianCopulaProcess(nugget=nugget,
                                     corr=corr_kernel,
                                     random_start=5,
                                     n_clusters=n_clusters,
                                     mapWithNoise=GCP_mapWithNoise,
                                     useAllNoisyY=GCP_useAllNoisyY,
                                     model_noise=GCP_model_noise,
                                     try_optimize=True)
    mean_gcp.fit(X, mean_Y, raw_Y, obs_noise=std_Y)

    if (verbose == 2):
        print('GCP theta :' + str(mean_gcp.theta))

    if (acquisition_function == 'Simple'):

        predictions = mean_gcp.predict(rand_candidates,
                                       eval_MSE=False,
                                       eval_confidence_bounds=False)
        best_candidate_idx = np.argmax(predictions)
        best_candidate = rand_candidates[best_candidate_idx]
        if (verbose == 2):
            print 'Hopefully :', best_candidate, predictions[
                best_candidate_idx]

    elif (acquisition_function == 'UCB'):

        predictions,MSE,boundL,boundU = \
          mean_gcp.predict(rand_candidates,eval_MSE=True,eval_confidence_bounds=True,coef_bound = GCP_upperBound_coef)
        best_candidate_idx = np.argmax(boundU)
        best_candidate = rand_candidates[best_candidate_idx]
        if (verbose == 2):
            print 'Hopefully :', best_candidate, predictions[
                best_candidate_idx], boundU[best_candidate_idx]

    elif (acquisition_function == 'MaxLowerBound'):

        predictions,MSE,boundL,boundU = \
          mean_gcp.predict(rand_candidates,eval_MSE=True,eval_confidence_bounds=True,coef_bound = GCP_upperBound_coef)
        best_candidate_idx = np.argmax(boundL)
        best_candidate = rand_candidates[best_candidate_idx]
        if (verbose == 2):
            print 'Hopefully :', best_candidate, predictions[
                best_candidate_idx], boundL[best_candidate_idx], boundU[
                    best_candidate_idx]

    elif (acquisition_function == 'EI'):

        predictions,MSE = \
          mean_gcp.predict(rand_candidates,eval_MSE=True,transformY=False) # we want the predictions in the GP space
        y_best = np.max(mean_Y)
        sigma = np.sqrt(MSE)
        ei = [ gcp_compute_ei((rand_candidates[i]-mean_gcp.X_mean)/mean_gcp.X_std,predictions[i],sigma[i],y_best, \
            mean_gcp.mapping,mean_gcp.mapping_derivative) \
          for i in range(rand_candidates.shape[0]) ]

        best_candidate_idx = np.argmax(ei)
        best_candidate = rand_candidates[best_candidate_idx]
        if (verbose == 2):
            print 'Hopefully :', best_candidate, predictions[
                best_candidate_idx], ei[best_candidate_idx]

    else:
        print('Acquisition function not handled...')

    return best_candidate
for n_clusters in all_n_clusters:

	count += 2
	ax = fig.add_subplot(len(all_n_clusters),2,count)
	ax.set_title("GCP prediction")

	gcp = GaussianCopulaProcess(nugget = nugget,
								corr = corr_kernel,
								random_start = 5,
								n_clusters = n_clusters,
	                            coef_latent_mapping = coef_latent_mapping,
							 	mapWithNoise = GCP_mapWithNoise,
				 				useAllNoisyY = False,
				 				model_noise = None,
								try_optimize = True)
	gcp.fit(x_training,y_training)

	print '\nGCP fitted'
	print 'Theta', gcp.theta
	print 'Likelihood', np.exp(gcp.reduced_likelihood_function_value_)

	predictions,MSE,boundL,boundU = \
						gcp.predict(candidates,eval_MSE=True,eval_confidence_bounds=True,coef_bound = 1.96,integratedPrediction=integratedPrediction)

	pred_error = np.mean( (predictions - np.asarray(real_y) ) **2. )
	print 'MSE', pred_error / (np.std(real_y) **2.)

	idx = np.argsort(candidates[:,0])
	s_candidates = candidates[idx,0]
	s_boundL = boundL[idx]
	s_boundU = boundU[idx]
Beispiel #7
0
for n_clusters in all_n_clusters:

    fig = plt.figure()
    ax = fig.add_subplot(1, 2, 1, projection='3d')
    ax.set_title("GCP prediction")

    gcp = GaussianCopulaProcess(nugget=nugget,
                                corr=corr_kernel,
                                random_start=5,
                                n_clusters=n_clusters,
                                coef_latent_mapping=coef_latent_mapping,
                                mapWithNoise=GCP_mapWithNoise,
                                useAllNoisyY=False,
                                model_noise=None,
                                try_optimize=True)
    gcp.fit(x_training, y_training)

    print '\nGCP fitted'
    print 'Theta', gcp.theta
    print 'Likelihood', np.exp(gcp.reduced_likelihood_function_value_)

    predictions,MSE,boundL,boundU = \
         gcp.predict(candidates,eval_MSE=True,eval_confidence_bounds=True,coef_bound = 1.96,integratedPrediction=integratedPrediction)

    pred_error = np.mean((predictions - np.asarray(real_y))**2.)
    print 'MSE', pred_error
    print 'Normalized error', np.sqrt(pred_error) / np.std(real_y)

    pred, MSE_bis = gcp.predict(candidates,
                                eval_MSE=True,
                                transformY=False,
	if(j == 0):
		GCP_mapWithNoise = False
		model_noise = None
	else:
		GCP_mapWithNoise = False
		model_noise = 'EGN'

	gcp = GaussianCopulaProcess(nugget = nugget,
								corr = corr_kernel,
								random_start = 5,
								n_clusters = n_clusters,
							 	mapWithNoise = GCP_mapWithNoise,
				 				useAllNoisyY = False,
				 				model_noise = model_noise ,
								try_optimize = True)
	gcp.fit(x_training,y_training,y_detailed,obs_noise=noise_obs)

	print 'GCP fitted'
	print 'Theta', gcp.theta

	predictions,MSE,boundL,boundU = \
						gcp.predict(abs,eval_MSE=True,eval_confidence_bounds=True,coef_bound = 1.96)

	pred,MSE_bis = gcp.predict(abs,eval_MSE=True,transformY=False,eval_confidence_bounds=False,coef_bound = 1.96)
	gp_boundL = pred - 1.96*np.sqrt(MSE_bis)
	gp_boundU = pred + 1.96*np.sqrt(MSE_bis)
	t_f_plot =  [gcp.mapping(abs[i],f_plot[i],normalize=True) for i in range(len(f_plot))]
	t_y_training =  [gcp.mapping(x_training[i],y_training[i],normalize=True) for i in range(len(y_training))]
	t_all_y_training =  [gcp.mapping(all_x[i],array_y_detailed[i],normalize=True) for i in range(array_y_detailed.shape[0])]

	print pred.shape
    if (j == 0):
        GCP_mapWithNoise = False
        model_noise = None
    else:
        GCP_mapWithNoise = False
        model_noise = 'EGN'

    gcp = GaussianCopulaProcess(nugget=nugget,
                                corr=corr_kernel,
                                random_start=5,
                                n_clusters=n_clusters,
                                mapWithNoise=GCP_mapWithNoise,
                                useAllNoisyY=False,
                                model_noise=model_noise,
                                try_optimize=True)
    gcp.fit(x_training, y_training, y_detailed, obs_noise=noise_obs)

    print 'GCP fitted'
    print 'Theta', gcp.theta

    predictions,MSE,boundL,boundU = \
         gcp.predict(abs,eval_MSE=True,eval_confidence_bounds=True,coef_bound = 1.96)

    pred, MSE_bis = gcp.predict(abs,
                                eval_MSE=True,
                                transformY=False,
                                eval_confidence_bounds=False,
                                coef_bound=1.96)
    gp_boundL = pred - 1.96 * np.sqrt(MSE_bis)
    gp_boundU = pred + 1.96 * np.sqrt(MSE_bis)
    t_f_plot = [
Beispiel #10
0
def run_test(training_size, prediction_size, function_name, corr_kernel, n_cluster, prior="GCP"):

    scoring_function = functions[function_name]
    parameter_bounds = all_parameter_bounds[function_name]

    x_training = []
    y_training = []
    for i in range(training_size):
        x = [
            np.random.uniform(parameter_bounds[j][0], parameter_bounds[j][1]) for j in range(parameter_bounds.shape[0])
        ]
        x_training.append(x)
        y_training.append(scoring_function(x)[0])
    if isInt:
        x_training, y_training = compute_unique2(np.asarray(x_training, dtype=np.int32), np.asarray(y_training))

    candidates = []
    real_y = []
    for i in range(prediction_size):
        x = [
            np.random.uniform(parameter_bounds[j][0], parameter_bounds[j][1]) for j in range(parameter_bounds.shape[0])
        ]
        candidates.append(x)
        real_y.append(scoring_function(x)[0])
    real_y = np.asarray(real_y)
    if isInt:
        candidates = np.asarray(candidates, dtype=np.int32)

    if prior == "GP":
        gp = GaussianProcess(
            theta0=0.1 * np.ones(parameter_bounds.shape[0]),
            thetaL=0.001 * np.ones(parameter_bounds.shape[0]),
            thetaU=10.0 * np.ones(parameter_bounds.shape[0]),
            random_start=5,
            nugget=nugget,
        )
        gp.fit(x_training, y_training)
        pred = gp.predict(candidates)
        likelihood = gp.reduced_likelihood_function_value_

    else:
        gcp = GaussianCopulaProcess(
            nugget=nugget,
            corr=corr_kernel,
            random_start=5,
            normalize=True,
            coef_latent_mapping=coef_latent_mapping,
            n_clusters=n_clusters,
        )
        gcp.fit(x_training, y_training)
        likelihood = gcp.reduced_likelihood_function_value_

        if not (integratedPrediction):
            pred = gcp.predict(candidates)
        else:
            pred, _, _, _ = gcp.predict(
                candidates, eval_MSE=True, eval_confidence_bounds=True, integratedPrediction=True
            )

    mse = np.mean((pred - real_y) ** 2.0)
    # Normalize
    mse = mse / (np.std(real_y) ** 2.0)
    likelihood = np.exp(likelihood)
    return [mse, likelihood]
for i in range(nb_GCP_steps):
    rand_candidates = utils.sample_candidates(n_candidates, parameter_bounds,
                                              isInt)

    if (sampling_model == 'GCP'):
        mean_gcp = GaussianCopulaProcess(nugget=nugget,
                                         corr=corr_kernel,
                                         random_start=5,
                                         n_clusters=n_clusters,
                                         mapWithNoise=GCP_mapWithNoise,
                                         useAllNoisyY=GCP_useAllNoisyY,
                                         model_noise=GCP_model_noise,
                                         try_optimize=True)
        mean_gcp.fit(parameters,
                     mean_outputs,
                     raw_outputs,
                     obs_noise=std_outputs)

        if (acquisition_function == 'UCB'):
            predictions,MSE,boundL,boundU = \
              mean_gcp.predict(rand_candidates,eval_MSE=True,eval_confidence_bounds=True,coef_bound = GCP_upperBound_coef)
            best_candidate_idx = np.argmax(boundU)
            best_candidate = rand_candidates[best_candidate_idx]

            idx = np.argsort(rand_candidates[:, 0])
            s_candidates = rand_candidates[idx, 0]
            s_boundL = boundL[idx]
            s_boundU = boundU[idx]
            s_pred = predictions[idx]

            ax = fig.add_subplot(n_rows, 3, i + 1)
Beispiel #12
0
def find_best_candidate_with_GCP(X, raw_Y, mean_Y, std_Y, args, rand_candidates,verbose,acquisition_function='Simple'):
	corr_kernel = args[0]
	n_clusters = args[1]
	GCP_mapWithNoise = args[2]
	GCP_useAllNoisyY = args[3]
	GCP_model_noise = args[4]
	nugget = args[5]
	GCP_upperBound_coef = args[6]

	mean_gcp = GaussianCopulaProcess(nugget = nugget,
									corr = corr_kernel,
									random_start = 5,
									n_clusters = n_clusters,
								 	mapWithNoise = GCP_mapWithNoise,
					 				useAllNoisyY = GCP_useAllNoisyY,
					 				model_noise = GCP_model_noise,
									try_optimize = True)
	mean_gcp.fit(X,mean_Y,raw_Y,obs_noise=std_Y)

	if(verbose == 2):
		print ('GCP theta :'+str(mean_gcp.theta))
				
	if(acquisition_function=='Simple'):
	
		predictions = mean_gcp.predict(rand_candidates,eval_MSE=False,eval_confidence_bounds=False)
		best_candidate_idx = np.argmax(predictions)
		best_candidate = rand_candidates[best_candidate_idx]
		if(verbose == 2):
			print 'Hopefully :', best_candidate, predictions[best_candidate_idx]	
	
	elif(acquisition_function=='UCB'):
	
		predictions,MSE,boundL,boundU = \
				mean_gcp.predict(rand_candidates,eval_MSE=True,eval_confidence_bounds=True,coef_bound = GCP_upperBound_coef)
		best_candidate_idx = np.argmax(boundU)
		best_candidate = rand_candidates[best_candidate_idx]
		if(verbose == 2):
			print 'Hopefully :', best_candidate, predictions[best_candidate_idx], boundU[best_candidate_idx]

	elif(acquisition_function=='MaxLowerBound'):
	
		predictions,MSE,boundL,boundU = \
				mean_gcp.predict(rand_candidates,eval_MSE=True,eval_confidence_bounds=True,coef_bound = GCP_upperBound_coef)
		best_candidate_idx = np.argmax(boundL)
		best_candidate = rand_candidates[best_candidate_idx]
		if(verbose == 2):
			print 'Hopefully :', best_candidate, predictions[best_candidate_idx], boundL[best_candidate_idx],boundU[best_candidate_idx]

	elif(acquisition_function=='EI'):
	
		predictions,MSE = \
				mean_gcp.predict(rand_candidates,eval_MSE=True,transformY=False) # we want the predictions in the GP space
		y_best = np.max(mean_Y)
		sigma = np.sqrt(MSE)
		ei = [ gcp_compute_ei((rand_candidates[i]-mean_gcp.X_mean)/mean_gcp.X_std,predictions[i],sigma[i],y_best, \
						mean_gcp.mapping,mean_gcp.mapping_derivative) \
				for i in range(rand_candidates.shape[0]) ]

		best_candidate_idx = np.argmax(ei)
		best_candidate = rand_candidates[best_candidate_idx]
		if(verbose == 2):
			print 'Hopefully :', best_candidate, predictions[best_candidate_idx], ei[best_candidate_idx]

	else:
		print('Acquisition function not handled...')

	return best_candidate
	save_data = np.asarray([X_init[:,0],np.asarray(Y_init)]).T
	np.savetxt('data_plots/train_data_plot.csv',save_data,delimiter=',')

for i in range(nb_GCP_steps):
	rand_candidates = utils.sample_candidates(n_candidates,parameter_bounds,isInt)
	
	if(sampling_model == 'GCP'):
		mean_gcp = GaussianCopulaProcess(nugget = nugget,
										corr = corr_kernel,
										random_start = 5,
										n_clusters = n_clusters,
									 	mapWithNoise = GCP_mapWithNoise,
						 				useAllNoisyY = GCP_useAllNoisyY,
						 				model_noise = GCP_model_noise,
										try_optimize = True)
		mean_gcp.fit(parameters,mean_outputs,raw_outputs,obs_noise=std_outputs)

		if(acquisition_function == 'UCB'):
			predictions,MSE,boundL,boundU = \
					mean_gcp.predict(rand_candidates,eval_MSE=True,eval_confidence_bounds=True,coef_bound = GCP_upperBound_coef)
			best_candidate_idx = np.argmax(boundU)
			best_candidate = rand_candidates[best_candidate_idx]

			idx = np.argsort(rand_candidates[:,0])
			s_candidates = rand_candidates[idx,0]
			s_boundL = boundL[idx]
			s_boundU = boundU[idx]
			s_pred = predictions[idx]

			ax = fig.add_subplot(n_rows,3,i+1)
			ax.plot(abs,f_plot)