MathGroup Archive 2005

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

Search the Archive

Re: Weighted histogram


kalymereau at yahoo.fr wrote:
> Thank you for the answers but it seems that neither the solution of Bob
> nor the one of Ray works.
>
> I want that each value in a be weighted by both its occurrence in the
> list a and by the corresponding weight in w. In other words, with
> a={1,1,1,2,3,4} and w={1,1,1,1,2,1}
> I want the histogram of b={1,1,1,2,3,3,4}
>
> Obviously this is not what does Bob's proposal.
>
> When w is a list of integers it is if course very easy to construct b
> from a and w: just duplicates the elements of a according to their
> weights in w.
>
> When a is a list of integers then it seems that the solution of Ray
> works (although I don't really understand it).
>
> But when both a and w are lists of real numbers, then it doesn't work.
>
> It is possible to construct a binning and then to fill each bin by the
> corresponding values in a ponderated by the weights in w. But the tests
> I made in this direction were very slow for large lists, and I would
> like something more natural.
>
> It seems to me to be a very basic problem of data analysis, any other
> idea ?
>
> Thanks.

There are two distinct problems here. To convert a vector a of
general reals to a vector b of integer bin numbers that run from
1...m, try

b = Ceiling @ Rescale[ a, {Min@a, Max@a}, {$MachineEpsilon*m, m} ];

To construct the vector of weighted frequencies, try

f = Normal @ SparseArray[ Rule[ #[[1,1]], Tr[#[[All,2]]] ]& /@
    Split[ Sort @ Transpose @ {b,w}, #1[[1]] == #2[[1]] & ] ];

I'm posting from a machine that does not have Mathematica on it,
so both the above are untried.

In my previous post, I normalized f to a vector of proportions:

p = f/Tr@f;

That was just a personal preference, to make the values more
interpretable.


  • Prev by Date: Re: Programmatic Search and Replace
  • Next by Date: Re: Re: Hardcopy or electronic books?
  • Previous by thread: Re: Re: Weighted histogram
  • Next by thread: Export to PDF