TransWikia.com

Map Function with Table on each row

Mathematica Asked by Smarter Math on August 1, 2021

I need help as I am new to Mathematica. I have a table with some nominal bond values:

 {
 {, Nominal, Maturity, Coupon },
 {Bond 1, 200, 1, 0.05},
 {Bond 2, 110, 15, 0.1},
 {Bond 3, 195, 7, 0.05},
 {Bond 4, 100, 25, 0.025},
 {Bond 5, 100, 10, 0.03}
}

and I want to calculate the Value of the bond with the following function but I could not map it right so that the values fit. Could someone help me ?

Here is the function for the valuation of the bond. If I paste each value by hand it works fine but I have to match it with the table with the Map function.

mwbond2[Nominal_, Maturity_, Coupon_, int0_] := Module[
  {n = Nominal, m = Maturity, c = Coupon, int = int0},
  MW = Sum[n*c /(1+int)^i, {i, 1, m, 1}] + n/(1 + int)^m; Print[MW]]

Thanks

One Answer

Using your data

list = {{, Nominal, Maturity, Coupon}, {Bond 1, 200, 1., 
    0.05}, {Bond 2, 110., 15., 0.1}, {Bond 3, 195., 7., 
    0.05}, {Bond 4, 100., 25., 0.025}, {Bond 5, 100., 10., 0.03}};

and a slightly modified version of your function

mwbond2[Nominal_, Maturity_, Coupon_, int0_] := 
 Module[{n = Nominal, m = Maturity, c = Coupon, int = int0}, 
  Sum[n*c/(1 + int)^i, {i, 1, m, 1}] + n/(1 + int)^m]

Note that Module evaluates to the Sum given at the end. Assigning the value to a variable is unnecessary here and I get it to return a value rather than printing it.

We don't use the row or column labels, so drop these

list1 = Drop[#, 1] & /@ Drop[list, 1]
(* {{200, 1., 0.05}, {110., 15., 0.1}, {195., 7., 0.05}, {100., 
  25., 0.025}, {100., 10., 0.03}} *)

We do want an interest rate (which I assume you omitted inadvertently). I use rates of 1%, 2%, ..., 5% for the 5 cases. I add the rates into the table

list2 = MapThread[Append, {list1, {1, 2, 3, 4, 5}/100}]
(* {{200, 1., 0.05, 1/100}, {110., 15., 0.1, 1/50}, {195., 7., 
  0.05, 3/100}, {100., 25., 0.025, 1/25}, {100., 10., 0.03, 1/20}} *)

I now apply the function to each row of the table

mwbond2 @@@ list2
(* {207.921, 223.074, 219.298, 76.5669, 84.5565} *)

Answered by mikado on August 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP