{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create a custom covariance model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this basic example we are going to build a custom covariance model from elementary covariance matrices."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import openturns as ot\n",
    "import math as m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Create the covariance function at (s,t):\n",
    "def C(s, t):\n",
    "    return m.exp( -4.0 * abs(s - t) / (1 + (s * s + t * t)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Create the time grid:\n",
    "N = 32\n",
    "a = 4.0\n",
    "myMesh = ot.IntervalMesher([N]).build(ot.Interval(-a, a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Create the collection of elementary covariance matrices\n",
    "myCovarianceCollection = ot.CovarianceMatrixCollection()\n",
    "for k in range(myMesh.getVerticesNumber()):\n",
    "    t = myMesh.getVertices()[k]\n",
    "    for l in range(k + 1):\n",
    "        s = myMesh.getVertices()[l]\n",
    "        matrix = ot.CovarianceMatrix(1)\n",
    "        matrix[0, 0] = C(s[0], t[0])\n",
    "        myCovarianceCollection.add(matrix)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Create the covariance model\n",
    "myCovarianceModel = ot.UserDefinedCovarianceModel(myMesh, myCovarianceCollection)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
