Python 为什么在计算两条曲线之间的面积时,在下面的数值积分中不进行累积和?
说明: 在下面的python代码中,我将生成一个高斯PDF,即p(y)。我试图通过矩形求和的方法,找到[min_p,max_p]范围内曲线和任何水平线之间的区域。我的主要问题在于函数的实现,该函数应该为p数组中的任何特定元素(如代码中定义的)迭代计算该区域,并将其绘制为上述范围内给定值的函数 问题: 代码运行良好,但不会生成所需的单调递增/递减函数Python 为什么在计算两条曲线之间的面积时,在下面的数值积分中不进行累积和?,python,arrays,for-loop,numpy,sympy,Python,Arrays,For Loop,Numpy,Sympy,说明: 在下面的python代码中,我将生成一个高斯PDF,即p(y)。我试图通过矩形求和的方法,找到[min_p,max_p]范围内曲线和任何水平线之间的区域。我的主要问题在于函数的实现,该函数应该为p数组中的任何特定元素(如代码中定义的)迭代计算该区域,并将其绘制为上述范围内给定值的函数 问题: 代码运行良好,但不会生成所需的单调递增/递减函数 import matplotlib from scipy.stats import norm from scipy import integrate
import matplotlib
from scipy.stats import norm
from scipy import integrate
import matplotlib.pyplot as pyplot
import numpy as np
N = 10 # Number of sigmas away from central value
M, K = 2**10, 2**10 # Number of grid points along y and p(y)
mean, sigma = 10.0, 1.0 #Mean value and standard deviation of a Gaussian probability distribuiton (PDF)
ymin, ymax = -N*sigma+mean, N*sigma+mean #Minimum and maximum and spacing between grid points along y-axis
ylims = [ymin, ymax]
y = np.linspace(ylims[0],ylims[1],M) #The values of y-axis on grid points
pdf = norm.pdf(y,loc=mean,scale=sigma) # Definiton of the normalized probability distribuiton function (PDF)
min_p , max_p = min(pdf), max(pdf) #The maximum, minimum value of p(y)
p = np.linspace(min_p, max_p, K) #The values of p(y)-axis on grid points
def Area(p): #Calculating the area under the PDF for which probability is more than pj-value for a particular jth index in pj_array above
Areas = []
for yi in y.flat:
for p_j in p.flat:
Area = 0.0
delta_y = (ymax - ymin) / (M-1) #The spacing beteen grid points along y-axis
if (pdf[yi] > p_j): Area += (delta_y * pdf.sum())
else: Area += 0.0
Areas.append(Area)
return Areas
pyplot.plot(p, Area(p), '-')
pyplot.axis([min_p, max_p, 0, 1])
pyplot.show()
在做了很多更改之后,我认为您正在尝试生成以下代码:
from scipy.stats import norm
import numpy as np
import pylab as p
%matplotlib inline
N = 10 # Number of sigmas away from central value
M, K = 2**10, 2**10 # Number of grid points along x and y
mean, sigma = 10.0, 1.0 #Mean value and standard deviation of a Gaussian probability distribuiton (PDF)
xmin, xmax = -N*sigma+mean, N*sigma+mean #Minimum and maximum and spacing between grid points along y-axis
xx = np.linspace(xmin,xmax,M) #The values of x-axis on grid points
pdf = norm.pdf(xx,loc=mean,scale=sigma ) # Definiton of the normalized probability distribuiton function (PDF)
min_p , max_p = min(pdf), max(pdf)
pp = np.linspace(min_p,max_p, K) #The values of y-axis on grid points
delta_x = xx[1]-xx[0] # spacing beteen grid points along x-axis
delta_p = pp[1]-pp[0] # spacing between grid points on y-axis
def Area(p): #Calculating the area under the PDF for which probability is more than pj-value for a particular jth index in pj_array above
areas = []
area=0
for i in range(nx):
for pi in pp :
if (pi< pdf[i]):
area += delta_x * delta_p
areas.append(area)
return areas
p.subplot(121)
p.plot(xx,pdf)
p.subplot(122)
p.plot(xx, Area(p), '-')
要在任何给定的p=const行上进行积分,您只需从
pdf[i]
中减去该固定值,并可能根据您在特定上下文中对积分的含义添加一个条件(减去负数或忽略负数) 打印区域(p)
,您将看到它不是您声称的1D数组是。我只是注意到了。我想知道为什么它没有更新p的任何元素。你会建议我编辑这个问题吗?你的问题现在很难读。你的回溯是混乱的,你有一个巨大的段落在顶部感谢roadrunner的澄清。现在,至少我知道代码出了什么问题。但是,我仍然没有得到这个函数。对于p_j,假设它是一个单调递减函数,其最大值为1,最小值在[0,1]范围内为0。所以,这就是为什么我编辑了这个问题,使它比以前更好地描述了这个问题。再次感谢,
from scipy.stats import norm
import numpy as np
import pylab as p
%matplotlib inline
N = 10 # Number of sigmas away from central value
M, K = 2**10, 2**10 # Number of grid points along x and y
mean, sigma = 10.0, 1.0 #Mean value and standard deviation of a Gaussian probability distribuiton (PDF)
xmin, xmax = -N*sigma+mean, N*sigma+mean #Minimum and maximum and spacing between grid points along y-axis
xx = np.linspace(xmin,xmax,M) #The values of x-axis on grid points
pdf = norm.pdf(xx,loc=mean,scale=sigma ) # Definiton of the normalized probability distribuiton function (PDF)
delta_x = xx[1]-xx[0] # spacing between grid points along x-axis
def Area(p): #Calculating the area under the PDF for which probability is more than pj-value for a particular jth index in pj_array above
areas = []
area=0
for i in range(nx):
area += delta_x * pdf[i]
areas.append(area)
return areas
p.subplot(121)
p.plot(xx,pdf)
p.subplot(122)
p.plot(xx, Area(p), '-')