# "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

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?

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

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

for n in 1:nb

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

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

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.