MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: For Loop and Array related

  • To: mathgroup at smc.vnet.net
  • Subject: [mg58108] Re: For Loop and Array related
  • From: "Ray Koopman" <koopman at sfu.ca>
  • Date: Sat, 18 Jun 2005 06:08:21 -0400 (EDT)
  • References: <d8oucl$t6q$1@smc.vnet.net><d8u63h$84q$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

danl at wolfram.com wrote:
> [...]
> Here is a method that is quite fast. If you have len lists and they are
> to sum to total, instead of walking and incrementing total times, just
> pick len-1 random values from 0 to total, sort them, augment with 0 and
> total at the ends, and take successive differences. The code below will
> do this.
>
> randomLattice1[len_,total_] := ListConvolve[{1,-1},
>   Join[{0},Sort[Table[Random[Integer,{0,total}], {len-1}]],{total}]]

Yes it's fast, but the distribution of frequencies that it gives
is consistently much farther from uniform than would be expected
by chance under multinomial sampling. The slower method seems ok.

In[1]:= {k = 10, n = 100};

In[2]:= f = ListConvolve[{1,-1},
                Join[{0},Sort[Table[Random[Integer,n],{k-1}]],{n}]]
        {Length@f, Tr@f}
        {#,GammaRegularized[(k-1)/2,#/2]}&[With[{e = n/k},
                                                N@Tr[(f-e)^2]/e]]
Out[2]= {5, 7, 0, 10, 8, 6, 6, 46, 6, 6}
Out[3]= {10, 100}
Out[4]= {149.8, 9.7024*^-28}

In[5]:= g = Normal[SparseArray[#[[All,1]] -> Length /@ #, k]] &[
            Split@Sort@Table[Random[Integer,{1,k}],{n}]]
        {Length@g,Tr@g}
        {#,GammaRegularized[(k-1)/2,#/2]}&[With[{e = n/k},
                                                N@Tr[(g-e)^2]/e]]
Out[5]= {12, 13, 8, 7, 8, 13, 9, 10, 14, 6}
Out[6]= {10, 100}
Out[7]= {7.2, 0.616305}


  • Prev by Date: Re: Problems with my first package:Statistics`Common`RegressionCommon`BestFitParameters instead ofBestFitParameters
  • Next by Date: Re: Epilog -vs- Prolog -vs- DisplayTogether
  • Previous by thread: Re: For Loop and Array related
  • Next by thread: Re: For Loop and Array related