2017-03-02 5 views
0

私はこれをしばらくの間動作させようとしてきましたが、まだ方法を見つけることはできません。私は、区分的ガウス関数の先読み密度を計算しようとしています。私は区分的に正規分布した関数の定常分布を推定しようとしています。y> 0.0かつx -y> = - Q1:ValueError:複数の要素を持つ配列の真理値があいまいです。 a.any()またはa.all()を使用する

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)用:エラーの種類を回避する方法があります。次のコードに記載されている条件を確認しますか?

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() 

答えて

0

エラーがベクトル化機能pを期待する、quantecon.LAE(p, X)で始まります。あなたの関数はベクトル化されていないので、他のすべてが機能しません。あなたはベクトル化されたコードをいくつかコピーしましたが、多くのものがsympyスタイルの関数として残っています。これはnumpyの人々があなたが望むものについて混乱させた理由です。

この場合、「ベクター化」とは、長さがnの2つの1Dアレイを2D n x nアレイに変換することを意味します。この場合、return 0.0にしたくない場合は、return outの2d ndArrayに、の関数に基づくブールマスクがfalseの位置out[i,j]の値が0.0になるようにします。

あなたは放送でこれを行うことができます:

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 
1

サイドバーのValueError質問をすべて表示しますか?

このエラーは、boolean配列がifまたはor/andなどのスカラーブール値コンテキストで使用されている場合に発生します。

このテストでは、yまたはxを試してください。対話シェルで試してみてください。

if y>0.0 and x -y>=-Q1: .... 

if y>0: 

(y>0.0) and (x-y>=10) 

は、すべてのxyと、このエラーが生成されます。

また、わかりやすくするために質問を編集しました。

関連する問題