简体   繁体   English

如何在Python中对具有2个变量且具有高斯分布的函数进行加权?

[英]How to weigh a function with 2 variables with a Gaussian distribution in python?

I've been working with this for the last days and I couldn't see yet where is the problem. 在过去的几天里,我一直在使用此工具,但我仍然看不出问题出在哪里。

I'm trying to weight a function with 2 variables f(q,r) within a Gaussian distribution g(r) with a specific mean value ( R0 ) and deviation ( sigma ). 我正在尝试在具有特定平均值( R0 )和偏差( sigma )的高斯分布g(r)内对具有2个变量f(q,r)的函数加权。 This is needed because the theoretical function f(q) has a certain dispersity in its r variable when analyzed experimentally. 这是必要的,因为理论函数f(q)在进行实验分析时在其r变量中具有一定的分散性。 Therefore, we use a probability density function to weigh our function in the r variable. 因此,我们使用概率密度函数来权衡r变量中的r

I include the code, which works, but doesn't give the expected result (the weighted curve should be smoother as the polydispersity grows (higher sigma ) as it is shown below. As you can see, I integrated the convolution of the 2 functions f(r,q)*g(r) from r = 0 to r = +inf . 我包含了可以正常工作的代码,但没有给出预期的结果(随着多分散度的增加,加权曲线应该更平滑(较高的sigma ),如下所示。如您所见,我整合了两个函数的卷积从r = 0r = +inf f(r,q)*g(r)

不同的聚酯

The result is plotted to compare the weigh result with the simple function: 绘制结果以将称量结果与简单功能进行比较:

from scipy.integrate import quad, quadrature
import numpy as np
import math as m
import matplotlib.pyplot as plt 

#function weighted with a probability density function (gaussian)
def integrand(r,q):
    #gaussian function normalized
    def gauss_nor(r):
        #gaussian function
        def gauss(r):
            return m.exp(-((r-R0)**2)/(2*sigma**2))
        return (m.exp(-((r-R0)**2)/(2*sigma**2)))/(quad(gauss,0,np.inf)[0])
    #function f(r,q)
    def f(r,q):
        return 3*(np.sin(q*r)-q*r*np.cos(q*r))/((r*q)**3)
    return gauss_nor(r)*f(r,q)

#quadratic integration of the integrand (from 0 to +inf)
#integrand is function*density_function (gauss) 
def function(q):
    return quad(integrand, 0, np.inf, args=(q))[0]

#parameters used in the function
R0=20
sigma=5

#range to plot q
q=np.arange(0.001,2.0,0.005)

#vector where the result of the integral will be saved
function_vec = np.vectorize(function)

#vector for the squared power of the integral
I=[]
I=(function_vec(q))**2

#function without density function
I0=[]
I0=(3*(np.sin(q*R0)-q*R0*np.cos(q*R0))/((R0*q)**3))**2

#plot of weighted and non-weighted functions
p1,=plt.plot(q,I,'b')
p3,=plt.plot(q,I0,'r')
plt.legend([p1,p3],('Weighted','No weighted'))
plt.yscale('log')
plt.xscale('log')
plt.show()

Thank you very much. 非常感谢你。 I've been with this problems for some days already and I haven't found the mistake. 我已经遇到这个问题好几天了,但没有发现错误。

Maybe somebody know how to weigh a function with a PDF in an easier way. 也许有人知道如何以一种更简单的方式来衡量PDF的功能。

I simplified your code, the output is the same as yours. 我简化了您的代码,其输出与您的代码相同。 I think it's already very smooth, there are some very sharp peak in the log-log graph, just because the curve has zero points. 我认为它已经非常平滑,对数-对数图中有一些非常尖锐的峰值,仅因为该曲线具有零点。 So it's not smooth in a log-log graph, but it's smooth in a normal XY graph. 因此,它在对数-对数图中不平滑,但在普通XY图中则平滑。

import numpy as np

def gauss(r):
    return np.exp(-((r-R0)**2)/(2*sigma**2))

def f(r,q):
    return 3*(np.sin(q*r)-q*r*np.cos(q*r))/((r*q)**3)

R0=20
sigma=5

qm, rm = np.ogrid[0.001:2.0:0.005, 0.001:40:1000j]
gr = gauss(rm)
gr /= np.sum(gr)
fm = f(rm, qm)
fm *= gr

plot(qm.ravel(), fm.sum(axis=1)**2)
plt.yscale('log')
plt.xscale('log')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM