我在该类中编写了一个名为BiVAR
的类,我有一个名为ResiPlot
的函数,用于绘制一个数字。但是,在该函数中,我定义了一个称为self.resi
的变量。在那个特定的类中,我有另一个名为hzTest
的函数。在这个函数中,我对函数ResiPlot
的图或打印不感兴趣。我所需要的只是确保self.resi
已被定义。
因此,我实际需要的是如何在函数self.resi
中定义hzTest
。即调用函数ResiPlot
来抑制其数字或任何打印输出。
我的代码:
from statsmodels.tsa.api import VAR
import matplotlib.pyplot as plt, subprocess
# Calling built-in functions from R
Rsummary = robjects.r['summary']
class BiVAR:
def __init__(self, df, restrict=0): # Initialize when created
self.data = np.array(df.values, dtype=float) # self is the new object
self.isrestricted = restrict
if self.isrestricted ==0:
self.Model= VAR(self.data)
else:
p = int(input("Since, you want a restricted model please enter the lag p: "))
self.p=p
if p==0: p=1
t= Rvars.VAR(self.data, p, type='const')
self.Model= Rvars.restrict(t,method = "ser")
def BestLagAic(self):
if self.isrestricted==1:
print('Sorry this can not be excuted since you chose the model to be restricted')
else:
R=self.Model.select_order(15)
return R['aic'] # Split string on blanks
def Fit(self, *parameters, **keyword_parameters):
# This function allows you to specify the lag variable. If not specified it will use the p value you previously
# give it for the restricted VAR model otherwise it will use the best lag based on AIC
if self.isrestricted ==0:
if len(parameters)==1:
p=parameters[0]
results = self.Model.fit(p)
print(results.summary())
elif len(parameters)==0:
p=self.BestLagAic()
results = self.Model.fit(p)
print(results.summary())
else:
print('You included so many unrequired variables')
else:
p=self.p
t= Rvars.VAR(self.data, p, type='const')
t1= Rvars.restrict(t, method = "ser")
H=str(Rsummary(t1))
start = H.find('VAR Estimation Results:') + 23
end = H.find('Roots of the characteristic', start)
pvalue=H[start:end]
start1 = H.find('Estimation results for equation') + 31
pvalue1=H[start1::]
print(pvalue+pvalue1)
def ResiPlot(self, *parameters):
# This function plots the residuals when fitted with a VAR(p) model
if self.isrestricted ==0:
if len(parameters)==1:
p=parameters[0]
results = self.Model.fit(p)
resi=results.resid
self.resi=pd.DataFrame(resi, columns=['Bond-Resi','Equity-Resi'])
pd.DataFrame(resi).plot()
plt.show()
elif len(parameters)==0:
p=self.BestLagAic()
results = self.Model.fit(p)
resi=results.resid
self.resi=pd.DataFrame(resi, columns=['Bond-Resi','Equity-Resi'])
pd.DataFrame(resi).plot()
plt.show()
else:
print('You included so many unrequired variables')
else:
t= Rvars.VAR(self.data, self.p, type='const')
t1= Rvars.restrict(t, method = "ser")
t2=t1.rx2('varresult').rx2('y1').rx2('residuals')
t3=t1.rx2('varresult').rx2('y2').rx2('residuals')
resi=pd.DataFrame(np.column_stack((np.array(t2), np.array(t3))), columns=['Bond-Resi','Equity-Resi'])
self.resi=resi
pd.DataFrame(resi).plot()
plt.show()
def hzTest(self):
print('This is the Henze-Zirkler Multivariate Normality test applied on the residuals of the fitted model')
subprocess.call('self.ResiPlot')
MVNresult =MVN.hzTest(self.resi, qqplot = 0)
np.array(MVNresult.slots[tuple(MVNresult.slotnames())[1]])[0]
答案 0 :(得分:0)
您需要(简单版本)multitier architecture。没有计算事物的功能也会询问用户他们的输入并绘制他们的结果。那你就不必“压制其他功能的输出”;你只需在不同的上下文中调用计算函数,其中一些从中产生用户可见的输出,而其中一些不产生。