Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
limit the sum

Subject: limit the sum

From: Catherine

Date: 5 Jul, 2011 04:54:09

Message: 1 of 3

Hi,

I have been using Matlab to generate presentation durations for a series of stimuli. The duration of each stimulus is selected at random from a specified range.

Is it possible to constrain the combination of durations generated so that the total does not exceed some predetermined amount?

Any help greatly appreciated,

Catherine

Subject: limit the sum

From: Roger Stafford

Date: 5 Jul, 2011 05:55:10

Message: 2 of 3

"Catherine " <corr@unimelb.edu.au> wrote in message <iuu5dh$p5n$1@newscl01ah.mathworks.com>...
> Hi,
>
> I have been using Matlab to generate presentation durations for a series of stimuli. The duration of each stimulus is selected at random from a specified range.
>
> Is it possible to constrain the combination of durations generated so that the total does not exceed some predetermined amount?
>
> Any help greatly appreciated,
>
> Catherine
- - - - - - - - -
  Let me pose that problem in different terms. You want to randomly select a set of values t1, t2, t3, ..., tn such that 0<=ti<=r for some limit r, but with the additional constraint that t1+t2+t3+...+tn <= s for s a "predetermined amount". Moreover I presume you want the probability to be that of a valid conditional probability given that constraint without any skewing of probabilities.

  I was afraid someone would someday pose that problem. I wrote a routine some years ago (randfixedsum) of a similar nature except that the constraint was that their sum must be equal to s rather than less than or equal, and it was no easy task. I suspect your problem has a direct solution free from rejection, but it would require a similar effort. The difficulty is that with a large value for n, the structure of the n-dimensional polytope that corresponds to combinations that satisfy the constraint can become unbelievably complicated.

  It is relatively easy to program such a process if you generate combinations without constraint and then practice rejection of those that exceed the limit. The drawback is that one cannot guarantee how long that process can take. You can only make statistical claims about the computation time required, and this will depend on how small s is relative to n*r - the smaller s/(n*r) is, the higher the percentage of rejections.

  Here is how the latter would work:

t = s+1;
while sum(t)>s
 t = r*rand(n,1);
end

You can try it and see if it is satisfactory for your purposes.

  (Note: If s <= r where the rejection rate could become unacceptably high with a large n, the above polytope becomes only a simplex which can be handled with relative ease.)
  
Roger Stafford

Subject: limit the sum

From: Catherine

Date: 5 Jul, 2011 06:26:09

Message: 3 of 3

"Roger Stafford" wrote in message <iuu8vu$46j$1@newscl01ah.mathworks.com>...
> "Catherine " <corr@unimelb.edu.au> wrote in message <iuu5dh$p5n$1@newscl01ah.mathworks.com>...
> > Hi,
> >
> > I have been using Matlab to generate presentation durations for a series of stimuli. The duration of each stimulus is selected at random from a specified range.
> >
> > Is it possible to constrain the combination of durations generated so that the total does not exceed some predetermined amount?
> >
> > Any help greatly appreciated,
> >
> > Catherine
> - - - - - - - - -
> Let me pose that problem in different terms. You want to randomly select a set of values t1, t2, t3, ..., tn such that 0<=ti<=r for some limit r, but with the additional constraint that t1+t2+t3+...+tn <= s for s a "predetermined amount". Moreover I presume you want the probability to be that of a valid conditional probability given that constraint without any skewing of probabilities.
>
> I was afraid someone would someday pose that problem. I wrote a routine some years ago (randfixedsum) of a similar nature except that the constraint was that their sum must be equal to s rather than less than or equal, and it was no easy task. I suspect your problem has a direct solution free from rejection, but it would require a similar effort. The difficulty is that with a large value for n, the structure of the n-dimensional polytope that corresponds to combinations that satisfy the constraint can become unbelievably complicated.
>
> It is relatively easy to program such a process if you generate combinations without constraint and then practice rejection of those that exceed the limit. The drawback is that one cannot guarantee how long that process can take. You can only make statistical claims about the computation time required, and this will depend on how small s is relative to n*r - the smaller s/(n*r) is, the higher the percentage of rejections.
>
> Here is how the latter would work:
>
> t = s+1;
> while sum(t)>s
> t = r*rand(n,1);
> end
>
> You can try it and see if it is satisfactory for your purposes.
>
> (Note: If s <= r where the rejection rate could become unacceptably high with a large n, the above polytope becomes only a simplex which can be handled with relative ease.)
>
> Roger Stafford

Hi Roger,

Thank you for responding so quickly and clearly. You have understood my issue exactly. Up to this point I have been doing as you suggest (generating combinations and rejecting those that exceed 's'), but I thought there may be a more efficient way of approaching it.

I will have a look at randfixedsum and see if that gets me close enough to where I need to be.

Thanks again,

Catherine

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us