# Latin Hypercube with condition sum = 1

 Classic List Threaded
7 messages
Reply | Threaded
Open this post in threaded view
|

## Latin Hypercube with condition sum = 1

 Hi I want to du a sensitivity analysis using Latin Hypercubes. But my parameters have to fulfill two conditions: 1) ranging from 0 to 1 2) have to sum up to 1 So far I am using the lhs package and am doing the following: library(lhs) ws <- improvedLHS(1000, 7) wsSums <- rowSums(ws) wss <- ws / wsSums but I think I can't do that, as after the normalization > min(wss) [1] 0.0001113015 > max(wss) [1] 0.5095729 Therefore my question: how can I create a Latin Hypercube whicgh fulfills the conditions 1) and 2)? Thanks a lot Rainer -- Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany) Centre of Excellence for Invasion Biology Faculty of Science Natural Sciences Building Private Bag X1 University of Stellenbosch Matieland 7602 South Africa ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: Latin Hypercube with condition sum = 1

 Rainer M Krug gmail.com> writes: > > Hi > > I want to du a sensitivity analysis using Latin Hypercubes. But my > parameters have to fulfill two conditions: > > 1) ranging from 0 to 1 > 2) have to sum up to 1 >   Perhaps I'm missing something, but it seems that there's only one vector that satisfies these two conditions simultaneously, which is x={0,1} ... ???  (Actually, technically speaking you could have as many zeros as you wanted -- x = {0,0,0,1} would also work -- but I don't think that's going to help much.) If min(x)=0 and max(x)=1, then x must contain 0 and 1.  If x contains any other non-zero values, and no negative ones, then sum(x)>1 ...    You seem to have a problem either in your definitions, or in your communication of those definitions to us ...   cheers    Ben Bolker ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: Latin Hypercube with condition sum = 1

 On Sun, Nov 23, 2008 at 7:43 PM, Ben Bolker <[hidden email]> wrote: > Rainer M Krug gmail.com> writes: > >> >> Hi >> >> I want to du a sensitivity analysis using Latin Hypercubes. But my >> parameters have to fulfill two conditions: >> >> 1) ranging from 0 to 1 >> 2) have to sum up to 1 >> > >  Perhaps I'm missing something, but it seems that there's > only one vector that satisfies these two conditions simultaneously, > which is x={0,1} ... ???  (Actually, technically speaking > you could have as many zeros as you wanted -- x = {0,0,0,1} > would also work -- but I don't think that's going to help much.) > If min(x)=0 and max(x)=1, then x must contain 0 and 1.  If x > contains any other non-zero values, and no negative > ones, then sum(x)>1 ... You are absolutely right - but when you look at the example, I want that the FINAL latin hypercube sets sum up to one, and the values RANGING from 0 to 1, i.e. (0.5, 0.5) would also satisfy the condition. > >   You seem to have a problem either in your definitions, > or in your communication of those definitions to us ... Probably both - but to try again: 1) values ranging between 0 and 1 (0 <= x <= 1) 2) all elements in one set of the lLatin Hypercube should add to one. Assuming two dimensions, (1,0) & (0,1) & (0.3, 0.7) , ... would all satisfy the condition 1 and 2; (0.8, 0.3) would satisfy 1, but not 2; (-1, 2) would satisfy 2, but not 1 Hope this clarifies my question, Rainer > >  cheers >   Ben Bolker > > ______________________________________________ > [hidden email] 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. > -- Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany) Centre of Excellence for Invasion Biology Faculty of Science Natural Sciences Building Private Bag X1 University of Stellenbosch Matieland 7602 South Africa ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: Latin Hypercube with condition sum = 1

 In that case won't x2 <- (x-min(x))/(max(x)-min(x)) ## establishes criterion 1 x3 <- x2/sum(x2)                 ## establishes criterion 2  work?  I haven't thought about this very carefully, but since transformation makes min(x)=0 and max(x)=1 (and therefore sum(x)>1 -- although it won't work if length(x)<2 or var(x)==0) transformation 2 will necessarily reduce the range and therefore not violate criterion 1 ... ?  Ben Bolker Rainer M Krug wrote: > On Sun, Nov 23, 2008 at 7:43 PM, Ben Bolker <[hidden email]> wrote: >> Rainer M Krug gmail.com> writes: >> >>> Hi >>> >>> I want to du a sensitivity analysis using Latin Hypercubes. But my >>> parameters have to fulfill two conditions: >>> >>> 1) ranging from 0 to 1 >>> 2) have to sum up to 1 >>> >>  Perhaps I'm missing something, but it seems that there's >> only one vector that satisfies these two conditions simultaneously, >> which is x={0,1} ... ???  (Actually, technically speaking >> you could have as many zeros as you wanted -- x = {0,0,0,1} >> would also work -- but I don't think that's going to help much.) >> If min(x)=0 and max(x)=1, then x must contain 0 and 1.  If x >> contains any other non-zero values, and no negative >> ones, then sum(x)>1 ... > > You are absolutely right - but when you look at the example, I want > that the FINAL latin hypercube sets sum up to one, and the values > RANGING from 0 to 1, i.e. (0.5, 0.5) would also satisfy the condition. > >>   You seem to have a problem either in your definitions, >> or in your communication of those definitions to us ... > > Probably both - but to try again: > > 1) values ranging between 0 and 1 (0 <= x <= 1) > 2) all elements in one set of the lLatin Hypercube should add to one. > > Assuming two dimensions, (1,0) & (0,1) & (0.3, 0.7) , ... would all > satisfy the condition 1 and 2; > > (0.8, 0.3) would satisfy 1, but not 2; > (-1, 2) would satisfy 2, but not 1 > > Hope this clarifies my question, > > Rainer > >>  cheers >>   Ben Bolker >> >> ______________________________________________ >> [hidden email] 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. >> > > > -- Ben Bolker Associate professor, Biology Dep't, Univ. of Florida [hidden email] / www.zoology.ufl.edu/bolker GPG key: www.zoology.ufl.edu/bolker/benbolker-publickey.asc ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: Latin Hypercube with condition sum = 1

 On Sun, Nov 23, 2008 at 8:06 PM, Ben Bolker <[hidden email]> wrote: >  In that case won't > > x2 <- (x-min(x))/(max(x)-min(x)) ## establishes criterion 1 That one is not needed, as the range is automatically between 0 and 1: min(ws) [1] 6.925322e-05 > max(ws) [1] 0.9999284 so that is fine. but when I do the normalization, i.e. the sum of each row has to be 1: wss <- ws / rowSums(ws) the min and max change to: > min(wss) [1] 0.0003943225 > max(wss) [1] 0.4834845 But there is also the possibility, that one value ws[n,1] is larger then one (or one), and the others are zero (or sum(ws[n,-1] == 1-ws[n,1]), e.g.  (1,0,0,0,0,0,0). In addition, I think that the resulting cube is not a Latin Hypercube any more. > x3 <- x2/sum(x2)                 ## establishes criterion 2 > >  work?  I haven't thought about this very carefully, but > since transformation makes min(x)=0 and max(x)=1 (and therefore > sum(x)>1 -- although it won't work if length(x)<2 or var(x)==0) > transformation 2 will necessarily reduce the range and therefore > not violate criterion 1 ... ? > >  Ben Bolker > > > Rainer M Krug wrote: >> On Sun, Nov 23, 2008 at 7:43 PM, Ben Bolker <[hidden email]> wrote: >>> Rainer M Krug gmail.com> writes: >>> >>>> Hi >>>> >>>> I want to du a sensitivity analysis using Latin Hypercubes. But my >>>> parameters have to fulfill two conditions: >>>> >>>> 1) ranging from 0 to 1 >>>> 2) have to sum up to 1 >>>> >>>  Perhaps I'm missing something, but it seems that there's >>> only one vector that satisfies these two conditions simultaneously, >>> which is x={0,1} ... ???  (Actually, technically speaking >>> you could have as many zeros as you wanted -- x = {0,0,0,1} >>> would also work -- but I don't think that's going to help much.) >>> If min(x)=0 and max(x)=1, then x must contain 0 and 1.  If x >>> contains any other non-zero values, and no negative >>> ones, then sum(x)>1 ... >> >> You are absolutely right - but when you look at the example, I want >> that the FINAL latin hypercube sets sum up to one, and the values >> RANGING from 0 to 1, i.e. (0.5, 0.5) would also satisfy the condition. >> >>>   You seem to have a problem either in your definitions, >>> or in your communication of those definitions to us ... >> >> Probably both - but to try again: >> >> 1) values ranging between 0 and 1 (0 <= x <= 1) >> 2) all elements in one set of the lLatin Hypercube should add to one. >> >> Assuming two dimensions, (1,0) & (0,1) & (0.3, 0.7) , ... would all >> satisfy the condition 1 and 2; >> >> (0.8, 0.3) would satisfy 1, but not 2; >> (-1, 2) would satisfy 2, but not 1 >> >> Hope this clarifies my question, >> >> Rainer >> >>>  cheers >>>   Ben Bolker >>> >>> ______________________________________________ >>> [hidden email] 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. >>> >> >> >> > > > -- > Ben Bolker > Associate professor, Biology Dep't, Univ. of Florida > [hidden email] / www.zoology.ufl.edu/bolker > GPG key: www.zoology.ufl.edu/bolker/benbolker-publickey.asc > -- Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany) Centre of Excellence for Invasion Biology Faculty of Science Natural Sciences Building Private Bag X1 University of Stellenbosch Matieland 7602 South Africa ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: Latin Hypercube with condition sum = 1

 In reply to this post by Rainer M Krug-6 Rainer M Krug gmail.com> writes: > > Hi > > I want to du a sensitivity analysis using Latin Hypercubes. But my > parameters have to fulfill two conditions: > > 1) ranging from 0 to 1 > 2) have to sum up to 1 > > So far I am using the lhs package and am doing the following: > > library(lhs) > ws <- improvedLHS(1000, 7) > wsSums <- rowSums(ws) > wss <- ws / wsSums > > but I think I can't do that, as after the normalization > > > min(wss) > [1] 0.0001113015 > > max(wss) > [1] 0.5095729 > > Therefore my question: how can I create a Latin Hypercube whicgh > fulfills the conditions 1) and 2)? > > Thanks a lot > > Rainer > Rainer, Your original solution meets your two conditions.  The problem for you (I think) is that you'd like the result to have values near zero and near one.   I have an imperfect solution to your problem using a Dirichlet distribution.   The Dirichlet seems to keep the range of the values larger once they are normalized.  The result is not uniformly distributed on (0,1) anymore, but instead is Dirichlet distributed with the parameters alpha.  The Latin properties are maintained. require(lhs) qdirichlet <- function(X, alpha) {   # qdirichlet is not an exact quantile function since the quantile of a   #  multivariate distribtion is not unique   # qdirichlet is also not the quantiles of the marginal distributions since   #  those quantiles do not sum to one   # qdirichlet is the quantile of the underlying gamma functions, normalized   # This has been tested to show that qdirichlet approximates the dirichlet   #  distribution well and creates the correct marginal means and variances   #  when using a latin hypercube sample   lena <- length(alpha)   stopifnot(is.matrix(X))   sims <- dim(X)[1]   stopifnot(dim(X)[2] == lena)   if(any(is.na(alpha)) || any(is.na(X)))     stop("NA values not allowed in qdirichlet")   Y <- matrix(0, nrow=sims, ncol=lena)   ind <- which(alpha != 0)   for(i in ind)   {     Y[,i] <- qgamma(X[,i], alpha[i], 1)   }   Y <- Y / rowSums(Y)   return(Y) } X <- randomLHS(1000, 7) Y <- qdirichlet(X, rep(1,7)) stopifnot(all(abs(rowSums(Y)-1) < 1E-12)) range(Y) ws <- randomLHS(1000, 7) wsSums <- rowSums(ws) wss <- ws / wsSums stopifnot(all(abs(rowSums(wss)-1) < 1E-12)) range(wss) I hope this helps! Rob Rob Carnell Battelle Principal Research Scientist ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: Latin Hypercube with condition sum = 1

 On Tue, Nov 25, 2008 at 4:16 PM, Rob Carnell <[hidden email]> wrote: > Rainer M Krug gmail.com> writes: > >> >> Hi >> >> I want to du a sensitivity analysis using Latin Hypercubes. But my >> parameters have to fulfill two conditions: >> >> 1) ranging from 0 to 1 >> 2) have to sum up to 1 >> >> So far I am using the lhs package and am doing the following: >> >> library(lhs) >> ws <- improvedLHS(1000, 7) >> wsSums <- rowSums(ws) >> wss <- ws / wsSums >> >> but I think I can't do that, as after the normalization >> >> > min(wss) >> [1] 0.0001113015 >> > max(wss) >> [1] 0.5095729 >> >> Therefore my question: how can I create a Latin Hypercube whicgh >> fulfills the conditions 1) and 2)? >> >> Thanks a lot >> >> Rainer >> > > Rainer, > > Your original solution meets your two conditions.  The problem for you (I > think) is that you'd like the result to have values near zero and near one. > > I have an imperfect solution to your problem using a Dirichlet distribution. > The Dirichlet seems to keep the range of the values larger once they are > normalized.  The result is not uniformly distributed on (0,1) anymore, but > instead is Dirichlet distributed with the parameters alpha.  The Latin > properties are maintained. Thanks a lot Rob. I'll look into the solution which sounds good. Thanks Rainer > > require(lhs) > > qdirichlet <- function(X, alpha) > { >  # qdirichlet is not an exact quantile function since the quantile of a >  #  multivariate distribtion is not unique >  # qdirichlet is also not the quantiles of the marginal distributions since >  #  those quantiles do not sum to one >  # qdirichlet is the quantile of the underlying gamma functions, normalized >  # This has been tested to show that qdirichlet approximates the dirichlet >  #  distribution well and creates the correct marginal means and variances >  #  when using a latin hypercube sample >  lena <- length(alpha) >  stopifnot(is.matrix(X)) >  sims <- dim(X)[1] >  stopifnot(dim(X)[2] == lena) >  if(any(is.na(alpha)) || any(is.na(X))) >    stop("NA values not allowed in qdirichlet") > >  Y <- matrix(0, nrow=sims, ncol=lena) >  ind <- which(alpha != 0) >  for(i in ind) >  { >    Y[,i] <- qgamma(X[,i], alpha[i], 1) >  } >  Y <- Y / rowSums(Y) >  return(Y) > } > > X <- randomLHS(1000, 7) > Y <- qdirichlet(X, rep(1,7)) > stopifnot(all(abs(rowSums(Y)-1) < 1E-12)) > range(Y) > > ws <- randomLHS(1000, 7) > wsSums <- rowSums(ws) > wss <- ws / wsSums > stopifnot(all(abs(rowSums(wss)-1) < 1E-12)) > range(wss) > > I hope this helps! > Rob > > Rob Carnell > Battelle > Principal Research Scientist > > ______________________________________________ > [hidden email] 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. > -- Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany) Centre of Excellence for Invasion Biology Faculty of Science Natural Sciences Building Private Bag X1 University of Stellenbosch Matieland 7602 South Africa ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.