# [Stheno](https://github.com/wesselb/stheno)
[![CI](https://github.com/wesselb/stheno/workflows/CI/badge.svg?branch=master)](https://github.com/wesselb/stheno/actions?query=workflow%3ACI)
[![Coverage Status](https://coveralls.io/repos/github/wesselb/stheno/badge.svg?branch=master)](https://coveralls.io/github/wesselb/stheno?branch=master)
[![Latest Docs](https://img.shields.io/badge/docs-latest-blue.svg)](https://wesselb.github.io/stheno)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
Stheno is an implementation of Gaussian process modelling in Python. See
also [Stheno.jl](https://github.com/willtebbutt/Stheno.jl).
[Check out our post about linear models with Stheno and JAX.](https://wesselb.github.io/2021/01/19/linear-models-with-stheno-and-jax.html)
Contents:
* [Nonlinear Regression in 20 Seconds](#nonlinear-regression-in-20-seconds)
* [Installation](#installation)
* [Manual](#manual)
- [AutoGrad, TensorFlow, PyTorch, or JAX? Your Choice!](#autograd-tensorflow-pytorch-or-jax-your-choice)
- [Model Design](#model-design)
- [Finite-Dimensional Distributions](#finite-dimensional-distributions)
- [Prior and Posterior Measures](#prior-and-posterior-measures)
- [Inducing Points](#inducing-points)
- [Kernels and Means](#kernels-and-means)
- [Important Remarks](#important-remarks)
* [Examples](#examples)
- [Simple Regression](#simple-regression)
- [Decomposition of Prediction](#decomposition-of-prediction)
- [Learn a Function, Incorporating Prior Knowledge About Its Form](#learn-a-function-incorporating-prior-knowledge-about-its-form)
- [Multi-Output Regression](#multi-output-regression)
- [Approximate Integration](#approximate-integration)
- [Bayesian Linear Regression](#bayesian-linear-regression)
- [GPAR](#gpar)
- [A GP-RNN Model](#a-gp-rnn-model)
- [Approximate Multiplication Between GPs](#approximate-multiplication-between-gps)
- [Sparse Regression](#sparse-regression)
- [Smoothing with Nonparametric Basis Functions](#smoothing-with-nonparametric-basis-functions)
**Breaking change:**
`f(x).marginals()` now returns the marginal means and variances, rather than the
marginal means and lower and upper 95% central credible region bounds.
For the credible bounds, use `f(x).marginal_credible_bounds()` instead.
## Nonlinear Regression in 20 Seconds
```python
>>> import numpy as np
>>> from stheno import GP, EQ
>>> x = np.linspace(0, 2, 10) # Some points to predict at
>>> y = x ** 2 # Some observations
>>> f = GP(EQ()) # Construct Gaussian process.
>>> f_post = f | (f(x), y) # Compute the posterior.
>>> pred = f_post(np.array([1, 2, 3])) # Predict!
>>> pred.mean
<dense matrix: shape=3x1, dtype=float64
mat=[[1. ]
[4. ]
[8.483]]>
>>> pred.var
<dense matrix: shape=3x3, dtype=float64
mat=[[ 8.032e-13 7.772e-16 -4.577e-09]
[ 7.772e-16 9.999e-13 2.773e-10]
[-4.577e-09 2.773e-10 3.313e-03]]>
```
[These custom matrix types are there to accelerate the underlying linear algebra.](#important-remarks)
To get vanilla NumPy/AutoGrad/TensorFlow/PyTorch/JAX arrays, use `B.dense`:
```python
>>> from lab import B
>>> B.dense(pred.mean)
array([[1.00000068],
[3.99999999],
[8.4825932 ]])
>>> B.dense(pred.var)
array([[ 8.03246358e-13, 7.77156117e-16, -4.57690943e-09],
[ 7.77156117e-16, 9.99866856e-13, 2.77333267e-10],
[-4.57690943e-09, 2.77333267e-10, 3.31283378e-03]])
```
Moar?! Then read on!
## Installation
See [the instructions here](https://gist.github.com/wesselb/4b44bf87f3789425f96e26c4308d0adc).
Then simply
```
pip install stheno
```
## Manual
Note: [here](https://wesselb.github.io/stheno) is a nicely rendered and more
readable version of the docs.
### AutoGrad, TensorFlow, PyTorch, or JAX? Your Choice!
```python
from stheno.autograd import GP, EQ
```
```python
from stheno.tensorflow import GP, EQ
```
```python
from stheno.torch import GP, EQ
```
```python
from stheno.jax import GP, EQ
```
### Model Design
The basic building block is a `f = GP(mean=0, kernel, measure=prior)`, which takes
in [a _mean_, a _kernel_](#kernels-and-means), and a _measure_.
The mean and kernel of a GP can be extracted with `f.mean` and `f.kernel`.
The measure should be thought of as a big joint distribution that assigns a mean and
a kernel to every variable `f`.
A measure can be created with `prior = Measure()`.
A GP `f` can have different means and kernels under different measures.
For example, under some _prior_ measure, `f` can have an `EQ()` kernel; but, under some
_posterior_ measure, `f` has a kernel that is determined by the posterior distribution
of a GP.
[We will see later how posterior measures can be constructed.](#prior-and-posterior-measures)
The measure with which a `f = GP(kernel, measure=prior)` is constructed can be
extracted with `f.measure == prior`.
If the keyword argument `measure` is not set, then automatically a new measure is
created, which afterwards can be extracted with `f.measure`.
Definition, where `prior = Measure()`:
```python
f = GP(kernel)
f = GP(mean, kernel)
f = GP(kernel, measure=prior)
f = GP(mean, kernel, measure=prior)
```
GPs that are associated to the same measure can be combined into new GPs, which is
the primary mechanism used to build cool models.
Here's an example model:
```python
>>> prior = Measure()
>>> f1 = GP(lambda x: x ** 2, EQ(), measure=prior)
>>> f1
GP(<lambda>, EQ())
>>> f2 = GP(Linear(), measure=prior)
>>> f2
GP(0, Linear())
>>> f_sum = f1 + f2
>>> f_sum
GP(<lambda>, EQ() + Linear())
>>> f_sum + GP(EQ()) # Not valid: `GP(EQ())` belongs to a new measure!
AssertionError: Processes GP(<lambda>, EQ() + Linear()) and GP(0, EQ()) are associated to different measures.
```
To avoid setting the keyword `measure` for every `GP` that you create, you can enter
a measure as a context:
```python
>>> with Measure() as prior:
f1 = GP(lambda x: x ** 2, EQ())
f2 = GP(Linear())
f_sum = f1 + f2
>>> prior == f1.measure == f2.measure == f_sum.measure
True
```
#### Compositional Design
* Add and subtract GPs and other objects.
Example:
```python
>>> GP(EQ(), measure=prior) + GP(Exp(), measure=prior)
GP(0, EQ() + Exp())
>>> GP(EQ(), measure=prior) + GP(EQ(), measure=prior)
GP(0, 2 * EQ())
>>> GP(EQ()) + 1
GP(1, EQ())
>>> GP(EQ()) + 0
GP(0, EQ())
>>> GP(EQ()) + (lambda x: x ** 2)
GP(<lambda>, EQ())
>>> GP(2, EQ(), measure=prior) - GP(1, EQ(), measure=prior)
GP(1, 2 * EQ())
```
* Multiply GPs and other objects.
*Warning:*
The product of two GPs it *not* a Gaussian process.
Stheno approximates the resulting process by moment matching.
Example:
```python
>>> GP(1, EQ(), measure=prior) * GP(1, Exp(), measure=prior)
GP(<lambda> + <lambda> + -1 * 1, <lambda> * Exp() + <lambda> * EQ() + EQ() * Exp())
>>> 2 * GP(EQ())
GP(2, 2 * EQ())
>>> 0 * GP(EQ())
GP(0, 0)
>>> (lambda x: x) * GP(EQ())
GP(0, <lambda> * EQ())
```
* Shift GPs.
Example:
```python
>>> GP(EQ()).shift(1)
GP(0, EQ() shift 1)
```
* Stretch GPs.
Example:
```python
>>> GP(EQ()).stretch(2)
GP(0, EQ() > 2)
```
* Select particular input dimensions.
Example:
```python
>>> GP(EQ()).select(1, 3)
GP(0, EQ() : [1, 3])
```
* Transform the input.
Example:
```python
>>> GP(EQ()).transform(f)
GP(0, EQ() transform f)
```
* Numerically take the derivative of a GP.
The argument specifies which dimension to take the derivative with respect
to.
Example:
```python
>>> GP(EQ()).diff(1)
GP(0, d(1) EQ())
```
* Construct a finite difference estimate of the derivativ
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共43个文件
py:32个
txt:5个
pkg-info:2个
资源分类:Python库 所属语言:Python 资源全名:stheno-1.1.3.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
收起资源包目录
stheno-1.1.3.tar.gz (43个子文件)
stheno-1.1.3
MANIFEST.in 22B
PKG-INFO 51KB
pyproject.toml 93B
stheno
autograd.py 119B
lazy.py 4KB
model
fdd.py 3KB
__init__.py 88B
gp.py 8KB
measure.py 13KB
observations.py 10KB
__init__.py 486B
tensorflow.py 121B
random.py 9KB
jax.py 114B
torch.py 116B
mo
mean.py 1KB
__init__.py 63B
input.py 2KB
kernel.py 3KB
stheno.egg-info
PKG-INFO 51KB
requires.txt 108B
SOURCES.txt 871B
top_level.txt 13B
dependency_links.txt 1B
tests
test_random.py 6KB
model
util.py 378B
test_fdd.py 3KB
test_observations.py 1KB
__init__.py 0B
test_model.py 13KB
test_cases.py 3KB
test_gp.py 5KB
util.py 1KB
__init__.py 669B
test_lazy.py 5KB
mo
test_kernel.py 3KB
__init__.py 0B
test_input.py 1KB
test_mean.py 760B
LICENCE.txt 1KB
setup.cfg 371B
setup.py 379B
README.md 39KB
共 43 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功