简体   繁体   English

是否有一种算法可以对数据使用不同的约束进行平均?

[英]Is there an algorithm to average using different constraints on data?

Month Product产品 Product产品 Product产品 Current当前的
1 1 Y A一种 17 17
2 2 Y A一种 22 22
3 3 Y A一种 21 21
4 4 Y S Q 44 44
5 5 Y S Q 32 32
6 6 Y S Q 23 23
7 7 Y S B 56 56
8 8 Y S B 73 73
9 9 Y S B 12 12
10 10 Y C C 26 26
11 11 Y C C 45 45
12 12 Y C C 37 37

As seen in table above I have some data, elements of which form part of different products.如上表所示,我有一些数据,其中的元素构成了不同产品的一部分。 For example, 4th element in above table (highlighted row) ie, 44 is part of product Q , S and Y .例如,上表中的第 4 个元素(突出显示的行)即44是乘积QSY Each product has an average as below :每个产品的平均值如下:

average of Product A=(17+22+21)/3=20
average of Product Q=(44+32+23)/3=33
average of Product S=(44+32+23+56+73+12)/6=40
average of Product Y=(17+22+21+44+32+23+56+73+12+26+45+37)/12=34

Now I want to manipulate the data such that new averages are:现在我想操纵数据,使新的平均值是:

Average of product A=   25
Average of product Q =  35
Average of Product S=   32
Average of product Y=   39

and shape of months within a product should be maintained.应保持产品内的月份形状。

So result should be (example calculation):所以结果应该是(示例计算):

new Month 1= (new average - current average) +current month 1 value
new Month 1= (25 - 20) + 17 = 22

new Month 2= (new average - current average) +current month 2 value
new month 2 = (25 - 20 ) + 22 = 27

new Month 3= (new average - current average) +current month 3 value
new months 3 =(25-20)+ 21= 26

monthly shape difference example月形差示例

month month 1第 1 个月 month 2第 2 个月 month 3第 3 个月
current当前的 17 17 22 22 21 21
current monthly difference当前月差 22-17= 5 22-17= 5 21-22= -1 21-22= -1
New新的 22 22 27 27 26 26
New monthly difference新月差 27-22= 5 27-22= 5 26-27= -1 26-27= -1

It can see that month 2 - month1 for both current and new data is 5 and month 3- month 2 is -1 for both current and new data.可以看到当前数据和新数据的第month 2 - month15而当前数据和新数据的5 month 3- month 2-1 Hence, monthly shape for product A is maintained.因此,产品A月度形状保持不变。

Month Product产品 Product产品 Product产品 New新的
1 1 Y A一种 22 22
2 2 Y A一种 27 27
3 3 Y A一种 26 26
4 4 Y S Q 46 46
5 5 Y S Q 34 34
6 6 Y S Q 25 25
7 7 Y S B 38 38
8 8 Y S B 55 55
9 9 Y S B -6 -6
10 10 Y C C 57 57
11 11 Y C C 76 76
12 12 Y C C 68 68

Currently I have a VBA code that loops over each element multiple times to calculate difference between current and new averages and apply this difference to elements.目前我有一个 VBA 代码,它在每个元素上循环多次以计算当前平均值和新平均值之间的差异并将这种差异应用于元素。 Is there any matrix algorithm I can use that would make the same calculations instead of using iterative loops ?我可以使用任何矩阵算法来进行相同的计算而不是使用迭代循环吗? Algorithm code will be implemented in Matlab, C++ or Python or VBA.算法代码将在 Matlab、C++ 或 Python 或 VBA 中实现。 Currently I have been exploring MATLAB function lsqlin but cannot get it to do what I want.目前我一直在探索 MATLAB 函数lsqlin但无法让它做我想做的事。

Could you please help?能否请你帮忙?

The method :方法 :

  1. Ignore individual month value, view it as a sum (that relates to its shape).忽略单个月份的值,将其视为总和(与其形状有关)。 [ mth1-mth12 >> ad ] [ mth1-mth12 >> 广告 ]
  2. Write all needed relation.写出所有需要的关系。 [ avg_X <--> a,b,c,d ] [ avg_X <--> a,b,c,d ]
  3. Using the relation & new known value, solve for the sum [ a,b,c,d ]使用关系和新的已知值,求解总和 [ a,b,c,d ]

Implementation :执行 :

Step1 : line 1-4 |步骤 1:第 1-4 行 | Step2 : line 5-8步骤 2:第 5-8 行

Defining :定义:

Value & relation价值与关系 "Current" case “当前”案例 "new" case “新”案
a = sum(mth1,mth2,mth3) a = sum(mth1,mth2,mth3) 60 60 ? ?
b = sum(mth4,mth5,mth6) b = sum(mth4,mth5,mth6) 99 99 ? ?
c = sum(mth7,mth8,mth9) c = sum(mth7,mth8,mth9) 141 141 ? ?
d = sum(mth10,mth11,mth12) d = sum(mth10,mth11,mth12) 108 108 ? ?
avg_A = a/3 avg_A = a/3 20 20 25 25
avg_Q = b/3 avg_Q = b/3 33 33 35 35
avg_S = (b+c)/6 avg_S = (b+c)/6 40 40 32 32
avg_Y = (a+b+c+d)/12 avg_Y = (a+b+c+d)/12 34 34 39 39

In "Current" case : all variable above have defined values.在“当前”情况下:上面的所有变量都有定义的值。

In "new" case (Step3) :在“新”情况下(步骤 3):

 avg_A = a/3 = 25 >> a = 25*3 = 75
 avg_Q = b/3 = 35 >> b = 35*3 = 105
 avg_S = (b+c)/6 = ((105)+c)/6 = 32 >> c = 32*6-105 = 87
 avg_Y = (a+b+c+d)/12 = ((75)+(105)+(87)+d)/12 = 39 >> d = 201

Fill in mth1-12 with any value that can produce a,b,c,d value above.将 mth1-12 填入任何可以产生上述 a,b,c,d 值的值。

Done.完毕。

p/s: since OP only share 1 new "case", Step2 detail may varies. p/s:由于 OP 仅共享 1 个新“案例”,因此步骤 2 的详细信息可能会有所不同。 However, at least for this "shapes", this works.然而,至少对于这个“形状”,这是有效的。

My comment earlier actually in point to a statement "since the (observed relation is not really linear, a matrix cannot be built on the original table". Nevertheless, if you can figure out how to solve/put the Step2 relations in matrix form, step3 should be easier/automated. 我之前的评论实际上指向一个陈述“因为(观察到的关系不是真正的线性,矩阵不能建立在原始表上”。然而,如果你能弄清楚如何解决/将 Step2 关系放在矩阵形式中, step3 应该更容易/自动化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM