[英]KMV in r solving non linear equations
I am currently doing my master thesis on Merton KMV model and I need to implement it to a set of companies. 我目前正在做有关Merton KMV模型的硕士论文,我需要将其应用于一系列公司。 Here is the link to the csv file I used.
这是我使用的csv文件的链接。
I am trying to loop the process to solve the nonlinear equations. 我正在尝试循环求解非线性方程组。 So far I have found a solution to solve it once by implementing each parameters manually but now I need to apply it to the whole dataframe.
到目前为止,我已经找到了一种解决方案,可以通过手动实现每个参数来解决它,但是现在我需要将其应用于整个数据帧。
This is the code I have come up with so far: 到目前为止,这是我想出的代码:
library(nleqslv) #this is a package that solve a non linear equation to compute both the value of teh asset and its volatility according to the Black and Scholes formula in the Merton model
df <- read.csv("/AREX_D.csv")
rownames(df) <- df$Date
start <- as.Date("31-12-16",format="%d-%m-%y")
end <- as.Date("31-12-17",format="%d-%m-%y")
theDate <- start
E <- df$Market.Cap
D <- df$Default.point
T <- 1
sigmaE <- df$stdev
r <- -0.017
df$Asset <- NA
df$sigmaA <- NA
df$DD <- NA
df$PD <- NA
fnewton <- function(x)
{
y <- numeric(2)
d1 <- (log(x[1]/D)+(r+x[2]^2/2)*T)/x[2]*sqrt(T)
d2 <- d1-x[2]*sqrt(T)
y[1] <- E-(x[1]*pnorm(d1)-exp(-r*T)*D*pnorm(d2))
y[2] <- sigmaE*E-pnorm(d1)*x[2]*x[1]
y
}
xstart <- c(E+D, sigmaE)#initialising the x-values (asset value and volatility for the function to solve the non linear equation
while (theDate<=end)
{
out <- nleqslv(xstart,fnewton,method="Newton")
df$Asset <- out[1]
df$sigmaA <- out[2]
theDate <- theDate+1
}
print(tail(df))
My two problems are: 我的两个问题是:
I have found a package that could solve this issue: ifrogs but it is not available through R version 3.5.1 我发现了一个可以解决此问题的软件包:ifrogs,但它无法通过R版本3.5.1获得。
If anyone has any insight on either problem that would be of termendous help. 如果有人对这两个问题有任何见解,那将有很大帮助。
Thank you in advance :) 先感谢您 :)
You should read the csv with 您应该阅读csv与
df <- read.csv("AREX_D.csv", stringsAsFactors=FALSE)
to keep the dates a characters. 保持日期为字符。 See below for why.
有关原因,请参见下文。
Your function fnewton
contains errors. 您的函数
fnewton
包含错误。 It uses D
, E
and sigmaE
but these are vectors. 它使用
D
, E
和sigmaE
但是它们是向量。 The function accepts a vector as argument (argument x
) and uses the individual elements of this vector. 该函数接受一个向量作为参数(参数
x
),并使用该向量的各个元素。 This combination will generate error messages. 此组合将生成错误消息。 In your code
xstart
is a single long vector. 在您的代码中
xstart
是单个长向量。 This will not be accepted by nleqslv
. nleqslv
将不接受此nleqslv
。
The function definition should be changed to 函数定义应更改为
fnewton <- function(x, D, E, sigmaE)
{
y <- numeric(2)
d1 <- (log(x[1]/D)+(r+x[2]^2/2)*T)/x[2]*sqrt(T)
d2 <- d1-x[2]*sqrt(T)
y[1] <- E-(x[1]*pnorm(d1)-exp(-r*T)*D*pnorm(d2))
y[2] <- sigmaE*E-pnorm(d1)*x[2]*x[1]
y
}
The values for D
etc. should be passed to the function as scalars. D
等的值应作为标量传递给函数。
nleqslv
returns a list consisting of various items. nleqslv
返回一个包含各种项目的列表。 One of these is x
: the final value for the argument x
. 其中之一是
x
:参数x
的最终值。 You cannot access these as out[1]
and out[2] .
您不能以
out[1]
和out [2]的身份访问它们.
You need to store the output in each row of the dataframe. 您需要将输出存储在数据框的每一行中。
You want to store these in the dataframe so use out$x[1]
and out$x[2]
. 您要将它们存储在数据框中,因此请使用
out$x[1]
和out$x[2]
。 Some initial values for the starting value xstart
contain NA
. 起始值
xstart
一些初始值包含NA
。 So one needs to test if xstart
contains NA
before calling nleqslv
. 因此,需要在调用
nleqslv
之前测试xstart
包含NA
。
There are other errors in your code and the final while
loop. 您的代码和最终的
while
循环中还有其他错误。 So change the final loop to this 因此将最终循环更改为此
df$termcd <- -1
kstart <- which(df$Date == "03/01/2017")
kend <- NROW(df)
for( k in kstart:kend ) {
xstart <- c(E[k]+D[k], sigmaE[k])
if(anyNA(xstart)) { next } # skip NA in input
out <- nleqslv(xstart,fnewton,method="Newton", D=D[k],E=E[k],sigmaE=sigmaE[k])
df$Asset[k] <- out$x[1]
df$sigmaA[k] <- out$x[2]
df$termcd[k] <- out$termcd
}
And now check if solutions have been obtained by inspecting df$termcd
. 现在检查
df$termcd
是否获得了解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.