# Net present value (npv function)

**URL:** <https://discourse.julialang.org/t/net-present-value-npv-function/48487>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 16, 2020, 1:11pm UTC](https://discourse.julialang.org/t/net-present-value-npv-function/48487 "2020-10-16T13:11:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ping](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Ping](https://discourse.julialang.org/u/Ping)\
**Post date:** [October 16, 2020, 1:11pm UTC](https://discourse.julialang.org/t/net-present-value-npv-function/48487/1 "2020-10-16T13:11:04Z")

</div>

I have been trying to use npv to calculate the net present value. The interest rate and cashflows are from a row of matrix, look like this:  
npv(norm(IRR\_adj[:,1]),nb,AssetsCF[:,1])

where IRR\_add is 1×1185 Array{Float64,2} and I use norm to convert it to a float64; AssetsCF is 101×1185 Adjoint{Float64,Array{Float64,2}}. the expression above is to take Row 1 in Arrays

The error message says:

LoadError: MethodError: no method matching npv(::Float64, ::Int64, ::Array{Float64,1})

Closest candidates are:

npv(::Real, !Matched::AbstractArray{var"#s17",1} where var"#s17"\<:Real) at C:\Users\XiaopingWang.julia\packages\NPFinancial\x1dsh\src\NPFinancial.jl:268

in expression starting at C:\Users\XiaopingWang\Desktop\Learning\_Julia\Assets\_IRR\_3.jl:38

top-level scope at Assets\_IRR\_3.jl:38

include\_string(::Function, ::Module, ::String, ::String) at loading.jl:1088

Could someone help to point out where is wrong in code please?

Thanks,  
Ping.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [October 16, 2020, 1:23pm UTC](https://discourse.julialang.org/t/net-present-value-npv-function/48487/2 "2020-10-16T13:23:34Z")

</div>

Looks like it takes only 2 arguments, but you are passing 3

[https://tk3369.github.io/NPFinancial.jl/latest/index.html](https://tk3369.github.io/NPFinancial.jl/latest/index.html)

---

<div class="post-metadata">

**Author:** ![Ping](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Ping](https://discourse.julialang.org/u/Ping)\
**Post date:** [October 16, 2020, 1:58pm UTC](https://discourse.julialang.org/t/net-present-value-npv-function/48487/3 "2020-10-16T13:58:49Z")

</div>

Thanks. well-spotted. although a different error appears after entering 2 arguments

for n in 1:3  
d(n) = npv(norm(IRR\_adj[:,n]),AssetsCF[:,n])

end

LoadError: ArgumentError: ‘Float64’ iterates ‘Float64’ values, which doesn’t satisfy the Tables.jl `AbstractRow` interface  
in expression starting at C:\Users\XiaopingWang\Desktop\Learning\_Julia\Assets\_IRR\_3.jl:41  
invalidtable(::Float64, ::Float64) at tofromdatavalues.jl:42  
iterate at tofromdatavalues.jl:48 [inlined]  
buildcolumns at fallbacks.jl:185 [inlined]  
columns at fallbacks.jl:237 [inlined]  
DataFrame(::Float64; copycols::Bool) at tables.jl:43  
DataFrame(::Float64) at tables.jl:34  
top-level scope at Assets\_IRR\_3.jl:44  
include\_string(::Function, ::Module, ::String, ::String) at loading.jl:1088

I am new to Julia, it’s great to have Julia Forum to help out!

Thanks,  
Ping.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 16, 2020, 2:50pm UTC](https://discourse.julialang.org/t/net-present-value-npv-function/48487/4 "2020-10-16T14:50:23Z")

</div>

My guess is that the error isn’t happening exactly on that line. It is happening when you are trying to put your results in a DataFrame

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [October 16, 2020, 3:04pm UTC](https://discourse.julialang.org/t/net-present-value-npv-function/48487/5 "2020-10-16T15:04:49Z")

</div>

Yea, if this is what you ran to get that error, it is likely coming from `d(n)`. Not sure what type `d` is but if it is just a `Vector` with length 3 then you should use

```julia
for n in 1:3
    d[n] = npv(norm(IRR_adj[:,n]),AssetsCF[:,n])
end

```

---

<div class="post-metadata">

**Author:** ![Ping](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Ping](https://discourse.julialang.org/u/Ping)\
**Post date:** [October 16, 2020, 4:40pm UTC](https://discourse.julialang.org/t/net-present-value-npv-function/48487/6 "2020-10-16T16:40:07Z")

</div>

Thanks guys! It was a dataFrame issue. I have amended my codes and the error is resolved.

Have a lovely weekend!

Cheers,  
Ping.
