"for" loop, collect()

Hello,

I have been using “for” loop to calculate Internal Rate of Return (irr) for a range of investment options (t below represents option t in array of data) and attempted the codes below:
for t in 1:nb
a(t) = irr([-Market_values[:,t];AssetsCF[:,t]])
b(t) = irr([-Market_values[:,t];PDCF_annual[:,t]])
c(t) = irr([-Market_values[:,t];FSCF_annual[:,t]])

IRR_MVCF = collect(a(t))
IRR_PDCF = collect(b(t))
IRR_FSCF = collect(c(t))

CSV.write(“irr_all.csv”,DataFrame(irr_1=a(t),irr_2=b(t),irr_3=c(t)),append=true)
end

IRR_adj = IRR_MVCF - IRR_PDCF + IRR_FSCF

CSV.write(“irr_adj.csv”,DataFrame(IRR_adj))

As far as I understand collect() is used to transform iterators into an array. So I defined e.g. " IRR_MVCF = collect(a(t))" in order to calculate IRR_adj. But the error message states below showing IRR_MVCF isn’t defined. Could someone point me out how I can refine the codes please?

LoadError: UndefVarError: IRR_MVCF not defined

in expression starting at C:\Users\XiaopingWang\Desktop\Learning_Julia\AdjAssetsMV.jl:30

top-level scope at AdjAssetsMV.jl:30

include_string(::Function, ::Module, ::String, ::String) at loading.jl:1088

Many thanks
Ping.

Hard to say what’s going on without a reproducible example, but sounds a bit like a scope issue - if you create variables in a loop, they won’t generally be accessible outside the loop.

Further, your a(t) notation looks a bit odd - from what you’re doing it looks like you’re trying to index into an array, but round brackets () are for function calls in Julia, not for indexing. Do you come from MATLAB by any chance? If you want to store results in some array a, the syntax is a[t] (square brackets).

2 Likes

please kindly start your code block with

```julia

and close it with
```
right now it is very hard to read

3 Likes

Thanks, I think you are right it seems a scope issue. I have two “for” loop statements which I posted the first one as above. When I moved the “end”, the error message " IRR_MVCF not defined" disappeared. Unfortunately, the similar error message appeared.

I think the issue here is how to define array which is a result of collective loop iterators. Could you please point out to me.

In addition, you are right in terms of the notations of a, the “t” represents an index of array. When changed a(t) - a[t], the codes do not work.

Happy to share the Julia codes and error message again if needed.

Many thanks,
Ping.

Sorry for not being clear.
…Julia

for t in 1:nb
a(t) = irr([-Market_values[:,t];AssetsCF[:,t]])
b(t) = irr([-Market_values[:,t];PDCF_annual[:,t]])
c(t) = irr([-Market_values[:,t];FSCF_annual[:,t]])

IRR_MVCF = collect(a(t))
IRR_PDCF = collect(b(t))
IRR_FSCF = collect(c(t))

IRR_adj = IRR_MVCF - IRR_PDCF + IRR_FSCF

CSV.write("irr_all.csv",DataFrame(irr_1=a(t),irr_2=b(t),irr_3=c(t)),append=true)
CSV.write("irr_adj.csv",DataFrame(IRR_adj=IRR_adj),append=true)

for n in 1:nb

    d(n) = npv(dot(IRR_adj'[n,:],ones(1)),vcat(0,AssetsCF[:,n])) 

    Adj_AssetMV = collect(d(n))

    CSV.write("Adj_MarketValues_new.csv",DataFrame(Adj_MarketValue_new=d(n)),append=true)
end

end

println(“Results were saved in separate CSV files”)


Error message
…Julia
ERROR: LoadError: adjoint not defined for Array{Float64,0}. Consider using permutedims for higher-dimensional arrays.
Stacktrace:
[1] error(::String) at .\error.jl:33
[2] adjoint(::Array{Float64,0}) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\transpose.jl:3
[3] (::var"#d#128"{Array{Float64,0}})(::Int64) at C:\Users\XiaopingWang\Desktop\Learning_Julia\AdjAssetsMV.jl:36
[4] top-level scope at C:\Users\XiaopingWang\Desktop\Learning_Julia\AdjAssetsMV.jl:38
[5] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1088
in expression starting at C:\Users\XiaopingWang\Desktop\Learning_Julia\AdjAssetsMV.jl:20

Many thanks,
Ping

The code above still has a(t) rather than a[t]?

The error is telling you that you’re trying to get the adjoint of a zero-dimensional array: this probably happens on this line:

    d(n) = npv(dot(IRR_adj'[n,:],ones(1)),vcat(0,AssetsCF[:,n])) 

where I assume IRR_adj is zero-dimensional. Again it’s hard to say why this is the case as we can’t actually run your code ourselves, but I presume it’s because of your indexing error (using round brackets). Consider:

julia> a(t) = 5.0
a (generic function with 1 method)

julia> collect(a(1))
0-dimensional Array{Float64,0}:
5.0

a(1) is just a call to the single argument function a you defined when you did a(t) = irr(...), and returns a zero dimensional array with whatever the result of irr(...) is.

2 Likes

triple backtick `
not period .

3 Likes

If you find our tips a bit hard to understand, please read Please read: make it easier to help you

2 Likes

Thank you Nilshg. I appreciated that the information I provided in my question wasn’t that clear as a couple of others suggested. But you have been so helpful to resolve my issue. Very much appreciated!

Have a lovely day!
Cheers,
Ping.

1 Like

Thank you for the tips, Josuagrw.

Regards,
Ping.