Dear all, I just want to get the derivative of a function that looks like: y = exp(x1*b) / (exp(x1*b) + exp(x2*b)) where y is a scalar, x1, x2, and b are vectors. I am going to take the derivative of b with respect to y, but I cannot derive an expression in which b is function of y. I know there is another way to get the similar result, i.e., first take the derivative of y with respect to each element of b, and then take its reciprocal. But it is not what I want. Could someone please tell me how to solve this problem? Thank you in advance. Lisa -- View this message in context: http://r.789695.n4.nabble.com/Derivative-of-a-function-tp3631814p3631814.html Sent from the R help mailing list archive at Nabble.com.
(1) You really ought to do your own homework. (2) What has this to do with R? cheers, Rolf Turner On 29/06/11 14:03, Lisa wrote:> Dear all, > > I just want to get the derivative of a function that looks like: > > y = exp(x1*b) / (exp(x1*b) + exp(x2*b)) > > where y is a scalar, x1, x2, and b are vectors. I am going to take the > derivative of b with respect to y, but I cannot derive an expression in > which b is function of y. I know there is another way to get the similar > result, i.e., first take the derivative of y with respect to each element of > b, and then take its reciprocal. But it is not what I want. Could someone > please tell me how to solve this problem? Thank you in advance. > > Lisa > > > -- > View this message in context: http://r.789695.n4.nabble.com/Derivative-of-a-function-tp3631814p3631814.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
This is not a homework. I just want to see if there are some R functions or some ideas I can borrow to solve my problem. -- View this message in context: http://r.789695.n4.nabble.com/Derivative-of-a-function-tp3631814p3633071.html Sent from the R help mailing list archive at Nabble.com.
On Jun 29, 2011, at 10:48 AM, Lisa wrote:> This is not a homework. I just want to see if there are some R > functions or > some ideas I can borrow to solve my problem.There is a deriv function that provides limited support for symbolic differentiation. The Rhelp list is advertised ( http://www.R-project.org/posting-guide.html ) as expecting you to have made some efforts at searching. This answer might not have floated to the surface among the thousand or so hits, but at least you should have tried. RSiteSearch("derivative") And there is, of course, CrossValidated: http://stats.stackexchange.com/> > -- > View this message in context: http://r.789695.n4.nabble.com/Derivative-of-a-function-tp3631814p3633071.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT
On Tue, Jun 28, 2011 at 10:03 PM, Lisa <lisajca at gmail.com> wrote:> Dear all, > > I just want to get the derivative of a function that looks like: > > y = exp(x1*b) / (exp(x1*b) + exp(x2*b)) > > where y is a scalar, x1, x2, and b are vectors. I am going to take the > derivative of b with respect to y, but I cannot derive an expression in > which b is function of y. I know there is another way to get the similar > result, i.e., first take the derivative of y with respect to each element of > b, and then take its reciprocal. But it is not what I want. Could someone > please tell me how to solve this problem? Thank you in advance.Assuming you meant the derivative of y with respect to b:> D(expression(exp(x1*b) / (exp(x1*b) + exp(x2*b))), "b")exp(x1 * b) * x1/(exp(x1 * b) + exp(x2 * b)) - exp(x1 * b) * (exp(x1 * b) * x1 + exp(x2 * b) * x2)/(exp(x1 * b) + exp(x2 * b))^2 See ?D and also note deriv on the same help page for another alternative. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
On 30/06/11 06:16, Gabor Grothendieck wrote:> On Tue, Jun 28, 2011 at 10:03 PM, Lisa<lisajca at gmail.com> wrote: >> Dear all, >> >> I just want to get the derivative of a function that looks like: >> >> y = exp(x1*b) / (exp(x1*b) + exp(x2*b)) >> >> where y is a scalar, x1, x2, and b are vectors. I am going to take the >> derivative of b with respect to y, but I cannot derive an expression in >> which b is function of y. I know there is another way to get the similar >> result, i.e., first take the derivative of y with respect to each element of >> b, and then take its reciprocal. But it is not what I want. Could someone >> please tell me how to solve this problem? Thank you in advance. > Assuming you meant the derivative of y with respect to b:I think the original post made it quite clear that the derivative of b with respect to y was indeed what was wanted; i.e. the OP needs to do implicit differentiation which R does not do automatically. cheers, Rolf Turner>> D(expression(exp(x1*b) / (exp(x1*b) + exp(x2*b))), "b") > exp(x1 * b) * x1/(exp(x1 * b) + exp(x2 * b)) - exp(x1 * b) * > (exp(x1 * b) * x1 + exp(x2 * b) * x2)/(exp(x1 * b) + exp(x2 * > b))^2 > > See ?D and also note deriv on the same help page for another alternative. >
Yes. I need to do implicit differentiation. After rearrangement, I got (x2 ? x1) * b = log(1 / y - 1) Take derivative of both sides with respect to y, I have (x2 ? x1) * b?[y] = - 1/y(1-y) Since both (x2 ? x1) and b?[y] are vectors, I cannot move (x2 ? x1) to RHS. This is why I posted my question here to see if there is some R functions or some idea that can help me solve this problem. Thanks. Lisa -- View this message in context: http://r.789695.n4.nabble.com/Derivative-of-a-function-tp3631814p3633947.html Sent from the R help mailing list archive at Nabble.com.
If you just want the value of the derivative at a particular point, would numerical derivatives suffice? If so, try (for example) the numDeriv package. S ________________________________________ From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of Lisa [lisajca at gmail.com] Sent: 29 June 2011 21:35 To: r-help at r-project.org Subject: Re: [R] Derivative of a function Yes. I need to do implicit differentiation. After rearrangement, I got (x2 ? x1) * b = log(1 / y - 1) Take derivative of both sides with respect to y, I have (x2 ? x1) * b?[y] = - 1/y(1-y) Since both (x2 ? x1) and b?[y] are vectors, I cannot move (x2 ? x1) to RHS. This is why I posted my question here to see if there is some R functions or some idea that can help me solve this problem. Thanks. Lisa -- View this message in context: http://r.789695.n4.nabble.com/Derivative-of-a-function-tp3631814p3633947.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
On Wed, Jun 29, 2011 at 4:35 PM, Lisa <lisajca at gmail.com> wrote:> Yes. I need to do implicit differentiation. After rearrangement, I got > > (x2 ? x1) * b = log(1 / y - 1) > > Take derivative of both sides with respect to y, I have > > (x2 ? x1) * b?[y] = - 1/y(1-y) > > Since both (x2 ? x1) and b?[y] are vectors, I cannot move (x2 ? x1) to > RHS. This is why I posted my question here to see if there is some R > functions or some idea that can help me solve this problem. Thanks. >I am not sure if this counts as an approach that you are trying to exclude but: db[i]/dy = { dlogit(y)/dy } / { dlogit(y)/db[i] } = { 1/{y(1-y)} / { x1[i] - x2[i] } The numerator is taken from your calculation and the denominator is from linearity. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com