-
Notifications
You must be signed in to change notification settings - Fork 0
/
gaussian.py
35 lines (27 loc) · 1.02 KB
/
gaussian.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2017-11-10 14:52:46
# @Author : jingray (lionel_jing@163.com)
# @Link : http://www.jianshu.com/u/01fb0364467d
# @Version : $Id$
class Gaussian():
def __init__(self, mean, variance):
self.mu = mean
self.sigma2 = variance
def evaluate(self, x):
coefficient = 1.0 / sqrt(2.0 * pi *self.sigma2)
exponential = exp(-0.5 * (x-self.mu) ** 2 / self.sigma2)
return coefficient * exponential
def multiply(self, other):
# calculate new mean
denominator = self.sigma2 + other.sigma2
numerator = self.mu * other.sigma2 + other.mu * self.sigma2
new_mu = numerator / denominator
# calcuate new variance
new_var = 1.0 / ( (1.0 / self.sigma2) + (1.0 / other.sigma2) )
# generate new gaussian
return Gaussian(new_mu, new_var)
def add(self, other):
new_mu = self.mu + other.mu
new_sigma2 = self.sigma2 + other.sigma2
return Gaussian(new_mu, new_sigma2)