N = 64
a = 4.0
#myMesh = ot.IntervalMesher([N]).build(ot.Interval(-a, a))
myMesh = ot.RegularGrid(-a, 2 * a / N, N + 1)

myCovarianceCollection = ot.CovarianceMatrixCollection()
for k in range(myMesh.getVerticesNumber()):
    t = myMesh.getVertices()[k]
    for l in range(k + 1):
        s = myMesh.getVertices()[l]
        matrix = ot.CovarianceMatrix(1)
        matrix[0, 0] = C(s[0], t[0])
        myCovarianceCollection.add(matrix)

covarianceModel = ot.UserDefinedCovarianceModel(myMesh, myCovarianceCollection)


def f(x):
    return [covarianceModel([x[0]], [x[1]])[0, 0]]


func = ot.PythonFunction(2, 1, f)
func.setDescription(['$s$', '$t$', '$cov$'])
cov_graph = func.draw([-a] * 2, [a] * 2, [512] * 2)
fig = plt.figure(figsize=(10, 4))
plt.suptitle('User defined covariance model')
cov_axis = fig.add_subplot(111)
View(cov_graph, figure=fig, axes=[cov_axis], add_legend=False)
    XX_input = ot.Sample([[0.1, 0], [0.32, 0], [0.6, 0], [0.9, 0], [
                         0.07, 1], [0.1, 1], [0.4, 1], [0.5, 1], [0.85, 1]])
    y_output = ot.Sample(len(XX_input), 1)
    for i in range(len(XX_input)):
        y_output[i, 0] = fun_mixte(XX_input[i])

    def C(s, t):
        return m.exp(-4.0 * abs(s - t) / (1 + (s * s + t * t)))

    N = 32
    a = 4.0
    myMesh = ot.IntervalMesher([N]).build(ot.Interval(-a, a))

    myCovariance = ot.CovarianceMatrix(myMesh.getVerticesNumber())
    for k in range(myMesh.getVerticesNumber()):
        t = myMesh.getVertices()[k]
        for l in range(k + 1):
            s = myMesh.getVertices()[l]
            myCovariance[k, l] = C(s[0], t[0])

    covModel_discrete = ot.UserDefinedCovarianceModel(myMesh, myCovariance)
    f_ = ot.SymbolicFunction(["tau", "theta", "sigma"], [
                             "(tau!=0) * exp(-1/theta) * sigma * sigma +  (tau==0) * exp(0) * sigma * sigma"])
    rho = ot.ParametricFunction(f_, [1, 2], [0.2, 0.3])
    covModel_discrete = ot.StationaryFunctionalCovarianceModel([1.0], [
                                                               1.0], rho)
    covModel_continuous = ot.SquaredExponential([1.0], [1.0])
    covarianceModel = ot.ProductCovarianceModel(
        [covModel_continuous, covModel_discrete])
    covarianceModel.discretize(XX_input)
from math import exp
from matplotlib import pyplot as plt
from openturns.viewer import View


def C(s, t):
    return exp(-4.0 * abs(s - t) / (1 + (s * s + t * t)))


N = 64
a = 4.0
# myMesh = ot.IntervalMesher([N]).build(ot.Interval(-a, a))
myMesh = ot.RegularGrid(-a, 2 * a / N, N + 1)

vertices = myMesh.getVertices()
myCovariance = ot.CovarianceMatrix(len(vertices))
for k in range(len(vertices)):
    t = vertices[k]
    for l in range(k + 1):
        s = vertices[l]
        myCovariance[k, l] = C(s[0], t[0])

covarianceModel = ot.UserDefinedCovarianceModel(myMesh, myCovariance)
cov_graph = covarianceModel.draw(0, 0, -a, a, 512)
cov_graph.setTitle('User defined covariance model')

fig = plt.figure(figsize=(10, 4))
cov_axis = fig.add_subplot(111)
View(cov_graph, figure=fig, axes=[cov_axis],
     add_legend=False, square_axes=True)
def C(s, t):
    return m.exp(-4.0 * abs(s - t) / (1 + (s * s + t * t)))


# %%
# Create the large covariance matrix
covariance = ot.CovarianceMatrix(mesh.getVerticesNumber())
for k in range(mesh.getVerticesNumber()):
    t = mesh.getVertices()[k]
    for l in range(k + 1):
        s = mesh.getVertices()[l]
        covariance[k, l] = C(s[0], t[0])

# %%
# Create the covariance model
covmodel = ot.UserDefinedCovarianceModel(mesh, covariance)


# %%
# Draw the covariance model
def f(x):
    return [covmodel([x[0]], [x[1]])[0, 0]]


func = ot.PythonFunction(2, 1, f)
func.setDescription(['$s$', '$t$', '$cov$'])
cov_graph = func.draw([-a] * 2, [a] * 2, [512] * 2)
cov_graph.setLegendPosition('')
view = viewer.View(cov_graph)
plt.show()
graph.setTitle('Original covariance model')
graph.setLegendPosition('')
view = viewer.View(graph)

# %%
# Create data from a non stationary normal process Omega * [0,T]--> R

# Create the collection of HermitianMatrix
covariance = ot.CovarianceMatrix(N)
for k in range(N):
    s = tgrid.getValue(k)
    for l in range(k + 1):
        t = tgrid.getValue(l)
        covariance[k, l] = C(s, t)

covmodel = ot.UserDefinedCovarianceModel(tgrid, covariance)

# %%
# Create the normal process with that covariance model
# based on the mesh tgrid
process = ot.GaussianProcess(covmodel, tgrid)

# Get a sample of fields from the process
N = 1000
sample = process.getSample(N)

# %%
# The covariance model factory
factory = ot.NonStationaryCovarianceModelFactory()

# Estimation on a sample