Esempio n. 1
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[function_name]):
		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[function_name]):
		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]
Esempio n. 2
0
def main():
	### Set parameters ###
	parameter_bounds = np.asarray( [[0,400]] )
	training_size = 50
	nugget = 1.e-10
	n_clusters_max = 3
	corr_kernel = 'exponential_periodic' # 'squared_exponential'

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

	fig1 = plt.figure()
	plt.title('Density estimation')
	fig2 = plt.figure()
	plt.title("GCP prediction")

	n_rows = (n_clusters_max+1)/2
	if not((n_clusters_max+1)% 2 == 0):
		n_rows += 1

	mf_plt_axis = np.asarray(range(100)) / 100.
	mapping_functions_plot = []

	for n_clusters in range(1,n_clusters_max+1):

		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_
		print 'LGCP-'+str(n_clusters)+' fitted'
		# print 'Theta', gcp.theta
		print 'Likelihood',likelihood

		if(n_clusters > 1):
			centers = np.asarray([gcp.centroids[i][0]* gcp.X_std + gcp.X_mean for i in range(gcp.n_clusters) ], dtype=np.int32)

		m = np.mean(y_training)
		s = np.std(y_training)
		y_mean, y_std = gcp.raw_y_mean,gcp.raw_y_std
		x_density_plot = (np.asarray ( range(np.int(m *100.- 100.*(s)*10.),np.int(m*100. + 100.*(s)*10.)) ) / 100. - y_mean)/ y_std


		ax1 = fig1.add_subplot(n_rows,2,n_clusters)
		for i in range(gcp.n_clusters):
			plt_density_gcp = gcp.density_functions[i](x_density_plot)
			if(n_clusters > 1):
				l = 'Cluster ' + str(centers[i])
			else:
				l = 'KDE estimation'
			ax1.plot(x_density_plot*y_std + y_mean,plt_density_gcp,label=l)
		ax1.legend()
		ax1.set_title('n_clusters == ' + str(n_clusters) )

		candidates = np.atleast_2d(range(80)).T * 5
		prediction = gcp.predict(candidates)

		# plot results
		abs = range(0,400)
		f_plot = [scoring_function(i) for i in abs]
		ax2 = fig2.add_subplot(n_rows,2,n_clusters)
		plt.plot(abs,f_plot)
		plt.plot(x_training,y_training,'bo')
		#plt.plot(candidates,simple_prediction,'go',label='Simple prediction')
		plt.plot(candidates,prediction,'r+',label='n_clusters == ' + str(n_clusters))
		plt.axis([0,400,0,1.])
		ax2.set_title('Likelihood = ' + str(likelihood))
		plt.legend()

		mapping_functions_plot.append( [gcp.mapping(200,mf_plt_axis[i],normalize=True) for i in range(100)])

	## with GP ##
	gp = GaussianProcess(theta0=.1 ,
					 thetaL = 0.001,
					 thetaU = 10.,
					 random_start = 5,
					 nugget=nugget)
	gp.fit(x_training,y_training)
	likelihood = gp.reduced_likelihood_function_value_
	print 'GP'
	# print 'Theta',gp.theta_
	print 'Likelihood',likelihood
	candidates = np.atleast_2d(range(80)).T * 5
	prediction = gp.predict(candidates)

	# plot results
	abs = range(0,400)
	ax2 = fig2.add_subplot(n_rows,2,n_clusters_max+1)
	plt.plot(abs,f_plot)
	plt.plot(x_training,y_training,'bo')
	plt.plot(candidates,prediction,'r+',label='GP')
	plt.axis([0,400,0,1.])
	ax2.set_title('Likelihood = ' + str(likelihood))
	plt.legend()

	ax1 = fig1.add_subplot(n_rows,2,n_clusters_max+1)
	for i in range(n_clusters_max):
		ax1.plot(mf_plt_axis,np.asarray(mapping_functions_plot[i])[:,0],label=str(i+1)+' clusters')
	ax1.set_title('Mapping functions')
	ax1.legend(loc=4)

	plt.show()
def main():
    save_plots = False

    ### Set parameters ###
    nugget = 1.e-10
    all_n_clusters = [1]
    corr_kernel = 'squared_exponential'
    GCP_mapWithNoise = False
    sampling_model = 'GCP'
    integratedPrediction = False
    coef_latent_mapping = 0.1
    prediction_size = 1000

    ### Set parameters ###
    parameter_bounds = np.asarray([[0, 15], [0, 15]])
    training_size = 50

    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(branin_f(x)[0])
    x_training = np.asarray(x_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(branin_f(x)[0])
    real_y = np.asarray(real_y)
    candidates = np.asarray(candidates)

    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 '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,
                                    eval_confidence_bounds=False,
                                    coef_bound=1.96)

        t_f_plot = [
            gcp.mapping(candidates[i], real_y[i], normalize=True)
            for i in range(real_y.shape[0])
        ]
        t_y_training = [
            gcp.mapping(x_training[i], y_training[i], normalize=True)
            for i in range(len(y_training))
        ]

        ax.scatter(x_training[:, 0],
                   x_training[:, 1],
                   y_training,
                   c='g',
                   label='Training points',
                   alpha=0.5)
        ax.scatter(candidates[:, 0],
                   candidates[:, 1],
                   real_y,
                   c='b',
                   label='Branin function',
                   alpha=0.5)
        ax.scatter(candidates[:, 0],
                   candidates[:, 1],
                   predictions,
                   c='r',
                   label='predictions',
                   marker='+')

        ax = fig.add_subplot(1, 2, 2, projection='3d')
        ax.set_title('GP space')
        ax.scatter(x_training[:, 0],
                   x_training[:, 1],
                   t_y_training,
                   c='g',
                   label='Training points',
                   alpha=0.5)
        ax.scatter(candidates[:, 0],
                   candidates[:, 1],
                   t_f_plot,
                   c='b',
                   label='Branin function',
                   alpha=0.5)
        ax.scatter(candidates[:, 0],
                   candidates[:, 1],
                   pred,
                   c='r',
                   label='predictions',
                   marker='+')

    plt.legend()
    plt.show()
Esempio n. 4
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
def main():
	save_plots = False

	### Set parameters ###
	nugget = 1.e-10
	all_n_clusters = [1,2]
	corr_kernel = 'exponential_periodic'
	GCP_mapWithNoise= False
	sampling_model = 'GCP'
	integratedPrediction = False
	coef_latent_mapping = 0.1
	prediction_size = 1000

	### Set parameters ###
	parameter_bounds = np.asarray( [[0,400]] )
	training_size = 40

	if (save_plots):
		if not os.path.exists('data_UCB'):
			os.mkdir('data_UCB')

	abs = np.atleast_2d(range(0,400)).T
	f_plot = [scoring_function(i) for i in abs[:,0]]

	x_training = []
	y_training = []
	for i in range(training_size):
		x = np.random.uniform(0,400)
		x_training.append(x)
		y_training.append(scoring_function(x))
	x_training = np.atleast_2d(x_training).T

	candidates = []
	real_y = []
	for i in range(prediction_size):
		x = [np.random.uniform(0,400)]
		candidates.append(x)
		real_y.append(scoring_function(x[0]))
	real_y = np.asarray(real_y)
	candidates = np.asarray(candidates)

	count = -1
	fig = plt.figure()

	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 '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 'SMSE', 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]

		pred,MSE_bis = gcp.predict(np.atleast_2d(s_candidates).T,
								   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))]

		if(save_plots):
			save_data = np.asarray([s_candidates,boundL,boundU,predictions,f_plot]).T
			np.savetxt('data_UCB/data_plot.csv',save_data,delimiter=',')

		ax.plot(abs,f_plot)
		l1, = ax.plot(candidates,predictions,'r+',label='GCP predictions')
		l3, = ax.plot(x_training,y_training,'bo',label='Training points')
		ax.fill(np.concatenate([s_candidates,s_candidates[::-1]]),np.concatenate([s_boundL,s_boundU[::-1]]),alpha=.5, fc='c', ec='None')


		ax = fig.add_subplot(len(all_n_clusters),2,count+1)
		ax.set_title('GP space')
		ax.plot(abs,t_f_plot)
		ax.plot(s_candidates,pred,'r+',label='GCP predictions')
		ax.plot(x_training,t_y_training,'bo',label='Training points')
		ax.fill(np.concatenate([s_candidates,s_candidates[::-1]]),np.concatenate([gp_boundL,gp_boundU[::-1]]),alpha=.5, fc='c', ec='None')

		if(save_plots):
			t_save_data = np.asarray([s_candidates,gp_boundL,gp_boundU,pred,np.asarray(t_f_plot)[:,0]]).T
			np.savetxt('data_UCB/gpspace_data_plot.csv',t_save_data,delimiter=',')
			training_points = np.asarray([x_training[:,0],y_training,np.asarray(t_y_training)[:,0]]).T
			np.savetxt('data_UCB/train_data_plot.csv',training_points,delimiter=',')

	plt.legend()
	plt.show()
def main():
	save_plots = False

	### Set parameters ###
	nugget = 1.e-10
	all_n_clusters = [1,2]
	corr_kernel = 'exponential_periodic'
	GCP_mapWithNoise= False
	sampling_model = 'GCP'
	coef_latent_mapping = 0.1
	prediction_size = 400

	### Set parameters ###
	parameter_bounds = np.asarray( [[0,400]] )
	training_size = 30

	abs = np.atleast_2d(range(0,400)).T
	f_plot = [scoring_function(i) for i in abs[:,0]]

	x_training = []
	y_training = []
	for i in range(training_size):
		x = np.random.uniform(0,400)
		x_training.append(x)
		y_training.append(scoring_function(x))
	x_training = np.atleast_2d(x_training).T
	candidates = abs

	if (save_plots):
		if not os.path.exists('data_EI'):
			os.mkdir('data_EI')

		# store training data
		g=open('data_EI/training_data.csv','w')
		g.write('x,y\n')
		for i in range(training_size):
			g.write( str(x_training[i]) + ',' + str(y_training[i]) + '\n')
		g.close()

	count = 0
	fig = plt.figure()

	for n_clusters in all_n_clusters:
		if (save_plots):
			f=open('data_EI/cluster' + str(n_clusters) +'.csv','w')
			f.write('x,y,pred,ei\n')

		count += 1
		ax = fig.add_subplot(len(all_n_clusters),1,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 '\nLGCP fitted -', n_clusters, 'clusters'
		print 'Likelihood', np.exp(gcp.reduced_likelihood_function_value_)

		predictions = gcp.predict(candidates,eval_MSE=False,eval_confidence_bounds=False,coef_bound = 1.96,integratedPrediction=False)

		pred,mse = gcp.predict(candidates,eval_MSE=True,transformY=False)
		y_best =np.max(y_training)
		sigma = np.sqrt(mse)
		ei = [ utils.gcp_compute_ei((candidates[i]- gcp.X_mean) / gcp.X_std,pred[i],sigma[i],y_best, \
		                gcp.mapping,gcp.mapping_derivative) \
		        for i in range(candidates.shape[0]) ]
		ei = np.asarray(ei)

		if(save_plots):
			for i in range(abs.shape[0]):
				f.write( str(candidates[i,0]) + ',' + str(f_plot[i]) +',' + str(predictions[i]) + ',' + str(ei[i]) +'\n' )
			f.close()

		ax.plot(abs,f_plot)
		ax.plot(candidates,predictions,'r+',label='GCP predictions')
		ax.plot(x_training,y_training,'bo',label='Training points')
		ax.plot(candidates,100.*ei,'g+',label='EI')

	plt.legend()
	plt.show()
Esempio n. 7
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[function_name]):
        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[function_name]):
        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]
def main():
	save_plots = False

	### Set parameters ###
	nugget = 1.e-10
	all_n_clusters = [1]
	corr_kernel = 'squared_exponential'
	GCP_mapWithNoise= False
	sampling_model = 'GCP'
	integratedPrediction = False
	coef_latent_mapping = 0.1
	prediction_size = 1000

	### Set parameters ###
	parameter_bounds = np.asarray( [[0,15],[0,15]] )
	training_size = 50

	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(branin_f(x)[0])
	x_training = np.asarray(x_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(branin_f(x)[0])
	real_y = np.asarray(real_y)
	candidates = np.asarray(candidates)

	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 '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,eval_confidence_bounds=False,coef_bound = 1.96)

		t_f_plot =  [gcp.mapping(candidates[i],real_y[i],normalize=True) for i in range(real_y.shape[0])]
		t_y_training =  [gcp.mapping(x_training[i],y_training[i],normalize=True) for i in range(len(y_training))]

		ax.scatter(x_training[:,0],x_training[:,1],y_training,c='g',label='Training points',alpha=0.5)
		ax.scatter(candidates[:,0],candidates[:,1],real_y,c='b',label='Branin function',alpha=0.5)
		ax.scatter(candidates[:,0],candidates[:,1],predictions,c='r',label='predictions',marker='+')

		ax = fig.add_subplot(1,2,2, projection='3d')
		ax.set_title('GP space')
		ax.scatter(x_training[:,0],x_training[:,1],t_y_training,c='g',label='Training points',alpha=0.5)
		ax.scatter(candidates[:,0],candidates[:,1],t_f_plot,c='b',label='Branin function',alpha=0.5)
		ax.scatter(candidates[:,0],candidates[:,1],pred,c='r',label='predictions',marker='+')

	plt.legend()
	plt.show()
def main():
    save_plots = False

    ### Set parameters ###
    nugget = 1.e-10
    all_n_clusters = [1, 2]
    corr_kernel = 'exponential_periodic'
    GCP_mapWithNoise = False
    sampling_model = 'GCP'
    coef_latent_mapping = 0.1
    prediction_size = 400

    ### Set parameters ###
    parameter_bounds = np.asarray([[0, 400]])
    training_size = 30

    abs = np.atleast_2d(range(0, 400)).T
    f_plot = [scoring_function(i) for i in abs[:, 0]]

    x_training = []
    y_training = []
    for i in range(training_size):
        x = np.random.uniform(0, 400)
        x_training.append(x)
        y_training.append(scoring_function(x))
    x_training = np.atleast_2d(x_training).T
    candidates = abs

    if (save_plots):
        if not os.path.exists('data_EI'):
            os.mkdir('data_EI')

        # store training data
        g = open('data_EI/training_data.csv', 'w')
        g.write('x,y\n')
        for i in range(training_size):
            g.write(str(x_training[i]) + ',' + str(y_training[i]) + '\n')
        g.close()

    count = 0
    fig = plt.figure()

    for n_clusters in all_n_clusters:
        if (save_plots):
            f = open('data_EI/cluster' + str(n_clusters) + '.csv', 'w')
            f.write('x,y,pred,ei\n')

        count += 1
        ax = fig.add_subplot(len(all_n_clusters), 1, 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 '\nLGCP fitted -', n_clusters, 'clusters'
        print 'Likelihood', np.exp(gcp.reduced_likelihood_function_value_)

        predictions = gcp.predict(candidates,
                                  eval_MSE=False,
                                  eval_confidence_bounds=False,
                                  coef_bound=1.96,
                                  integratedPrediction=False)

        pred, mse = gcp.predict(candidates, eval_MSE=True, transformY=False)
        y_best = np.max(y_training)
        sigma = np.sqrt(mse)
        ei = [ utils.gcp_compute_ei((candidates[i]- gcp.X_mean) / gcp.X_std,pred[i],sigma[i],y_best, \
                        gcp.mapping,gcp.mapping_derivative) \
                for i in range(candidates.shape[0]) ]
        ei = np.asarray(ei)

        if (save_plots):
            for i in range(abs.shape[0]):
                f.write(
                    str(candidates[i, 0]) + ',' + str(f_plot[i]) + ',' +
                    str(predictions[i]) + ',' + str(ei[i]) + '\n')
            f.close()

        ax.plot(abs, f_plot)
        ax.plot(candidates, predictions, 'r+', label='GCP predictions')
        ax.plot(x_training, y_training, 'bo', label='Training points')
        ax.plot(candidates, 100. * ei, 'g+', label='EI')

    plt.legend()
    plt.show()