When I run the code following
proc iml;
start main;
varNames = {"tau" "K" "call_option_price" "S" "r" "put_option_price" "T" };
use cboe; * use the import data set;
read all var varNames;
T = tau/365;
F = K + exp(r*T) * (call_option_price - put_option_price);
K0 = int(F);
finish;
It returns
ERROR: (execution) Matrices do not conform to the operation.
where
operation : * at line 89 column 14
operands : r, T
r 2531 rows 1 col (numeric)
T 2531 rows 1 col (numeric)
I know the calculations should be conducted between matched matrixes. However, I found the variable r and T have the same structure with 2531 rows and 1 col.
This error message arises when you try to take the square root of negative numbers, and it clear that this has happened 1437 times. To see where the problem is occuring, I suggest you split the one line of code into 3 or 4 separate lines and print the values at each step for a small data set. The problem may be the data itself, or it may be that your large compound expression is not evaluated in the way that you expect because of operator precedence, so try adding more parentheses to make the evaluation order unambiguous.
You have read in column vectors. There are two ways to multiply with column vectors: the inner product and elementwise multiplication. An inner product will return a scalar. Elementwise multiplication will return a vector.
The asterisk (*) is used for matrix and vector operations. So r`*T will return the inner product. The # operator performs elementwise multiplication. So r#T will return a vector.
If you are trying to perform the same operation on each row of the data, then the syntax is
F = K + exp(r#T) # (call_option_price - put_option_price);
By the way, you can delete the lines "start main;" and "finish." You might have inserted them because you have a RUN statement at the end of the program. A RUN statement is not necessary.
It works. However, when I use this method to run the following syntax, it returns ERROR: Invocation of unresolved module SQR.
sigma = sqr(2/T # sum(deltak/K) # exp(R # T) # Q-1/T # (F/K0-1) ## 2);
where deltak is an Integer 5 and all the other variables are read in iml as elementwise I think. Is it caused by the incorrect use of sum function? The parameter k is a vector with n rows and 1 column.
how can I achieve Summation[i] (deltak[i]/k[i]) correctly?
Thanks.
The ERROR message tells you the problem: There is no SQR function. Did you mean SQRT for "square root"?
The SUM function looks correct.
Yes it is. I have changed it to sqrt().
But why it returns following error when I perform sqrt() for this calculation.
ERROR: (execution) Invalid argument to function.
count : number of occurrences is 1437
operation : SQRT at line 93 column 13
operands : sigma_2
sigma_2 2531 rows 1 col (numeric)
If I remove the sqrt() function, it works well.
This error message arises when you try to take the square root of negative numbers, and it clear that this has happened 1437 times. To see where the problem is occuring, I suggest you split the one line of code into 3 or 4 separate lines and print the values at each step for a small data set. The problem may be the data itself, or it may be that your large compound expression is not evaluated in the way that you expect because of operator precedence, so try adding more parentheses to make the evaluation order unambiguous.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.