您的位置:

高斯分布相乘

一、两个高斯分布相乘的方差

两个高斯分布相乘后,新的分布的方差小于或等于原来两个分布的方差之和。这是因为两个分布相乘的最大值发生在它们的平均值处,而平均值总是比最大值小。下面是Python代码示例:

import numpy as np

def multivariate_gaussian(x, mean, covariance):
    numerator = np.exp(-0.5 * ((x-mean).T).dot(np.linalg.inv(covariance)).dot(x-mean))
    denominator = (2*np.pi)**(len(mean)/2) * np.sqrt(np.linalg.det(covariance))
    return numerator/denominator

mean1 = np.array([1,1])
mean2 = np.array([-1,-1])
covariance1 = np.array([[1,0],[0,1]])
covariance2 = np.array([[2,0],[0,2]])

def multiply_gaussians(mean1, covariance1, mean2, covariance2):
    mean3 = (covariance2.dot(np.linalg.inv(covariance1+covariance2)).dot(mean1) + covariance1.dot(np.linalg.inv(covariance1+covariance2)).dot(mean2))
    covariance3 = covariance1.dot(np.linalg.inv(covariance1+covariance2)).dot(covariance2)
    return mean3, covariance3

mean3, covariance3 = multiply_gaussians(mean1, covariance1, mean2, covariance2)

print("Mean of Gaussian 1: ", mean1)
print("Covariance of Gaussian 1: ", covariance1)
print("Mean of Gaussian 2: ", mean2)
print("Covariance of Gaussian 2: ", covariance2)
print("Mean of multiplied Gaussian: ", mean3)
print("Covariance of multiplied Gaussian: ", covariance3)

二、两个高斯分布相除

两个高斯分布相除得到的是一个新的分布。在一些情况下,比如贝叶斯推断中,需要使用两个高斯分布相除。下面是代码示例:

import numpy as np

def multivariate_gaussian(x, mean, covariance):
    numerator = np.exp(-0.5 * ((x-mean).T).dot(np.linalg.inv(covariance)).dot(x-mean))
    denominator = (2*np.pi)**(len(mean)/2) * np.sqrt(np.linalg.det(covariance))
    return numerator/denominator

mean1 = np.array([1,1])
mean2 = np.array([-1,-1])
covariance1 = np.array([[1,0],[0,1]])
covariance2 = np.array([[2,0],[0,2]])

def divide_gaussians(mean1, covariance1, mean2, covariance2):
    mean3 = mean1 - mean2
    covariance3 = covariance1 + covariance2
    return mean3, covariance3

mean3, covariance3 = divide_gaussians(mean1, covariance1, mean2, covariance2)

print("Mean of Gaussian 1: ", mean1)
print("Covariance of Gaussian 1: ", covariance1)
print("Mean of Gaussian 2: ", mean2)
print("Covariance of Gaussian 2: ", covariance2)
print("Mean of divided Gaussian: ", mean3)
print("Covariance of divided Gaussian: ", covariance3)

三、两个高斯分布相乘方差变小

两个高斯分布相乘的方差比两个高斯分布中小的方差和大的方差更小。下面是Python代码示例:

import numpy as np

def multivariate_gaussian(x, mean, covariance):
    numerator = np.exp(-0.5 * ((x-mean).T).dot(np.linalg.inv(covariance)).dot(x-mean))
    denominator = (2*np.pi)**(len(mean)/2) * np.sqrt(np.linalg.det(covariance))
    return numerator/denominator

mean1 = np.array([0,0])
mean2 = np.array([0,0])
covariance1 = np.array([[1,0],[0,2]])
covariance2 = np.array([[2,0],[0,1]])

def multiply_gaussians(mean1, covariance1, mean2, covariance2):
    mean3 = (covariance2.dot(np.linalg.inv(covariance1+covariance2)).dot(mean1) +
             covariance1.dot(np.linalg.inv(covariance1+covariance2)).dot(mean2))
    covariance3 = covariance1.dot(np.linalg.inv(covariance1+covariance2)).dot(covariance2)
    return mean3, covariance3

mean3, covariance3 = multiply_gaussians(mean1, covariance1, mean2, covariance2)

print("Mean of Gaussian 1: ", mean1)
print("Covariance of Gaussian 1: ", covariance1)
print("Mean of Gaussian 2: ", mean2)
print("Covariance of Gaussian 2: ", covariance2)
print("Mean of multiplied Gaussian: ", mean3)
print("Covariance of multiplied Gaussian: ", covariance3)

四、两个高斯分布的乘积

两个高斯分布相乘得到的是一个新的高斯分布。下面是Python代码示例:

import numpy as np

def multivariate_gaussian(x, mean, covariance):
    numerator = np.exp(-0.5 * ((x-mean).T).dot(np.linalg.inv(covariance)).dot(x-mean))
    denominator = (2*np.pi)**(len(mean)/2) * np.sqrt(np.linalg.det(covariance))
    return numerator/denominator

mean1 = np.array([1,1])
mean2 = np.array([-1,-1])
covariance1 = np.array([[1,0],[0,1]])
covariance2 = np.array([[2,0],[0,2]])

def multiply_gaussians(mean1, covariance1, mean2, covariance2):
    mean3 = (covariance2.dot(np.linalg.inv(covariance1+covariance2)).dot(mean1) +
             covariance1.dot(np.linalg.inv(covariance1+covariance2)).dot(mean2))
    covariance3 = covariance1.dot(np.linalg.inv(covariance1+covariance2)).dot(covariance2)
    return mean3, covariance3

mean3, covariance3 = multiply_gaussians(mean1, covariance1, mean2, covariance2)

def gaussian_product(x, mean1, covariance1, mean2, covariance2):
    mean3, covariance3 = multiply_gaussians(mean1, covariance1, mean2, covariance2)
    return multivariate_gaussian(x, mean3, covariance3)

x = np.array([1,-1])
p = gaussian_product(x, mean1, covariance1, mean2, covariance2)

print("Mean of Gaussian 1: ", mean1)
print("Covariance of Gaussian 1: ", covariance1)
print("Mean of Gaussian 2: ", mean2)
print("Covariance of Gaussian 2: ", covariance2)
print("Mean of multiplied Gaussian: ", mean3)
print("Covariance of multiplied Gaussian: ", covariance3)
print("Product of Gaussians: ", p)

五、两个独立正态分布相乘

两个独立正态分布相乘得到的是一个新的独立正态分布。下面是Python代码示例:

import numpy as np
import matplotlib.pyplot as plt

def normal_pdf(x, mean, variance):
    return 1/np.sqrt(2*np.pi*variance) * np.exp(-0.5 * (x-mean)**2/variance)

mean1 = 0
variance1 = 1
mean2 = 0
variance2 = 2

x = np.linspace(-10, 10, 1000)
y1 = normal_pdf(x, mean1, variance1)
y2 = normal_pdf(x, mean2, variance2)
y3 = y1 * y2

plt.plot(x, y1, label="Gaussian 1")
plt.plot(x, y2, label="Gaussian 2")
plt.plot(x, y3, label="Product of Gaussians")
plt.legend()
plt.show()

六、两个正态分布相乘公式

两个正态分布相乘得到的新的分布的均值和方差可以通过以下公式进行计算:

均值:mean3 = (covariance2*mean1 + covariance1*mean2) / (covariance1+covariance2)

方差:covariance3 = covariance1 * covariance2 / (covariance1+covariance2)

七、多维高斯分布相乘

对于多维高斯分布相乘,可以使用和二维高斯分布相乘相同的公式进行计算,只需要将均值向量和协方差矩阵扩展到多维即可。下面是Python代码示例:

import numpy as np

def multivariate_gaussian(x, mean, covariance):
    numerator = np.exp(-0.5 * ((x-mean).T).dot(np.linalg.inv(covariance)).dot(x-mean))
    denominator = (2*np.pi)**(len(mean)/2) * np.sqrt(np.linalg.det(covariance))
    return numerator/denominator

mean1 = np.array([1,2,3])
mean2 = np.array([4,5,6])
covariance1 = np.array([[1,0,0],[0,2,0],[0,0,3]])
covariance2 = np.array([[4,0,0],[0,5,0],[0,0,6]])

def multiply_gaussians(mean1, covariance1, mean2, covariance2):
    mean3 = (covariance2.dot(np.linalg.inv(covariance1+covariance2)).dot(mean1) + 
             covariance1.dot(np.linalg.inv(covariance1+covariance2)).dot(mean2))
    covariance3 = covariance1.dot(np.linalg.inv(covariance1+covariance2)).dot(covariance2)
    return mean3, covariance3

mean3, covariance3 = multiply_gaussians(mean1, covariance1, mean2, covariance2)

x = np.array([1,2,3])
p = multivariate_gaussian(x, mean3, covariance3)

print("Mean of Gaussian 1: ", mean1)
print("Covariance of Gaussian 1: ", covariance1)
print("Mean of Gaussian 2: ", mean2)
print("Covariance of Gaussian 2: ", covariance2)
print("Mean of multiplied Gaussian: ", mean3)
print("Covariance of multiplied Gaussian: ", covariance3)
print("Value of multiplied Gaussian at x: ", p)

八、高斯分布相乘还是高斯分布吗

两个高斯分布相乘得到的是一个新的高斯分布,只是均值和方差有所变化。下面是Python代码示例:

import numpy as np
import matplotlib.pyplot as plt

def multivariate_gaussian(x, mean, covariance):
    numerator = np.exp(-0.5 * ((x-mean).T).dot(np.linalg.inv(covariance)).dot(x-mean))
    denominator = (2*np.pi)**(len(mean)/2) * np.sqrt(np.linalg.det(covariance))
    return numerator/denominator

mean1 = np.array([1,1])
mean2 = np.array([-1,-1])
covariance1 = np.array([[1,0],[0,1]])
covariance2 = np.array([[2,0],[0,2]])

def multiply_gaussians(mean1, covariance1, mean2, covariance2):
    mean3 = (covariance2.dot(np.linalg.inv(covariance1+covariance2)).dot(mean1) +
             covariance1.dot(np.linalg.inv(covariance1+covariance2)).dot(mean2))
    covariance3 = covariance1.dot(np.linalg.inv(covariance1+covariance2)).dot(covariance2)
    return mean3, covariance3

mean3, covariance3 = multiply_gaussians(mean1, covariance1, mean2, covariance2)

x = np.linspace(-5, 5, 1000)
y1 = multivariate_gaussian(np.array([x,x]).T, mean1, covariance1)
y2 = multivariate_gaussian(np.array([x,x]).T, mean2, covariance2)
y3 = multivariate_gaussian(np.array([x,x]).T, mean3, covariance3)

plt.plot(x, y1, label="Gaussian 1")
plt.plot(x, y2, label="Gaussian 2")
plt.plot(x, y3, label="Product of Gaussians")
plt.legend()
plt.show()