如果y> 0.0并且x -y> =-Q1:ValueError:具有多个元素的数组的真值不明确.使用a.any()或a.all() [英] if y>0.0 and x -y>=-Q1: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

查看:115
本文介绍了如果y> 0.0并且x -y> =-Q1:ValueError:具有多个元素的数组的真值不明确.使用a.any()或a.all()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使它工作一段时间,但仍未找到解决方法.我正在尝试计算分段高斯函数的前瞻估计密度.我正在尝试估计分段正态分布函数的平稳分布.有没有办法避免错误类型:

I have been trying to get this to work for a while now, but still not finding a way. I am trying to compute the Look ahead estimate density of a piecewise gaussian function. I'm trying to estimate the stationary distribution of a piecewise normally distributed function. is there a way to avoid the error type:

Error-type: the truth value of an array with more than one element is ambiguous. Use a.any() or a.all(). 

,例如y=np.linspace(-200.0,200.0,100)x = np,linspace(-200.0,200.0,100).然后按照下面的代码验证条件?

for instance y=np.linspace(-200.0,200.0,100) and x = np,linspace(-200.0,200.0,100). then verify the condition as stated in the code below?

import numpy as np
import sympy as sp
from numpy import exp,sqrt,pi
from sympy import Integral, log, exp, sqrt, pi
import math
import matplotlib.pyplot as plt 
import scipy.integrate
from scipy.special import erf
from scipy.stats import norm, gaussian_kde
from quantecon import LAE
from sympy.abc import q
#from sympy import symbols
#var('q')
#q= symbols('q')

## == Define parameters == #
mu=80
sigma=20
b=0.2
Q=80
Q1=Q*(1-b)
Q2=Q*(1+b)
d = (sigma*np.sqrt(2*np.pi))
phi = norm()
n = 500

#Phi(z) = 1/2[1 + erf(z/sqrt(2))].

def p(x, y):
   # x, y = np.array(x, dtype=float), np.array(y, dtype=float)
    Positive_RG = norm.pdf(x-y+Q1, mu, sigma)
    print('Positive_R = ', Positive_RG)
    Negative_RG = norm.pdf(x-y+Q2, mu, sigma) 
    print('Negative_RG = ', Negative_RG)
    pdf_0= (1/(2*math.sqrt(2*math.pi)))*(erf((x+Q2-mu)/(sigma*np.sqrt(2)))-erf((x+Q1-mu)/(sigma*np.sqrt(2))))
    Zero_RG =norm.pdf
    print('Zero_RG',Zero_RG)
    print ('y',y)
    if y>0.0 and x -y>=-Q1:
        #print('printA', Positive_RG)
        return Positive_RG
    elif y<0.0 and x -y>=-Q2:
        #print('printC', Negative_RG)
        return Negative_RG
    elif y==0.0 and x >=-Q1:
        #print('printB', Zero_RG)
        return Zero_RG
    return 0.0 


Z = phi.rvs(n)
X = np.empty(n)
for t in range(n-1):
    X[t+1] = X[t] + Z[t]
    #X[t+1] = np.abs(X[t]) + Z[t]
psi_est = LAE(p, X)
k_est = gaussian_kde(X)

fig, ax = plt.subplots(figsize=(10,7))
ys = np.linspace(-200.0, 200.0, 200)
ax.plot(ys, psi_est(ys), 'g-', lw=2, alpha=0.6, label='look ahead estimate')
ax.plot(ys, k_est(ys), 'k-', lw=2, alpha=0.6, label='kernel based estimate')
ax.legend(loc='upper left')
plt.show()

推荐答案

错误从quantecon.LAE(p, X)开始,该错误需要 vectorized 函数p.您的函数未向量化,这就是为什么其他所有功能都不起作用的原因.您复制了一些矢量化代码,但留下了很多东西作为sympy样式函数,这就是为什么麻木的人对您想要的东西感到困惑.

Error starts with quantecon.LAE(p, X), which expects a vectorized function p. Your function isn't vectorized, which is why everything else doesn't work. You copied some vectorized code, but left a lot of things as sympy style functions which is why the numpy folks were confused about what you wanted.

在这种情况下,向量化"表示将长度为n的两个1D数组转换为2D n x n数组.在这种情况下,您不想return 0.0,而是想return out在位置out[i,j]上基于值x[i], y[j]的布尔掩码为false的值为0.0的2d ndArray.

In this case "vectorized" means transforming two 1D arrays with length n into a 2D n x n array. In this case, you don't want to return 0.0, you want to return out a 2d ndArray which has the value 0.0 in locations out[i,j] where a boolean mask based on a function of x[i], y[j] is false.

您可以通过广播来做到这一点:

You can do this by broadcasting:

def sum_function(x,y):
    return x[:, None] + y[None, :]   # or however you want to add them, broadcasted to 2D

def myFilter(x,y):
    x, y = x.squeeze(), y.squeeze()
    out=np.zeros((x.size,y.size))
    xyDiff = x[:, None] - y[None, :]
    out=np.where(np.bitwise_and(y[None, :] => 0.0, xyDiff >= -Q1), sum_function(x, y), out) # unless the sum functions are different
    out=np.where(np.bitwise_and(y[None, :] < 0.0, xyDiff >= -Q2), sum_function(x, y), out)
    return out

这篇关于如果y&gt; 0.0并且x -y&gt; =-Q1:ValueError:具有多个元素的数组的真值不明确.使用a.any()或a.all()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆