[Date Index]
[Thread Index]
[Author Index]
Re: weighted averages from 2 matrices
*To*: mathgroup at smc.vnet.net
*Subject*: [mg96139] Re: weighted averages from 2 matrices
*From*: dh <dh at metrohm.com>
*Date*: Fri, 6 Feb 2009 04:13:29 -0500 (EST)
*References*: <gmec5t$a8b$1@smc.vnet.net>
Hi Andreas,
If I understand correctly, you want to take an inner product of a row of
V with each row of S. We can do this by (denoting the column of V by t):
Inner[Times, t, #, Times] & /@ s
Then you want to take the mean of all thous values (it is more efficient
to do the division and addition outside the mean). Then we divide and add 1:
1 + (1/Length[t]) Mean[ Inner[Times, t, #, Times] & /@ S]
This operation is now mapped onto the rows of V:
(t = #;
1 + (1/Length[t]) Mean[ Inner[Times, t, #, Times] & /@ S]
) & /@ V
here we used a temporary variables to denote the argument of the outer
anonymous function.
hope this helps, Daniel
Andreas wrote:
> This builds on an earlier post, but involves a different enough problem, that I thought it merited a separate thread.
>
> I need to calculate a list or vector of weighted averages of n-dimensional samples, where the matrix "values" provides corresponding weights to the set of samples in matrix "samples" i.e.: I have two matrices:
>
> values = Array[V, ## &, {5, 4};
> samples = Array[S, ## &, {6, 4};
> Row[{" values =" MatrixForm[values, " ", "samples =" MatrixForm[samples}
>
> (* The code yields something like this *)
>
> V1,1 V1,2 V1,3 V1,4
> V2,1 V2,2 V2,3 V2,4
> V3,1 V3,2 V3,3 V3,4
> V4,1 V4,2 V4,3 V4,4
> V5,1 V5,2 V5,3 V5,4
>
> S1,1 S1,2 S1,3 S1,4
> S2,1 S2,2 S2,3 S2,4
> S3,1 S3,2 S3,3 S3,4
> S4,1 S4,2 S4,3 S4,4
> S5,1 S5,2 S5,3 S5,4
> S6,1 S6,2 S6,3 S6,4
>
> Columns in each matrix can run from 1 to n in number. Both matrices will always have the same number of columns. The number of rows in each matrix can differ one from the other, sometimes "values" might have more rows, sometimes "samples" will. In use samples will likely have around 1,000 to 10,000 rows and "values" may have more or less.
>
> Similar to what I asked about in an earlier post, I want to do the following:
>
> Multiply each element in a row of the matrix "values" by the corresponding column value in each of the rows of "samples", multiply the products of each new row, raise them to a power 1/(number of columns) and add one. Then calculate the Mean of that set relative to each row in values.
>
> So, for the first weighted average (using the first row of "values") I need to get something like this:
>
> Mean[{{1 + ((V1,1 * S1,1)*(V1,2 * S1,2)*(V1,3 * S1,3)*(V1,4 * S1,4))^(1/Length[values[[1)},
> {1 + ((V1,1 * S2,1)*(V1,2 * S2,2)*(V1,3 * S2,3)*(V1,4 * S2,4))^(1/Length[values[[1)},
> {1 + ((V1,1 * S3,1)*(V1,2 * S3,2)*(V1,3 * S3,3)*(V1,4 * S3,4))^(1/Length[values[[1)},
> {1 + ((V1,1 * S4,1)*(V1,2 * S4,2)*(V1,3 * S4,3)*(V1,4 * S4,4))^(1/Length[values[[1)},
> {1 + ((V1,1 * S5,1)*(V1,2 * S5,2)*(V1,3 * S5,3)*(V1,4 * S5,4))^(1/Length[values[[1)},
> {1 + ((V1,1 * S6,1)*(V1,2 * S6,2)*(V1,3 * S6,3)*(V1,4 * S6,4))^(1/Length[values[[1)}}]
>
>
> Then I need to Map that calculation in the same way across each successive row in the "values" matrix to generate my vector of weighted averages. I can do all of this iteratively, but I want to try thinking more like a mathematician or functional programmer.
>
> The following gives me the above, but only for the designated rows in each of the matrices:
>
>
> 1 + Inner[Times, values[[1]], samples[[1]], Times]^(1/Length[values[[1]]])
>
> 1 + (S1,1 * S1,2 * S1,3 * S1,4 * V1,1 * V1,2 * V1,3 * V1,4)^(1/4)
>
>
> Minor note, ideally I'd use a count of values > 0 rather than Length[[1]] in the above, but getting a 0 value in either matrix would happen very rarely.
>
> With some data:
>
> aSample = {{0.503171, 0.0178498, 0.351913, 0.127065}};
> aValue = {{1.03, 1.01, .987, 1.1}};
> 1 + Inner[Times, aValue[[1]], aSample[[1]], Times]^(1/Length[aValue[[1]]])
>
> 1.14594
>
>
> Building on that, I hoped I could use something with Map, Mean, and Inner. Something generally like:
>
> Map[Mean[Inner[Times, values, samples, Times]^(1 / Length[values[[1]]]) + 1], values]
>
> or adapt an earlier solution to a slightly different problem from Jean-Mark Gulliet on the order of:
>
>
> Clear[f1, f2, f3]
> f1 = Function[{values, samples}, 1 + Inner[Times, values, samples, Times]^(1/Length[values[[1]]])];
> f2 = Function[{values, samples}, Mean[(f1[#1, values] &) /@ samples]];
> f3[values_, samples_] := Map[(f2[samples, #1] &), values]
>
>
> f3[values, samples]
>
> But this doesn't work at all :-(
>
> I still need to make the shift from procedural to functional programing so I can't quite see the solution.
> I hope I've at least done a clear job of stating the problem.
>
> Any help much appreciated.
>
Prev by Date:
**Re: weighted averages from 2 matrices**
Next by Date:
**Re: Default Symbol Names in Package Functions**
Previous by thread:
**Re: weighted averages from 2 matrices**
Next by thread:
**Dashing**
| |