Hi, Thanks for your mail, and time It is not working for some arguments, when mean value is like >6. case mc0 <- c(0.08844446,0.1744455,0.1379778,0.1209769,0.1573065,0. 1134463,0.2074027) rv <-UnivarMixingDistribution(Norm(486.4255, 53.24133), Norm(664.0713, 3.674773), Norm(669.0484, 4.101381), Norm(677.1753, 4.869985), Norm(683.2635, 7.288175), Norm(727.6229, 37.64198), Norm(819.2011, 57.06655), mixCoeff=mc0/sum(mc0)) plot(rv, to.draw.arg="d") I am getting 731.1345 from the code you have provide It is part of a code, so it was difficult to write a reproducible code I have tried to use optimr but it gives me the local maxima, now I am struck with the problem of how to get the global maxima On Sat, Aug 26, 2017 at 3:12 PM, Ismail SEZEN [via R] < ml+s789695n4744993h60 at n4.nabble.com> wrote:> > > On 26 Aug 2017, at 14:18, Ulrik Stervbo <[hidden email] > <http:///user/SendEmail.jtp?type=node&node=4744993&i=0>> wrote: > > > > Please keep the list in cc. > > > > Sorry, it didn't work as expected. Maybe someone else have an > appropriate > > solution. > > > > Best, > > Ulrik > > > > On Sa., 26. Aug. 2017, 12:57 niharika singhal <[hidden email] > <http:///user/SendEmail.jtp?type=node&node=4744993&i=1>> > > wrote: > > > >> Hi > >> > >> Thanks for you mail, > >> I really appreciate your time on my problem > >> > >> I have posted this problem on > >> > >> > >> https://stats.stackexchange.com/questions/299590/to-find-max > ima-for-gaussian-mixture-model > >> > >> > >> The plot I am getting using UnivarMixingDistribution from distr package > in > >> R > >> > >> code is > >> > >> mc0= c(0.1241933, 0.6329082 <06329%20082>, 0.2428986 <02428%20986>) > >> rv > >> <-UnivarMixingDistribution(Norm(506.8644,61.02859),Norm(672.8448,9.149168),Norm( > > >> 829.902,74.84682), mixCoeff=mc0/sum(mc0)) > >> plot(rv, to.draw.arg="d") > >> > >> I want output around 672 in first case and in 2nd case around 2.1 > >> according to the plot. > >> your code will not work in both the scenario > >> > >> Regards > >> Niharika Singhal > >> > >> > >> On Sat, Aug 26, 2017 at 12:47 PM, Ulrik Stervbo <[hidden email] > <http:///user/SendEmail.jtp?type=node&node=4744993&i=2>> > >> wrote: > >> > >>> Hi, > >>> > >>> I once found this somewhere on stackoverflow: > >>> > >>> values <- rnorm(20, mean = c(2.15,2.0,2.9), sd = c(0.1,0.1,0.1)) > >>> > >>> v_dens <- density(values) > >>> v_dens_y <- v_dens$y > >>> > >>> r <- rle(v_dens_y) > >>> # These functions ignore the extremes if they are the first or last > point > >>> maxima_index <- which(rep(x = diff(sign(diff(c(-Inf, r$values, > -Inf)))) > >>> == -2, times = r$lengths)) > >>> minima_index <- which(rep(x = diff(sign(diff(c(-Inf, r$values, > -Inf)))) > >>> == 2, times = r$lengths)) > >>> > >>> plot(v_dens_y) > >>> > >>> HTH > >>> Ulrik > >>> > >>> > >>> On Sat, 26 Aug 2017 at 11:49 niharika singhal < > >>> [hidden email] <http:///user/SendEmail.jtp?type=node&node=4744993&i=3>> > wrote: > >>> > >>>> I have a Gaussian mixture model with some parameters > >>>> > >>>> mean=(506.8644,672.8448,829.902) > >>>> > >>>> sigma=(61.02859,9.149168,74.84682) > >>>> > >>>> c=(0.1241933, 0.6329082 <06329%20082>, 0.2428986 <02428%20986>) > >>>> > >>>> And the plot look something like below.[image: enter image > description > >>>> here] > >>>> <https://i.stack.imgur.com/4uUQ9.png> > >>>> > >>>> Also, if I change my parameters to > >>>> > >>>> mean=(2.15,2.0,2.9) > >>>> > >>>> sigma=(0.1,0.1,0.1) > >>>> > >>>> c=(1/3,1/3,1/3) > >>>> > >>>> Then plot would change to[image: enter image description here] > >>>> <https://i.stack.imgur.com/kESYX.png> > >>>> > >>>> Is there any way to find the maxima. I have tried Newton's method but > it > >>>> gave me the wrong output. > >>>> > >>>> Like in general some common solution, which would work on all the > cases, > >>>> is > >>>> needed.Can someone suggest me how can I achieve this > >>>> > >>>> Thanks in advance > >>>> > >>>> Niharika Singhal > >>>> > >>>> [[alternative HTML version deleted]] > >>>> > >>>> ______________________________________________ > >>>> [hidden email] > <http:///user/SendEmail.jtp?type=node&node=4744993&i=4> mailing list -- > To UNSUBSCRIBE and more, see > >>>> 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. > >>>> > >>> > >> > > See the solution below if it helps. Also please, create a minimal > reproduciple example as I did. So, we can investigate easily on your real > problem. > > set.seed(1) > m <- c(506.8644, 672.8448, 829.902) > sig <- c(61.02859, 9.149168, 74.84682) > x1 <- density(rnorm(500, mean = m, sd = sig)) > plot(x1) > > fun <- splinefun(x = x1$x, y = x1$y, method = "n") > optimize(fun, interval = range(x1$x), maximum = TRUE) > > > m <- c(2.15, 2.0, 2.9) > sig <- c(0.1,0.1,0.1) > x2 <- density(rnorm(500, mean = m, sd = sig)) > plot(x2) > > fun <- splinefun(x = x2$x, y = x2$y, method = "n") > optimize(fun, interval = range(x2$x), maximum = TRUE) > > ______________________________________________ > [hidden email] <http:///user/SendEmail.jtp?type=node&node=4744993&i=5> > mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posti > ng-guide.html > and provide commented, minimal, self-contained, reproducible code. > > > ------------------------------ > If you reply to this email, your message will be added to the discussion > below: > http://r.789695.n4.nabble.com/Find-maxima-of-a-function-OR-o > ptimization-of-a-Function-tp4744989p4744993.html > To unsubscribe from Find maxima of a function OR optimization of a > Function, click here > <http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4744989&code=bmloYXJpa2FzaW5naGFsMTk5MEBnbWFpbC5jb218NDc0NDk4OXwtNDI2MzAyOTM1> > . > NAML > <http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml> >[[alternative HTML version deleted]]

> On 26 Aug 2017, at 16:39, niharika singhal <niharikasinghal1990 at gmail.com> wrote: > > Hi, > > Thanks for your mail, and time > > It is not working for some arguments, when mean value is like >6. > > > case > > mc0 <- c(0.08844446,0.1744455,0.1379778,0.1209769,0.1573065,0. > 1134463,0.2074027) > > rv <-UnivarMixingDistribution(Norm(486.4255, 53.24133), > > Norm(664.0713, 3.674773), > > Norm(669.0484, 4.101381), > > Norm(677.1753, 4.869985), > > Norm(683.2635, 7.288175), > > Norm(727.6229, 37.64198), > > Norm(819.2011, 57.06655), > > mixCoeff=mc0/sum(mc0)) > > plot(rv, to.draw.arg="d") > > > I am getting 731.1345 from the code you have provide > > > It is part of a code, so it was difficult to write a reproducible code > > I have tried to use optimr but it gives me the local maxima, now I am > struck with the problem of how to get the global maxima >This is basically an optimization problem so it?s nothing to do with distr package and UnivarMixingDistribution function. Also I?m not able to install distr package because of an error. Can you create an example by rnorm function? or use dput to share x and y outputs of UnivarMixingDistribution function and I can look for the issue. Also You might have multiple maximas has same maximum y-values (for instance, y = sin(x)) and this definition is relevant to defined interval. For instance again, we can talk about a global extremum for a function like y = ax^2+bx + c. We know we will have only a single extremum point (maxima or minima). For higher order functions, we can talk about only local maximas. So, I assume you want to obtain maximum one of this local maximas, right? If yes, why don?t we find the maximum y-value and corresponding x-value as follows? y <- c(1,2,3,4,3,2,3,4,5,6,7,8,9,8,7,6,5,6,7,6,5) x <- 1:length(y) fun <- splinefun(x = x, y = y, method = "n") x2 <- seq(1, max(x), 0.1) y2 <- fun(x2) plot(x, y, type = "l") lines(x2, y2, col = "red") max.x <- optimize(fun, interval = range(x), maximum = TRUE) print(max.x) # x coordinate of global maximum of y by spline and optimize x[which(y == max(y))] # global maximum of dicrete x-y vectors spline function uses cubic spline method to obtain the undefined values in a discrete series and optimize function calculates EXACT LOCATION of the extremum. I suspect we have a communication failure :)

Just a quick note: "I have tried to use optimr but it gives me the local maxima, now I am struck with the problem of how to get the global maxima" This is a profound misunderstanding of how numerical optimization works. **All** numerical optimizers can provide only "local" maxima (the exact meaning of "local" may vary). It is mathematically impossible to numerically obtain "global" maxima and know that you have done so.* -- Bert *Unless, of course, you can prove analytically that only global maxima exist or some other such supplementary mathematical information. Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sat, Aug 26, 2017 at 6:39 AM, niharika singhal <niharikasinghal1990 at gmail.com> wrote:> Hi, > > Thanks for your mail, and time > > It is not working for some arguments, when mean value is like >6. > > > case > > mc0 <- c(0.08844446,0.1744455,0.1379778,0.1209769,0.1573065,0. > 1134463,0.2074027) > > rv <-UnivarMixingDistribution(Norm(486.4255, 53.24133), > > Norm(664.0713, 3.674773), > > Norm(669.0484, 4.101381), > > Norm(677.1753, 4.869985), > > Norm(683.2635, 7.288175), > > Norm(727.6229, 37.64198), > > Norm(819.2011, 57.06655), > > mixCoeff=mc0/sum(mc0)) > > plot(rv, to.draw.arg="d") > > > I am getting 731.1345 from the code you have provide > > > It is part of a code, so it was difficult to write a reproducible code > > I have tried to use optimr but it gives me the local maxima, now I am > struck with the problem of how to get the global maxima > > > On Sat, Aug 26, 2017 at 3:12 PM, Ismail SEZEN [via R] < > ml+s789695n4744993h60 at n4.nabble.com> wrote: > >> >> > On 26 Aug 2017, at 14:18, Ulrik Stervbo <[hidden email] >> <http:///user/SendEmail.jtp?type=node&node=4744993&i=0>> wrote: >> > >> > Please keep the list in cc. >> > >> > Sorry, it didn't work as expected. Maybe someone else have an >> appropriate >> > solution. >> > >> > Best, >> > Ulrik >> > >> > On Sa., 26. Aug. 2017, 12:57 niharika singhal <[hidden email] >> <http:///user/SendEmail.jtp?type=node&node=4744993&i=1>> >> > wrote: >> > >> >> Hi >> >> >> >> Thanks for you mail, >> >> I really appreciate your time on my problem >> >> >> >> I have posted this problem on >> >> >> >> >> >> https://stats.stackexchange.com/questions/299590/to-find-max >> ima-for-gaussian-mixture-model >> >> >> >> >> >> The plot I am getting using UnivarMixingDistribution from distr package >> in >> >> R >> >> >> >> code is >> >> >> >> mc0= c(0.1241933, 0.6329082 <06329%20082>, 0.2428986 <02428%20986>) >> >> rv >> >> <-UnivarMixingDistribution(Norm(506.8644,61.02859),Norm(672.8448,9.149168),Norm( >> >> >> 829.902,74.84682), mixCoeff=mc0/sum(mc0)) >> >> plot(rv, to.draw.arg="d") >> >> >> >> I want output around 672 in first case and in 2nd case around 2.1 >> >> according to the plot. >> >> your code will not work in both the scenario >> >> >> >> Regards >> >> Niharika Singhal >> >> >> >> >> >> On Sat, Aug 26, 2017 at 12:47 PM, Ulrik Stervbo <[hidden email] >> <http:///user/SendEmail.jtp?type=node&node=4744993&i=2>> >> >> wrote: >> >> >> >>> Hi, >> >>> >> >>> I once found this somewhere on stackoverflow: >> >>> >> >>> values <- rnorm(20, mean = c(2.15,2.0,2.9), sd = c(0.1,0.1,0.1)) >> >>> >> >>> v_dens <- density(values) >> >>> v_dens_y <- v_dens$y >> >>> >> >>> r <- rle(v_dens_y) >> >>> # These functions ignore the extremes if they are the first or last >> point >> >>> maxima_index <- which(rep(x = diff(sign(diff(c(-Inf, r$values, >> -Inf)))) >> >>> == -2, times = r$lengths)) >> >>> minima_index <- which(rep(x = diff(sign(diff(c(-Inf, r$values, >> -Inf)))) >> >>> == 2, times = r$lengths)) >> >>> >> >>> plot(v_dens_y) >> >>> >> >>> HTH >> >>> Ulrik >> >>> >> >>> >> >>> On Sat, 26 Aug 2017 at 11:49 niharika singhal < >> >>> [hidden email] <http:///user/SendEmail.jtp?type=node&node=4744993&i=3>> >> wrote: >> >>> >> >>>> I have a Gaussian mixture model with some parameters >> >>>> >> >>>> mean=(506.8644,672.8448,829.902) >> >>>> >> >>>> sigma=(61.02859,9.149168,74.84682) >> >>>> >> >>>> c=(0.1241933, 0.6329082 <06329%20082>, 0.2428986 <02428%20986>) >> >>>> >> >>>> And the plot look something like below.[image: enter image >> description >> >>>> here] >> >>>> <https://i.stack.imgur.com/4uUQ9.png> >> >>>> >> >>>> Also, if I change my parameters to >> >>>> >> >>>> mean=(2.15,2.0,2.9) >> >>>> >> >>>> sigma=(0.1,0.1,0.1) >> >>>> >> >>>> c=(1/3,1/3,1/3) >> >>>> >> >>>> Then plot would change to[image: enter image description here] >> >>>> <https://i.stack.imgur.com/kESYX.png> >> >>>> >> >>>> Is there any way to find the maxima. I have tried Newton's method but >> it >> >>>> gave me the wrong output. >> >>>> >> >>>> Like in general some common solution, which would work on all the >> cases, >> >>>> is >> >>>> needed.Can someone suggest me how can I achieve this >> >>>> >> >>>> Thanks in advance >> >>>> >> >>>> Niharika Singhal >> >>>> >> >>>> [[alternative HTML version deleted]] >> >>>> >> >>>> ______________________________________________ >> >>>> [hidden email] >> <http:///user/SendEmail.jtp?type=node&node=4744993&i=4> mailing list -- >> To UNSUBSCRIBE and more, see >> >>>> 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. >> >>>> >> >>> >> >> >> >> See the solution below if it helps. Also please, create a minimal >> reproduciple example as I did. So, we can investigate easily on your real >> problem. >> >> set.seed(1) >> m <- c(506.8644, 672.8448, 829.902) >> sig <- c(61.02859, 9.149168, 74.84682) >> x1 <- density(rnorm(500, mean = m, sd = sig)) >> plot(x1) >> >> fun <- splinefun(x = x1$x, y = x1$y, method = "n") >> optimize(fun, interval = range(x1$x), maximum = TRUE) >> >> >> m <- c(2.15, 2.0, 2.9) >> sig <- c(0.1,0.1,0.1) >> x2 <- density(rnorm(500, mean = m, sd = sig)) >> plot(x2) >> >> fun <- splinefun(x = x2$x, y = x2$y, method = "n") >> optimize(fun, interval = range(x2$x), maximum = TRUE) >> >> ______________________________________________ >> [hidden email] <http:///user/SendEmail.jtp?type=node&node=4744993&i=5> >> mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posti >> ng-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> >> >> ------------------------------ >> If you reply to this email, your message will be added to the discussion >> below: >> http://r.789695.n4.nabble.com/Find-maxima-of-a-function-OR-o >> ptimization-of-a-Function-tp4744989p4744993.html >> To unsubscribe from Find maxima of a function OR optimization of a >> Function, click here >> <http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4744989&code=bmloYXJpa2FzaW5naGFsMTk5MEBnbWFpbC5jb218NDc0NDk4OXwtNDI2MzAyOTM1> >> . >> NAML >> <http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml> >> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.