File:Gaussianprocess gapUncertainty.gif

From testwiki
Jump to navigation Jump to search
Gaussianprocess_gapUncertainty.gif (400 × 200 pixels, file size: 156 KB, MIME type: image/gif, looped, 50 frames, 5.0 s)

This file is from Wikimedia Commons and may be used by other projects. The description on its file description page there is shown below.

Summary

Description
English: Gaußprozess-Regression: Unsicherheit der Interpolation einer Lücke, dargestellt durch Zufallsfluktiononen gemäß der a-posteriori-Kovarianzfunktion.
Date
Source Own work
Author Physikinger
GIF development
InfoField
 This plot was created with Matplotlib.
Source code
InfoField

Python code

# This source code is public domain 
# Author: Christian Schirm

import numpy, scipy.spatial
import matplotlib.pyplot as plt
import imageio

def covMat(x1, x2, covFunc, noise=0):  # Covariance matrix
    cov = covFunc(scipy.spatial.distance_matrix(numpy.atleast_2d(x1).T, numpy.atleast_2d(x2).T))
    if noise: cov += numpy.diag(numpy.ones(len(cov))*noise)
    return cov

numpy.random.seed(107)

covFunc1 = lambda d: 2*numpy.exp(-numpy.abs(numpy.sin(1.55*numpy.pi*d))**1.9/3 - d**2/7.)
covFunc2 = lambda d: 1*numpy.exp( - d**2/6.)
covFunc = lambda d: 1.5*numpy.exp(-numpy.abs(numpy.sin(1.55*numpy.pi*d))**1.9/3 - d**2/10.)

n=60
x = numpy.linspace(0, 10, 300)
y1 = numpy.random.multivariate_normal(x.ravel()*0, covMat(x, x, covFunc1, noise=0.00))
y2 = numpy.random.multivariate_normal(x.ravel()*0, covMat(x, x, covFunc2, noise=0.00))
x_known = numpy.concatenate([x[:n+1], x[-n:]])
y_known = numpy.concatenate([y1[:n+1], y2[-n:]])
x_unknown = x[n:-n+1]

Ckk = covMat(x_known, x_known, covFunc, noise=0.000001)
Cuu = covMat(x_unknown, x_unknown, covFunc, noise=0.00)
CkkInv = numpy.linalg.inv(Ckk)
Cuk = covMat(x_unknown, x_known, covFunc, noise=0.0)
m = 0 #numpy.mean(y)
covPost = Cuu - numpy.dot(numpy.dot(Cuk,CkkInv),Cuk.T)
y_unknown = numpy.dot(numpy.dot(Cuk,CkkInv),y_known)

fig = plt.figure(figsize=(4.0,2))
sigma = numpy.sqrt(numpy.diag(covPost))
plt.plot(x_unknown, y_unknown,  label=u'Prediction')
plt.fill_between(x_unknown.ravel(), y_unknown - sigma, y_unknown + sigma, color = '0.85')
plt.plot(x[:n+1], y1[:n+1],'k-')
plt.plot(x[-n:], y2[-n:],'k-')
plt.vlines([x[n], x[-n]],-3,3,colors='r', linestyles='--', alpha=0.5)
plt.axis([0,10,-3,3])
plt.savefig('Gaussianprocess_gapMean.svg')

fig = plt.figure(figsize=(4.0,2))
for c in 'C1 C4  C2'.split():
    y_random = numpy.random.multivariate_normal(x_unknown.ravel()*0, covPost)
    plt.plot(x_unknown, y_unknown + y_random, c, label=u'Prediction')
sigma = numpy.sqrt(numpy.diag(covPost))
plt.plot(x[:n+1], y1[:n+1],'k-')
plt.plot(x[-n:], y2[-n:],'k-')
plt.vlines([x[n], x[-n]],-3,3,colors='r', linestyles='--', alpha=0.5)
plt.axis([0,10,-3,3])
plt.savefig('Gaussianprocess_gap.svg')

# Uncertainty animation

numpy.random.seed(1)
t = numpy.arange(0, 1, 0.02)
covFunc = lambda d: numpy.exp(-(3*numpy.sin(d*numpy.pi))**2) # Covariance function
chol = numpy.linalg.cholesky(covMat(t, t, covFunc, noise=1E-5))
r = chol.dot(numpy.random.randn(len(t), len(covPost)))
cov = covPost+1E-5*numpy.identity(len(covPost))
rSmooth = numpy.linalg.cholesky(cov).dot(r.T)

images = []
fig = plt.figure(figsize=(4.0,2))
for ti in [0]+list(range(len(t))):
    plt.plot(x_unknown, y_unknown + rSmooth[:,ti], label=u'Prediction',alpha=1)
    #plt.fill_between(x_unknown.ravel(), y_unknown - sigma, y_unknown + sigma, color = '0.85')
    plt.plot(x[:n+1], y1[:n+1],'k-')
    plt.plot(x[-n:], y2[-n:],'k-')
    plt.vlines([x[n], x[-n]],-3,3,colors='r', linestyles='--', alpha=0.5)
    plt.axis([0,10,-3,3])
    plt.xlabel('t')
    #plt.tight_layout()
    fig.canvas.draw()
    s, (width, height) = fig.canvas.print_to_buffer()
    images.append(numpy.fromstring(s, numpy.uint8).reshape((height, width, 4)))
    fig.clf()

# Save GIF animation
fileOut = 'Gaussianprocess_gapUncertainty.gif'
imageio.mimsave(fileOut, images[1:])

# Optimize GIF size
from pygifsicle import optimize
optimize(fileOut, colors=16)

Licensing

I, the copyright holder of this work, hereby publish it under the following license:
Creative Commons CC-Zero This file is made available under the Creative Commons CC0 1.0 Universal Public Domain Dedication.
The person who associated a work with this deed has dedicated the work to the public domain by waiving all of their rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law. You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.

Captions

Gaussian process regression: uncertainty of interpolated gap shown by random animation according to posterior covariance function

Items portrayed in this file

depicts

1 December 2019

image/gif

159,918 byte

200 pixel

400 pixel

e0908e47bf343f2c1a72e43651cc14105d2df06b

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current21:37, 8 September 2021Thumbnail for version as of 21:37, 8 September 2021400 × 200 (156 KB)wikimediacommons>PhysikingerSmaller file size

The following page uses this file: