Katherine Gobin
2014-Jan-06 11:41 UTC
[R] Reversing the Equation to find value of variable
Dear R forum I have following variables - EAD = 10000 LGD = 0.45 PD = 0.47 M = 3 # Equation 1 R = 0.12*(1-exp(-50*PD))/(1-exp(-50)) + 0.24*(1-(1-exp(-50*PD))/(1-exp(-50))) b = (0.11852 - 0.05478 * log(PD))^2 K = (LGD * pnorm((1 - R)^(-0.5) * qnorm(PD) + (R / (1 - R))^0.5 * qnorm(0.999)) - PD * LGD) * (1 - 1.5 * b)^(-1) * (1 + (M - 2.5) * b) RWA = K * 12.5 * EAD> RWA[1] 22845.07 # _________________________________________________________________ # MY Problem In the above part, knowing values of LGD, EAD, M and PD, the value of RWA was calculated. However, I need to go reverse way in the sense knowing the values of LGD, EAD, M and RWA, I need to find value of PD. So I have tried to use uniroot as (RWA - K * 12.5 * EAD and used the above equations i place of K and R) RWA = 22845.07 LGD = 0.45 EAD = 10000 M = 3 f = function(x) RWA - (LGD*pnorm((1-(0.12*(1-exp(-50*x))/(1-exp(-50))+0.24*(1-(1-exp(-50*x))/(1-exp(-50)))))^(-0.5)*qnorm(x)+((0.12*(1-exp(-50*x))/(1-exp(-50))+0.24*(1-(1-exp(-50*x))/(1-exp(-50))))/(1-(0.12*(1-exp(-50*x))/(1-exp(-50))+0.24*(1-(1-exp(-50*x))/(1-exp(-50))))))^0.5*qnorm(0.999))-x*LGD) * (1-1.5*((0.11852-0.05478 * log(x))^2))^(-1)*(1+(M-2.5)*((0.11852-0.05478 * log(x))^2))*12.5*EAD uniroot(f, c(0,1), tol = 0.0000000001) I get following error -> uniroot(f, c(0,1), tol = 0.0000000001)Error in uniroot(f, c(0, 1), tol = 1e-10) : f.lower = f(lower) is NA Kindly guide as I am not sure if uniroot is the correct way of doing it or not. Ideally, I should be getting the PD value of 0.47. With regards Katherine [[alternative HTML version deleted]]
Frede Aakmann Tøgersen
2014-Jan-06 12:01 UTC
[R] Reversing the Equation to find value of variable
Hi Reading the error message carefully you can see that f() is not defined at 0:> uniroot(f, c(0, 1))Error in uniroot(f, c(0, 1)) : f.lower = f(lower) is NA> f(0)[1] NaN If you plot f() in the interval (0,1) then you'll see there is two solutions:> uniroot(f, c(0.0001, 1))Error in uniroot(f, c(1e-04, 1)) : f() values at end points not of opposite sign> uniroot(f, c(0.0001, 0.2))$root [1] 0.1533901 $f.root [1] 0.3414232 $iter [1] 6 $estim.prec [1] 6.103516e-05> uniroot(f, c(0.3, 1))$root [1] 0.4699984 $f.root [1] -0.04112121 $iter [1] 8 $estim.prec [1] 6.103516e-05>Yours sincerely / Med venlig hilsen Frede Aakmann T?gersen Specialist, M.Sc., Ph.D. Plant Performance & Modeling Technology & Service Solutions T +45 9730 5135 M +45 2547 6050 frtog at vestas.com http://www.vestas.com Company reg. name: Vestas Wind Systems A/S This e-mail is subject to our e-mail disclaimer statement. Please refer to www.vestas.com/legal/notice If you have received this e-mail in error please contact the sender.> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > On Behalf Of Katherine Gobin > Sent: 6. januar 2014 12:42 > To: r-help at r-project.org > Subject: [R] Reversing the Equation to find value of variable > > Dear R forum > > I have following variables - > > EAD = 10000 > LGD = 0.45 > PD = 0.47 > M = 3 > > # Equation 1 > > R = 0.12*(1-exp(-50*PD))/(1-exp(-50)) + 0.24*(1-(1-exp(-50*PD))/(1-exp(- > 50))) > > b = (0.11852 - 0.05478 * log(PD))^2 > > K = (LGD * pnorm((1 - R)^(-0.5) * qnorm(PD) + (R / (1 - R))^0.5 * > qnorm(0.999)) - PD * LGD) * (1 - 1.5 * b)^(-1) * (1 + (M - 2.5) * b) > > RWA = K * 12.5 * EAD > > > > RWA > [1] 22845.07 > > # > __________________________________________________________ > _______ > > # MY Problem > > In the above part, knowing values of LGD, EAD, M and PD, the value of RWA > was calculated. However, I need to go reverse way in the sense knowing the > values of LGD, EAD, M and RWA, I need to find value of PD. > > So I have tried to use uniroot as (RWA - K * 12.5 * EAD and used the above > equations i place of K and R) > > RWA =?22845.07 > LGD = 0.45 > EAD = 10000 > M = 3 > > f = function(x) RWA - ?(LGD*pnorm((1-(0.12*(1-exp(-50*x))/(1-exp(- > 50))+0.24*(1-(1-exp(-50*x))/(1-exp(-50)))))^(-0.5)*qnorm(x)+((0.12*(1- > exp(-50*x))/(1-exp(-50))+0.24*(1-(1-exp(-50*x))/(1-exp(-50))))/(1-(0.12*(1- > exp(-50*x))/(1-exp(-50))+0.24*(1-(1-exp(-50*x))/(1-exp(- > 50))))))^0.5*qnorm(0.999))-x*LGD) * (1-1.5*((0.11852-0.05478 * > log(x))^2))^(-1)*(1+(M-2.5)*((0.11852-0.05478 * log(x))^2))*12.5*EAD > > uniroot(f, c(0,1), tol = 0.0000000001) > > I get following error - > > > uniroot(f, c(0,1), tol = 0.0000000001) > Error in uniroot(f, c(0, 1), tol = 1e-10) : f.lower = f(lower) is NA > > Kindly guide as I am not sure if uniroot is the correct way of doing it or not. > Ideally, I should be getting the PD value of 0.47. > > With regards > > Katherine > [[alternative HTML version deleted]]
Berend Hasselman
2014-Jan-06 12:12 UTC
[R] Reversing the Equation to find value of variable
On 06-01-2014, at 12:41, Katherine Gobin <katherine_gobin at yahoo.com> wrote:> Dear R forum > > I have following variables - > > EAD = 10000 > LGD = 0.45 > PD = 0.47 > M = 3 > > # Equation 1 > > R = 0.12*(1-exp(-50*PD))/(1-exp(-50)) + 0.24*(1-(1-exp(-50*PD))/(1-exp(-50))) > > b = (0.11852 - 0.05478 * log(PD))^2 > > K = (LGD * pnorm((1 - R)^(-0.5) * qnorm(PD) + (R / (1 - R))^0.5 * qnorm(0.999)) - PD * LGD) * (1 - 1.5 * b)^(-1) * (1 + (M - 2.5) * b) > > RWA = K * 12.5 * EAD > > >> RWA > [1] 22845.07 > > # _________________________________________________________________ > > # MY Problem > > In the above part, knowing values of LGD, EAD, M and PD, the value of RWA was calculated. However, I need to go reverse way in the sense knowing the values of LGD, EAD, M and RWA, I need to find value of PD. > > So I have tried to use uniroot as (RWA - K * 12.5 * EAD and used the above equations i place of K and R) > > RWA = 22845.07 > LGD = 0.45 > EAD = 10000 > M = 3 > > f = function(x) RWA - (LGD*pnorm((1-(0.12*(1-exp(-50*x))/(1-exp(-50))+0.24*(1-(1-exp(-50*x))/(1-exp(-50)))))^(-0.5)*qnorm(x)+((0.12*(1-exp(-50*x))/(1-exp(-50))+0.24*(1-(1-exp(-50*x))/(1-exp(-50))))/(1-(0.12*(1-exp(-50*x))/(1-exp(-50))+0.24*(1-(1-exp(-50*x))/(1-exp(-50))))))^0.5*qnorm(0.999))-x*LGD) * (1-1.5*((0.11852-0.05478 * log(x))^2))^(-1)*(1+(M-2.5)*((0.11852-0.05478 * log(x))^2))*12.5*EAD > > uniroot(f, c(0,1), tol = 0.0000000001) > > I get following error - > >> uniroot(f, c(0,1), tol = 0.0000000001) > Error in uniroot(f, c(0, 1), tol = 1e-10) : f.lower = f(lower) is NA > > Kindly guide as I am not sure if uniroot is the correct way of doing it or not. Ideally, I should be getting the PD value of 0.47.To make life easier and more readable define a function f, where x is a value for PD f <- function(x) { PD <- x R <- 0.12*(1-exp(-50*PD))/(1-exp(-50)) + 0.24*(1-(1-exp(-50*PD))/(1-exp(-50))) b <- (0.11852 - 0.05478 * log(PD))^2 K <- (LGD * pnorm((1 - R)^(-0.5) * qnorm(PD) + (R / (1 - R))^0.5 * qnorm(0.999)) - PD * LGD) * (1 - 1.5 * b)^(-1) * (1 + (M - 2.5) * b) RWA - K * 12.5 * EAD } where a value of x must be found such that f becomes 0. Draw a curve for f(x) curve(f,from=0,to=1) So is 0 a valid lower bound for f? Try f(0) So try a little differently curve(f,from=0.1,to=1) Have a careful look at the curve. The endpoints for uniroot must be give function values of opposite sign. So try this: uniroot(f,c(0.1,.4), tol=1e-8) uniroot(f,c(0.4,.6), tol=1e-8) where the second uniroot will give what you seem to want. But beware: your function has two solutions. Berend
Reasonably Related Threads
- Appending data to a data.frame and writing a csv
- The best solver for non-smooth functions?
- Solving a simple linear equation using uniroot give error object 'x' not found
- ansari.test.default: bug in call to uniroot?
- Solving a simple linear equation using uniroot give error object 'x' not found