My requirement is, every time I need to calculate the average value for the provided cost. Currently I am using the following formula to calculate it:
Average cost = (previous_average_cost + cost_value)/2
After adding 5 to 6 cost_value, I need to remove one of the cost_value from the average cost. Currently I thinking how to remove the added cost_value from the average cost. Please share your thoughts.
For example:
Step:1
previous_average_cost = 0
cost_value = 10
average cost = (0+10)/2 = 5
Step:2
previous_average_cost = 5
cost_value = 15
average cost = (5+15)/2 = 10
Step:3
previous_average_cost = 10
cost_value = 20
average cost = (10+20)/2 = 15
Finally now the average cost is 15: I need to remove the previously added cost_value = 15 from the current average cost 15. Please share your thoughts
This is incorrect:
Average cost = (previous_average_cost + cost_value)/2
Let's review the definition of average:
x(ave) = sum(x)/N
where N = # of points.
If you add one more point:
x(new_ave) = (N*x(ave) + y)/(N+1)
You have to hang onto the current average and number of points to be able to update.
If you remove a point you've added:
x(new_ave) = (N*x(ave) - y)/(N-1)
Related
If I have two values with a calculated ratio for example:
Value 1 = 5000
Value 2 = 100
Calculated Ratio = 50:1
How do I distribute a value of 500 between value 1 and value 2 so I can get to a 17:8 ratio or as close as possible to 17:8 ratio without decreasing any of the values.
I tried adding all the values and then splitting them into 17:8 ratio but this will in some cases decrease one value to get to another.
Incorrect example as value one has decreased from its original value of 5000:
Value 1 = 3808
Value 2 = 1792
Calculated Ratio = 17:8
You have two equations and two unknowns. The two unknowns are the adjustment values a and b such as the ratio below is known (17/8)
aspect = (value1+a)/(value2+b)
but the combined value of the adjustments has to be a fixed amount (500)
sum = a + b
Soution 1
The solution if the aspect ratio is float type value 17/8=2.125, then the solution is
a = (aspect*(value2+sum) - value1)/(aspect+1)
b = (value1 - aspect*value2+sum)/(aspect+1)
In your case I get a = -1192 and b = 1692 for
value1 + a = 3808
value2 + b = 1792
The ratio 3808/1792 = 17/8 and the sum (1692) + (-1192) = 500
Solution 2
The solution if the aspect ratio is a rational number aspect = num/den is:
a = (num*(value2+sum) - den*value1)/(den + num)
b = (den*(value1+sum) - num*value2)/(den + num)
and again the sample calculation is (num=17, den=8)
a = (17*(100+500) - 8*5000)/(8 + 17) = -1192
b = (8*(5000+500) - 17*100)/(8 + 17) = +1692
Adjustments
If you constrain a>=0 and a<=sum as well as b>=0 and b<=sum then you would not reach the aspect ratio.
You can do this will the following code adjusting a and b
if (a<0)
{
a = 0;
b = sum;
}
else if(b<0)
{
a = sum;
b = 0;
}
Graph
Graphically this problem is a follows:
The blue line is the combination of Value 1 and Value 2 that have the aspect ratio desired.
The pink dot is the starting value (5000,100).
The slanted lines are the adjusted Value 1 and Value 2 for a given sum amount to adjust by. I have included lines for 500, 1000, 2000, and 4000.
Where the slanted lines intersect the blue line is the ideal solution. The solution
The red dot is where the above solution(s) lead you before adjustments. After adjusting for non-negative a and b you will end up at the black dot.
In google sheets, you need some extra columns to implement the above
This is not specific to sheets and is a basic math cross multiplication problem
So, 17 is to 8 as 500 is to x
17/8 = 500/x
Cross multiplying give us
8 * 500 = 4,000
17 * x = 17x
solving for x
x = 4,000/17
x = 235.29
This was a really fun problem thanks for sharing it. Solution
Spoiler alert! The integers are 5302 and 298 with a final ratio of 17.79 which can be found in row 303.
Edit 1:
I misunderstood the question. A 17:8 ratio can be simplified to 2.125:1. The spreadsheet lists all of the possible combinations, where the smallest ratio is 8.35:1. Thus it doesn't seem there is a solution close to a 17:8 ratio.
i have data in the following form (2 examples):
p1 <- structure(c(1.38172177074188, 1.18601365390563, 1.25131938561825,
1.07175353794277, 0.887770295772917, 0.806599968169486, 0.843543355495394,
0.889051695167723, 0.764131945540256, 0.699309441111923, 0.945165791967098,
1.31310409471336), .Dim = 12L)
p2 <- structure(c(1.24801075135611, 1.06280347993594, 1.21410288703334,
1.36797720634294, 1.07291218307332, 0.936924063490867, 0.819723966406961,
0.854960740335283, 0.718565087630857, 0.649827141012991, 0.785853771875901,
1.04368795443605), .Dim = 12L)
These are standardized monthly means of hydrological time series; so-called Pardé regimes that give some indication about annual seasonality. To do further analysis, i need to derive the 3 highest and lowest months from these Pardé series. Because seasonality can be bimodal, i need to identify the 3 highest/lowest consecutive data points (which are most often not the three absolute highest/lowest data points, see examples) to derive the timing of the most wet and dry periods. Up to now i failed because of the circular character of the time series, which poses a special challenge.
Any suggestions?
You could use filter. It sums consecutive values and can deal with circular time series.
f1 <- stats::filter(p1, c(1, 1, 1), circular = TRUE, sides = 1)
#Time Series:
# Start = 1
#End = 12
#Frequency = 1
#[1] 3.639992 3.880840 3.819055 3.509087 3.210843 2.766124 2.537914 2.539195 2.496727 2.352493 2.408607 2.957579
((which.max(f1) - (3:1)) %% 12) + 1
#[1] 12 1 2
I have such a problem on hand. Imagine there are 5 sliders with a range of values 0 to 100. In the beginning, they are all set to 50, so the average score is 50.
Goal 1. Maintain average at 50
Say, if I move the first slider to 70, in order to maintain the average at 50, I decrease the value of the other 4 sliders to 45, i.e. 50 - (20/4) = 45.
Goal 2. Maintain ratios between individual values
The above example was easy, because all 4 affected values where equal. However, if I decide to move the 5th slider to 50, I want all the other sliders to adjust so that the ratios between individual values (e.g. slider 1 / slider 2 is 70 / 45 -> 1,5555...) remain the same.
Here's the method I'm considering.
Step 1. Find the smallest value in the array of affected values (slider 1-4).
Step 2. Calculate ratios of each slider with the minimum-value-slider.
Step 3. This gives me a formula avg(ratio1*minV + ratio2*minV + ratio3*minV + *minV + newManualV) = 50
Step 4. Calculate minV and the remaining values using ratios.
So, in my example, it would be something like this:
newManualV = 50 (5th slider)
minV = 45 (any of the 2nd-4th sliders; let's say it's the 2nd)
ratio1 = 1.55556 (1st and minV)
ratio2 = 1 (3rd and minV)
ratio3 = 1 (3rd and minV)
(1.55556*minV + 1*minV + 1*minV + minV + 50) / 5 = 50
4.55556minV = 200
minV = 43.9
New (rounded) slider values are:
68 (43.9 * 1.55556)
44
44
44
50
Question. Is there a better way of doing this?
What is better? I think there is an easier way, as you do not need to explicitly calculate the ratio's. Just distribute the difference of the altered slider equally.
Denote the slide values with s1, ..., s5.
Suppose you change s1 with and amount d. Then, calculate s, the sum of the other sliders: s = s2+s3+s4+s5. Now, s2 -= (s2/s)*d.
The sum of s1 to s5 does not change (and so the average), and all other sliders are changed in proportion.
Five sliders equals 4 ratios to maintain. For example x_1/x_2. This is done only by multiplying all the slider by the same factor.
As soon as you set one value (for example slider #5) then all the others have to scale accordingly to maintain the ratios.
old_x_5 = x_5
x_5 = (new value)
scale = x_5/old_x_5
x_4 = x_4*scale
x_3 = x_3*scale
x_2 = x_2*scale
x_1 = x_1*scale
Now you have to re-scale everything in order to meet the average goal
average = (x_1+x_2+x_3+x_4+x_5)/5
if average>0
scale = 50/average
x_5 = x_5*scale
x_4 = x_4*scale
x_3 = x_3*scale
x_2 = x_2*scale
x_1 = x_1*scale
else
(now what?)
This changes the new x_5 to a value needed for the average goal instead of the user input. Also what happens if any of the sliders need to be over 100 to maintain the average.
A product is rated according to two features. The ratings for each feature are averaged, and the results delivered to us in a web service. The data might look something like:
Product_One:{
"Feature_A": {
"TotalReviewCount": 14,
"AverageOverallRating": 4.9286,
},
"Feature_B": {
"TotalReviewCount": 42,
"AverageOverallRating": 4.3571,
},
}
My working for calculating (to one decimal place) the average overall rating for the Product is:
4.9286 + 4.3571 = 9.2857
9.2857 / 2 = 4.64285
round(4.64285) = 4.6
A colleague has presented a different working, resulting in a different number:
(14 * 4.9286) + (42 * 4.3571) = 251.9986
251.9986 / (42 + 14) = 4.499975
round(4.499975) = 4.5
Whose is... best? Is one wrong?
Your colleague calculated weighted average per review, and you calculated average per feature (not taking into account number of reviews).
In general weighted average is more reliable (compare with Center of mass)
I'm working on user-profile where each term exists in the user-profile have wight and the weight formulated from set of factors such as (duration, total number of visit ...etc) , I need to normalize the result of their summation to be number between 0 and 1, I performed this equation:
(x+y+z+......)/100
Where x, y and z are factors. I have suggested this equation to my self (I'm sorry I'm not very good in math :( ), but unfortunately it returns some value more than 1 , so is there any way that can be applied to limit the result of the summation between 0 and 1?
Many thanks in advance.
Ok, generally, to normalize, this is what you do:
Find the absolute minimum value, and subtract this from your number. (This may be 0, in which case you can skip this step.)
Find the absolute maximum value. Your total range after step 1 will be from 0..(maximum - minimum). Divide your number by this value, and everything will be in the range of 0..1.
To spin it back, you do the opposite: take your normalized number, multiply by the range (i.e. max - min), then add back the min.
The reason you're having a problem is because x + y + z + ... has a range that is not 100.
Example
If x has a range of 0-10, y has a range of 15-25 and z has a range of 10-25, and your specific values are x = 8, y = 17, z = 12:
x + y + z = 8 + 17 + 12 = 37
min = 0 + 15 + 10 = 25
max = 10 + 25 + 25 = 60
so your normalized value is calculated by doing:
(37 - 25) / (60 - 25) = (12 / 35) = 0.342857 (approximately).
To go back from normalized to a composite number, do the opposite:
0.342857 * 35 = 11.999995 = 12 once rounded.
12 + 25 = 37
If your variables are unbounded, nobody can reach the normalized value 1, because if someone achieved 1, another person with larger factors would exceed 1.
This said, you can transform every factor with a function that maps [0 +inf[ to [0 1[, like X/(X+a) or 1-2^(-X/a), where a is some scaling constant (chosen by you). You will apply this transform to the individual factors and average them, or just apply it to the global sum.