Vector Calculations

vc <vector_name>[{<date1> [- <date2>]}] = <expression>

The vc, or vector calculation, command evaluates the expression on the right and puts the results into the named vector. Only a few matrix and vector operations are implemented with vc. It can add or subtract any number of vectors and multiply a matrix times a vector. It cannot add or subtract matrices. Parentheses are not supported. However, scalar values (either constants or G bank variables) can be used to premultiply or postmultiply a vector or matrix. Also, a scalar value can appear all alone on the right hand side of a vc equation to indicate that the entire vector will be set equal to that scalar value. Between a matrix and a vector, an ‘*’ indicates matrix-vector multiplication. Between two vectors, the ‘*’ means element-by-element multiplication. Between a vector and a matrix, the ‘/’ means multiplication by the transpose of the matrix or vector on the left. Between two vectors, the ‘/’ means element-by-element division. Setting a vector equal to a time series stores the time series in each vector element. Limited capability exists for setting values for all periods equal to the value in a single year and to normalize a vector by its values in a single year; see the examples below. The vc command is performed only over the interval defined by the current fdates command.

Example: If am and cm are matrices and out, pdm, qcu, cprices, and index are vectors, we could write

vc out = am*out + out    # Matrix multiplication, vector addition
vc qcu = out*pdm         # Element-by-element vector multiplication
vc out = qcu/pdm         # Element-by-element vector division
vc cprices = pdm/cm      # This actually is pdm'cm.
vc pdm = pdm / pdm{2000} # Normalize prices to 1.0 in 2000.
vc index = index{2000}   # Set equal to values in 2000 for all periods.

The <vector_name> must be a valid vector name in the default Vam file; matrices and packed matrices are not allowed. All right-hand-side vectors and matrices must be in the default Vam file; bank letters are not allowed.

The vc command is the only tool available for vector calculations that allows general (though limited) operations on the right-hand-side. Note that the vf command allows far more flexibility, but it operates on just one element at a time. While vc operates only on vectors, several standard matrix operations are offered. The following technique provides another means of copying vectors and matrices, in whole or in part, and allows addition and subtraction.

<vmake|vplus|vminus> [startyr [endyr]] <target> <source> [source …]

The vmake, vplus, and vminus commands construct a vector out of another vector or part of a matrix, or make a matrix out of one or more vectors and/or other matrices. vmake replaces selected values in the target with the values from the source; vplus adds the values in the source to the values is the target; and vminus subtracts the values in the source from the values is the target. Up to 200 sources may be taken from one or more assigned Vam files.

startyr:

endyr:

Optional parameters. If not given, then the process will work over the range of dates common to the target Vam file and all the source vam files.

target:

A matrix or a vector.

source:

Sources are one or more matrices and/or vectors.

Each matrix, whether a target or a source, is represented by

MatrixName [(<r|c> [lines])[(<c|r> elements)]]:

r|c indicates whether the data will be taken by row or by column, lines the selected rows or columns, and elements the items selected within the specified rows or columns. The total number of lines selected from the sources must equal the number of lines selected from the target, and the number of elements selected from each source must equal the number of elements selected in the target. If lines or elements are omitted, the default is all rows/columns or all elements.

Each vector, whether a target or a source, is represented by

VectorName [(v elements)]:

where elements are the items selected within the vector. If elements are omitted, the default is all elements. The number of elements selected from each source must equal the number of elements selected in the target.

Here are some examples:

The first example replaces all elements in rows 1, 2, and 3 of matrix ‘A’ with all elements in vectors ‘a’, ‘b’, and ‘c’.

vmake A(r 1-3) a b c

The second example adds the values of all elements in vectors ‘a’, ‘b’, and ‘c’ to the first five elements of columns 1, 2 and 3 in matrix ‘A’ (assuming that vectors ‘a’, ‘b’, and ‘c’ each have five elements.

vplus A(c 1-3)(r 1-5) a b c

If vectors ‘a’, ‘b’, and ‘c’ had more than five elements, the command would have to be

vplus A(c 1-3)(r 1-5) a(v 1-5) b(v 1-5) c(v 1-5)

The next example subtracts column 3 of matrix ‘A’ from the vector ‘c’.

vminus c A(c 3)

The final example is more complicated. It puts all rows of matrix ‘B’ (starting from row 1) followed by the vector ‘c’ into columns 2-100 of the matrix ‘A’, thus transposing the data in the sources.

vmake A(c 2-100) B(r) c

Related Topics: vmake