相同的代码在julia中的结果不同

时间:2018-10-31 04:08:09

标签: julia

我有两个代码:

1)第一个代码:

Vp = EVw[:,:,1]
V0 = EV0

Va_test, Ca_test, Sa_test, Ta_test, EVa_test, Ap_test = [zeros(P.Na,P.Nh) for i in 1:6]
# interpolators
Vp_itp = interpolate((G.a, G.h), Vp, Gridded(Linear()))
V0_itp = interpolate((G.a,), V0, Gridded(Linear()))
# initialize state
a0 = G.a[1]
h0 = G.h[1]
# value function and budget constraints
VF(c,s,ak) = ValueFunctionTransfer(c,s,ak,a0,h0,r,w,Vp_itp,V0_itp,P,θ0)
BC(s) = BudgetConstraint(s,a0,h0,r,w,P)

# solve
for i in 1:P.Na, j in 1:P.Nh
# state
a0 = G.a[i]
h0 = G.h[j]
# solve
Ca_test[i,j], Sa_test[i,j], Ta_test[i,j], Va_test[i,j], Ap_test[i,j] = NestedGolden3D(VF,BC)
end

2)第二个代码:

Vp = EVw[:,:,1]
V0 = EV0

Va, Ca, Sa, Ta, EVa, Ap = [zeros(P.Na,P.Nh) for i in 1:6]
# interpolators
Vp_itp = interpolate((G.a, G.h), Vp, Gridded(Linear()))
V0_itp = interpolate((G.a,), V0, Gridded(Linear()))
# initialize state
a0 = G.a[1]
h0 = G.h[1]
# value function and budget constraints
VF(c,s,ak) = ValueFunctionTransfer(c,s,ak,a0,h0,r,w,Vp_itp,V0_itp,P,θ0)
BC(s) = BudgetConstraint(s,a0,h0,w,r,P)

# solve
for i in 1:P.Na, j in 1:P.Nh
# state
a0 = G.a[i]
h0 = G.h[j]
# solve
Ca[i,j], Sa[i,j], Ta[i,j], Va[i,j], Ap[i,j] = NestedGolden3D(VF, BC)
end

两个版本在我看来都是一样的,但它们提供了不同的结果!!可能会发生什么?

我在函数中有第二个代码,但对结果不满意。然后,我在不同的脚本中创建了第一个代码,并获得了不错的结果。我意识到两个版本本质上是相同的,然后我使它们看起来相同(名称中的_test除外)。

在我看来,它们看起来完全一样,但是它们确实提供了不同的答案。我想引擎盖下正在发生某些事情,我听不懂。

要确保我正确运行代码,请先运行所需的部分(未显示),然后运行其中一个版本,保存结果,然后重新启动julia。然后,我对其他版本也是如此。然后,我再次重新启动julia,然后比较结果。它们不一样。我发现这真令人困惑。

1 个答案:

答案 0 :(得分:1)

两个示例之间是有区别的。

在第一个示例中,您具有: BC(s) = BudgetConstraint(s,a0,h0,r,w,P) 在第二个示例中,您具有: BC(s) = BudgetConstraint(s,a0,h0,w,r,P)

相关问题