[R] Optimisation with Normalisation Constraint

Lorenzo Isella lorenzo@i@ell@ @ending from gm@il@com
Wed Jun 20 17:50:48 CEST 2018


Dear All,
I have a problem I haver been struggling with for a while: I need to
carry out a non-linear fit (and this is the
easy part).
I have a set of discrete values {x1,x2...xN} and the corresponding
{y1, y2...yN}. The difficulty is that I would like the linear fit to
preserve the sum of the values y1+y2+...yN.
I give an example below (for which there may even be an analytical
solution, but that is not the point here)

############################################################################
library(minpack.lm)



set.seed(124)

z <- rexp(3000,3)


zf <- z[z<= 0.5 | z>=0.9]

myhist <- hist(zf, plot=FALSE) 



df <- data.frame(x=myhist$mids, y=myhist$density)



myfit <- nlsLM(y~(A*exp(-lambda*x))
                ,data=df, start=list(A=1,lambda=1))



> sum(myhist$density)
[1] 5
> sum(predict(myfit))
[1] 4.931496

############################################################################
I would like sum(predict(myfit)) to be exactly 5 from the start,
without renormalising a posteriori the fit.

Any suggestion is appreciated.
Cheers

Lorenzo



More information about the R-help mailing list