# Generate weights for mean, such that mean of weighted means is closest to $G$, while minimising std. dev. of weights

Given some fixed 'target' $$T=150$$, and a set of sets of arbitrary numbers $$G$$ in the range $$0 < x \leq T\ln\left(T\right)$$, for instance:

$$G=\left[ \begin{array}{ccc} p_1&p_2&p_3&p_4&p_5&p_6\\ \hline 204&216&2&597&302&401\\ 188&551&478&417&71&383\\ 314&583&203&42&189&510\\ 231&64&298&95&23&312\\ 417&210&112&224&90&432\\ 495&297&490&580&361&477\\ 298&163&568&590&64&388\\ 175&360&526&583&352&96 \end{array} \right]$$

... we can simply taking the mean of each of these rows, resulting in (rounded to nearest integer):

$$M=\{\operatorname{mean}(d) : d \in G \}=\left[ \begin{array}{ccc} 287\\ 348\\ 307\\ 171\\ 248\\ 450\\ 345\\ 349 \end{array} \right]$$

The mean of these means is then simply evaluated to be $$\approx313$$ - not very close to our target, $$T$$. However, by weighting each of $$p_x$$ by some 'weight', we can alter the final mean of means quite significantly. For instance, by multiplying the value of each column as follows:

$$W=\left[ \begin{array}{c|c} p_1&p_2&p_3&p_4&p_5&p_6\\ \hline \frac{1}{2}&\frac{2}{5}&\frac{2}{7}&\frac{1}{2}&\frac{1}{2}&\frac{2}{3}\\ \end{array} \right]$$

we get the weighted grid:

$$G'=\left[ \begin{array}{ccc} p_1\cdot W'_1&p_2\cdot W'_2&p_3\cdot W'_3&p_4\cdot W'_4&p_5\cdot W'_5&p_6\cdot W'_6\\ \hline 102&86&1&299&151&267\\94&220&137&209&36&255\\157&233&58&21&95&340\\116&26&85&48&12&208\\209&84&32&112&45&288\\248&119&140&290&181&318\\149&65&162&295&32&259\\88&144&150&292&176&64 \end{array} \right]$$

giving us the table of means:

$$M'=\left[ \begin{array}{ccc} 151\\159\\151\\83\\128\\216\\160\\152 \end{array} \right]$$

which gives us the final value as $$\operatorname{mean}(M')\approx 150=T$$, when all is rounded.

However, although these 'weights' do work to give us a result, they are all quite small (less than $$1$$, in fact, so I'd hesitate to even call them weights as they function more as coefficients). Ideally, $$\frac{\sum{W}}{|W|}$$ should be greater than one, where possible.

Therefore, I am looking for an algorithm, formula, method, or even citation regarding how I might go about evaluating the members of set $$W$$ given $$G$$ and $$T$$, such that (in order of significance):

1. $$\operatorname{abs}\left(\cfrac{\sum_{x \in G}{\cfrac{\sum_{i=1}^{|x|}{x_i\cdot W_i}}{|x|}}}{|G|} - T\right)$$ is minimised (it is not always possible for the mean of means to equal $$T$$ exactly, but it should be as close as possible);
2. $$\operatorname{mean}\left(W\right)$$ is maximised — i.e. each weight is as large (therefore as 'precise', relative to the others) as possible — but;
3. $$\frac{\sigma\left(W\right)}{\operatorname{mean}\left(W\right)}$$ (where $$\sigma$$ is std. dev.) is minimised.

So far, I have no better ideas than just random guessing and checking, particularly in order to expand this to arbitrary sizes of $$G$$ and values for $$T$$. Although I'm not expecting a neat or closed-form solution by any means, I'm fairly sure that there has to exist some better approach to try to get optimised values.