我想知道Apache Math Commons库中是否有一个与逐个元素相乘的特性,类似于MATLAB中使用的特性,结果矩阵#Binomial option pricing (2)
#We can expand the tree to more than 3rd depth.
#U = exp(volatility)
#D = exp(-volatility)
#p = 0.5 (We have the equal chance of making or losing money)
#Risk free rate = 0.02 => exp(0.02)
#For those who are not familiar with data structure, I deliberately used just array.
#I'll make a new code for those who are familiar with tree data structure
library(igraph)
#Define the variable
depth<-3 #How many steps (tree depth) do you want to make
rate <-0.02 #Risk Free rate
volatility <- 0.35
exercise_price <- 35
stock_price <- 47.5
upside_probability<-(exp(rate)-exp(-volatility))/(exp(volatility)-exp(-volatility))
rate <- exp(rate)
total_node<-2^depth-1 #Total number of node
G <- graph.tree(n=total_node, children=2) #I'll make a graph whose nodes are 7, and each node has two children
stock_tree <- (1:total_node)
stock_tree[1]<-stock_price
tree_traverse <- 2^(depth-1) -1
for(i in 1:tree_traverse) {
#We are going to use mathematical trick to represent tree.
stock_tree[i*2] <- stock_tree[i] * exp(volatility)
stock_tree[i*2 + 1] <- stock_tree[i] * exp(-volatility)
}
V(G)$name <- round(stock_tree) #Name of the tree
lay <- layout.reingold.tilford(G) #It's tree. You can try other shape with other layout options
plot(G, layout=lay, vertex.size=15, edge.arrow.size=0.1) #Draw the tree.
#As opposed to the stock price, the option pricing starts out with end nodes (bottom nodes)
#I already explained the logic. Just follow it from one by one.
option_price<-(1:total_node)
bottom_node<-tree_traverse + 1
#In order to value the option, we need to calculate bottom line first.
for(i in bottom_node:total_node) {
after_option <- stock_tree[i] - exercise_price
if( after_option >0 ) {
option_price[i] <- after_option
} else {
option_price[i] <- 0
}
}
#Discount it back to current time while considering the probabilty of up and down
for(i in tree_traverse:1) {
option_price[i]<-upside_probability*option_price[i*2]
option_price[i]<-option_price[i]+(1-upside_probability)*option_price[i*2+1]
option_price[i]<-option_price[i]/rate
}
V(G)$name <- round(option_price)
plot(G, layout=lay, vertex.size=15, edge.arrow.size=0.1)
等于矩阵{{1}中的每个值}乘以矩阵C
中的对应值。我想避免编写自己的版本,因为我知道线性代数包是针对这些类型的操作进行了高度优化的,如果已经存在(已经优化),我不想在我自己的实现上失去性能。
在MATLAB中:A
和B
的维度必须等于C = A.*B
。
答案 0 :(得分:0)
我用过Apache的RealVectors数学ebeMultiplication。我不知道是否有一个等效的矩阵函数,我检查过,我没有找到。至于载体:
RealVector output = new ArrayRealVector(o); // o and y are double[]
RealVector expected = new ArrayRealVector(y);
RealVector errors = expected.subtract(output);
RealVector delta = errors.ebeMultiply(output);