2
$\begingroup$

This question already has an answer here:

I'm trying to understand the Wolfram language and copied some examples from the documentation into a workbook and then tried to work through examples of my own. I'm attempting to evaluate a function with two variables for several combinations of those variables. I've attempted this in the following two ways, but I can only get one output rather than an output for each of the three sets of inputs:

spot = {45, 50, 55};vol = {.20, .30, .40};
Function[{a, b}, FinancialDerivative[{"European", "Call"}, {"StrikePrice" -> 50.00, 
"Expiration" -> 1}, {"InterestRate" -> 0.1, "Volatility" -> b, 
"CurrentPrice" -> a, "Dividend" -> 0.05}]][spot, vol]

This gives the output 2.42146

I also tried inputting as a pure function:

FinancialDerivative[{"European", "Call"}, {"StrikePrice" -> 50.00, 
 "Expiration" -> 1}, {"InterestRate" -> 0.1, "Volatility" -> #2, 
 "CurrentPrice" -> #1, "Dividend" -> 0.05}] &[spot, vol]

and got the same output 2.42146

How can I evaluate the function for spot=45,vol=.2, spot=50,vol=.3 and spot=55,vol=.4?

$\endgroup$

marked as duplicate by Artes, corey979, Feyre, MarcoB, Young Feb 5 '17 at 17:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2
$\begingroup$

Add the attribute Listable to your function to make it Thread over the list arguments:

func = Function[{a, b}, FinancialDerivative[{"European", "Call"}, {"StrikePrice" -> 50.00, 
     "Expiration" -> 1}, {"InterestRate" -> 0.1, "Volatility" -> b, 
     "CurrentPrice" -> a, "Dividend" -> 0.05}], Listable];

func[spot, vol]

{2.42146, 6.76859, 11.7673}

Alternatively, with a function that is not Listable

func2 = Function[{a, b}, FinancialDerivative[{"European", "Call"}, {"StrikePrice" -> 50.00,
     "Expiration" -> 1}, {"InterestRate" -> 0.1, "Volatility" -> b, 
     "CurrentPrice" -> a, "Dividend" -> 0.05}]];

MapThread[func2, {spot, vol}]

{2.42146, 6.76859, 11.7673}

func2 @@@ Transpose[{spot, vol}]

{2.42146, 6.76859, 11.7673}

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.