MathGroup Archive 2009

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

Search the Archive

Re: weighted averages from 2 matrices

  • To: mathgroup at smc.vnet.net
  • Subject: [mg96138] Re: [mg96108] weighted averages from 2 matrices
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Fri, 6 Feb 2009 04:13:18 -0500 (EST)
  • References: <200902050939.EAA10464@smc.vnet.net>
  • Reply-to: drmajorbob at longhorns.com

OK, I'd suggest you remove about 14 typos and repost workable code.

Edit>Copy as Plain Text, then paste into e-mail.

Here's my guess at the first part of it, but I'm not willing to waste more  
time:

values = Array[V, {5, 4}];
samples = Array[S, {6, 4}];
Row[" values =" , MatrixForm[values], "   ", "samples =",
  MatrixForm[samples]]

Row[" values =", {{V[1, 1], V[1, 2], V[1, 3], V[1, 4]}, {V[2, 1],
    V[2, 2], V[2, 3], V[2, 4]}, {V[3, 1], V[3, 2], V[3, 3],
    V[3, 4]}, {V[4, 1], V[4, 2], V[4, 3], V[4, 4]}, {V[5, 1], V[5, 2],
    V[5, 3], V[5, 4]}}, "   ", "samples =", {{S[1, 1], S[1, 2],
    S[1, 3], S[1, 4]}, {S[2, 1], S[2, 2], S[2, 3], S[2, 4]}, {S[3, 1],
    S[3, 2], S[3, 3], S[3, 4]}, {S[4, 1], S[4, 2], S[4, 3],
    S[4, 4]}, {S[5, 1], S[5, 2], S[5, 3], S[5, 4]}, {S[6, 1], S[6, 2],
    S[6, 3], S[6, 4]}}]

Bobby

On Thu, 05 Feb 2009 03:39:33 -0600, Andreas <aagas at ix.netcom.com> 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.
>



-- 
DrMajorBob at longhorns.com


  • Prev by Date: Re: FindFit & NIntegrate
  • Next by Date: Re: weighted averages from 2 matrices
  • Previous by thread: weighted averages from 2 matrices
  • Next by thread: Re: weighted averages from 2 matrices