[R] Reversing the Equation to find value of variable

Berend Hasselman bhh at xs4all.nl
Mon Jan 6 13:12:09 CET 2014


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




More information about the R-help mailing list