<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Horsetail matching is a method for optimizing under uncertainty. 
We are looking to minimize a quantity of interest, q, which is a function of design variables (that we can control), x, and uncertain variables (which are uncontrollable), u. Since u is uncertain, q at a given design x is also uncertain. We therefore need to define some measure of the behaviour of q under uncertainty to minimize instead. With horseail matching this measure is the difference between the bounds on the CDF (the horsetail plot) of q for a given design and targets for these bounds. 
For further details see the website
Step1:  A horsetail matching object requires a function that takes two arguments
Step2:  Next we must define the input uncertainties to the problem, by creating horsetail matching parameters. We can assign the parameters a distribution by using the base UncertainParameter class and defining a pdf function, or we can use the specific distribution subclasses such as UniformParameter and GaussianParameter. 
Here we create a uniformly distributed uncertainty and a gaussian uncertainty.
Then we create the HorsetailMatching object which will do the evalation of the horsetail matching metric. We can specify the target inverse CDF using the ftarget argument (by default a standard target of t(h) = 0 is used).
Step3:  Now we use the evalMetric method to evaluate the horsetail matching metric at a design point
Step4:  We can use the getHorsetail() method to visualize the behaviour under uncertainty. It returns plottable lists of values for the two horsetail curves (the upper and lower bounds on the CDF) as the first two returned tuples. In this case the two bounds are coincidental and the horsetail plot is the CDF. 
We can then plot this using the matplotlib module for plotting in python.
Step5:  Now if we want to use this within an optimization, its a simple as passing theHM.evalMetric to whichever optimizer we like. For example, using the scipy optimize module's minimize function
<ASSISTANT_TASK:>
Python Code:
from horsetailmatching import UncertainParameter, UniformParameter, GaussianParameter, HorsetailMatching
from horsetailmatching.demoproblems import TP1
Explanation: Horsetail matching is a method for optimizing under uncertainty. 
We are looking to minimize a quantity of interest, q, which is a function of design variables (that we can control), x, and uncertain variables (which are uncontrollable), u. Since u is uncertain, q at a given design x is also uncertain. We therefore need to define some measure of the behaviour of q under uncertainty to minimize instead. With horseail matching this measure is the difference between the bounds on the CDF (the horsetail plot) of q for a given design and targets for these bounds. 
For further details see the website: http://www-edc.eng.cam.ac.uk/aerotools/horsetailmatching/ which has links to the relevant publications. 
This tutorial illustrates how to setup and run a simple horsetail matching optimization. 
Note that for clarification on how any of the module works, see the documentation available at: http://www-edc.eng.cam.ac.uk/aerotools/horsetailmatching/documentation/
To begin with, we will only consider probabilistic uncertainties. This requires a probability distribution to be assigned to all of our uncertain parameters, and this is propagated to give a probability distribution of our quantity of interest for a given design x. Therefore the horsetail plot is the CDF itself and we are essentially doing CDF matching.
Firstly we need to import the functions and classes we will use from the horsetail matching module...
End of explanation
my_func = TP1
print TP1(x=[0, 1], u=[1, 0])
Explanation: A horsetail matching object requires a function that takes two arguments: the value of the design variables, and the value of the uncertainties; it should return the value quantity of interest. Here we will use a test problem that comes that is part of the horsetail matching module. In reality, this function is likely to be a computationally expensive simulation, for example a 3D computational fluid dynamics simulation of a wing.
End of explanation
def myPDF(u):
    if u > 1 or u < -1:
        return 0
    else:
        return 0.5
u_uniform = UncertainParameter(myPDF, lower_bound=-1, upper_bound=1)
u_gaussian = GaussianParameter(mean=0, standard_deviation=1)
def my_target(h): 
    return 0
theHM = HorsetailMatching(my_func, [u_uniform, u_gaussian], ftarget=my_target)
Explanation: Next we must define the input uncertainties to the problem, by creating horsetail matching parameters. We can assign the parameters a distribution by using the base UncertainParameter class and defining a pdf function, or we can use the specific distribution subclasses such as UniformParameter and GaussianParameter. 
Here we create a uniformly distributed uncertainty and a gaussian uncertainty.
Then we create the HorsetailMatching object which will do the evalation of the horsetail matching metric. We can specify the target inverse CDF using the ftarget argument (by default a standard target of t(h) = 0 is used).
End of explanation
print(theHM.evalMetric(x=[1,1]))
Explanation: Now we use the evalMetric method to evaluate the horsetail matching metric at a design point:
End of explanation
import matplotlib.pyplot as plt
(x1, y1, t1), (x2, y2, t2), _ = theHM.getHorsetail()
plt.plot(x1, y1, 'b', label='CDF')
plt.plot(t1, y1, 'k--', label='Target')
plt.xlim([-1, 6])
plt.xlabel('Quantity of Interest')
plt.legend(loc='lower right')
plt.show()
Explanation: We can use the getHorsetail() method to visualize the behaviour under uncertainty. It returns plottable lists of values for the two horsetail curves (the upper and lower bounds on the CDF) as the first two returned tuples. In this case the two bounds are coincidental and the horsetail plot is the CDF. 
We can then plot this using the matplotlib module for plotting in python.
End of explanation
from scipy.optimize import minimize
solution = minimize(theHM.evalMetric, x0=[3,2], method='Nelder-Mead')
print(solution)
(x1, y1, t1), (x2, y2, t2), _ = theHM.getHorsetail()
plt.plot(x1, y1, 'b', label='CDF')
plt.plot([theHM.ftarget(y) for y in y1], y1, 'k--', label='Target')
plt.xlim([-1, 6])
plt.xlabel('Quantity of Interest')
plt.legend(loc='lower right')
plt.show()
Explanation: Now if we want to use this within an optimization, its a simple as passing theHM.evalMetric to whichever optimizer we like. For example, using the scipy optimize module's minimize function:
End of explanation
<END_TASK>