How many X cells to add to reach Y% - excel

I'm working on this: https://tempfile.me/download/nYhdQHD65GxRzk/ .
And I need to count how many 1 cells should be added to column A to reach a percentage of 1s = 85%.
This is just an example, I can't add cells with 1 and see how many of them I need since it should be automated on a big sample of data.

Expressed as:
.85(count + x) = sum + x
this renders down to x = ( (85 x count) - (100 x sum) ) / (100 - 85) or,
=(85*COUNT(A:A)-100*SUM(A:A))/(100-85)
However, this does not result in an integer, so to ensure 85% is reached:
=ROUNDUP((85*COUNT(A:A)-100*SUM(A:A))/(100-85),0)
The result (234) when added as 1s increases the TRUE total to 284 and the count of all entries to 334, where 284/334 is 85.03%.

It is a lot easier, if you don't look at the percentage which is TRUE or FALSE, but at the count of TRUE and FALSE.
In your example, you have 50 1's and 50 0's.
You want to see how many 1's you have to add in order to get a percentage of 1's of 85%.
In numbers this would look like this:
(x+dx)/(x+dx+y) = 0.85
where x is the number of 1's, y the number of 0's and dx the increase of 1's needed, to get to 85%.
You are looking for dx. So just solve for dx and you get:
dx = (0.85*y-0.15*x)/0.15
Which yields in your example an dx = 234. So you need to add another 234 1's in order to get a (minimum) 85% of 1's.
Hope this is what you wanted. It has however nothing to do with excel.

Related

Multiple MAX in one Excel function

I am trying to find the formula on excel that will solve my problem.
I need to calculate X, which is equivalent to a percentage YĆ·2 (and X has maximum and minimum values which depend on Y)
We have that if Y is less than 500, X represents 25% of Y. In this case, X must take a value between 5 and 125.
If Y is between 500 and 1000, X represents 20% of Y. In this case, X must take a value between 125 and 200.
If Y is greater than 1000, X represents 15% of Y. In this case, X must take a value between 200 and 400.
In order to find the value of X based on the value of Y, I first used this formula (omitting the max and min values)
Y=A2
=IF(A2<500; A2*0,125; IF(A2<=1000; A2*0,1; IF(A2>1000; A2*0,075)))
Now I tried to incorporate in my formula the min and max and that's where I'm stuck
For the first part of the problem (Y<500), I applied this formula:
=MAX((IF(A2<500;A2*0,125));5)
Ideally I would have realized this formula:
=MAX((IF(A2<500; A2*0,125));5) ; MAX((IF(A2<=1000; A2*0,1));125) ; MAX((IF(A2>1000; A2*0,075));200)
But since I can't do it (Excel won't let me), would you have any alternatives to offer me?
Do you want to determine the percentage based on the value but set a minimum?
=IF(A2<500,MAX(A2*0.125,5),IF(A2<=1000,MAX(A2*0.1,125),MAX(A2*0.075,200)))

Excel Sumproduct counts rows incorrectly with multiple column criteria

I am trying to calculate Precision and Recall for the output of a model which makes 3 predictions whether a person's name is John or not.
The ground truth for each entry/row is column A. The predictions are stored in columns O,Q and S. The model only needs 2 out of 3 predictions to be > 50% each to be considered correct.
Therefore a True Positive is when >=2 of O,Q,S are > 50%.
Similarly, a False Negative is when < 2 of O,Q,S are > 50%.
Precision = TP / (TP + FP)
Recall = TP / (TP + FN)
I can calculate precision ok because the final logic operator is >= and therefore the values cannot be 0 to be counted. But for this, the final SUM in the denominator is problematic, counting all rows.
This is the part that works, the Precision:
SUM(IF((A2:A300="John")*((O2:O300>=.5)+(Q2:Q300>=.5)+(S2:S300>=.5))>=2,1)) / (SUM(IF((A2:A300="John")*((O2:O300>=.5)+(Q2:Q300>=.5)+(S2:S300>=.5))>=2,1)) + (SUM(IF((A2:A300="Not John")*((O2:O300>=.5)+(Q2:Q300>=.5)+(S2:S300>=.5))>=2,1)))
And this is what I'm trying but doesn't work. The last < operator screws up the denominator and I can't figure out how to fix:
SUM(IF((A2:A300="John")*((O2:O300>=.5)+(Q2:Q300>=.5)+(S2:S300>=.5))>=2,1)) / (SUM(IF((A2:A300="John")*((O2:O300>=.5)+(Q2:Q300>=.5)+(S2:S300>=.5))>=2,1)) + (SUM(IF((A2:A300="John")*((O2:O300>=.5)+(Q2:Q300>=.5)+(S2:S300>=.5))<2,1)))
If there are 3 rows where A = "John" with only 2 of those rows have 2 of O,Q,S > 50%
And if there are 3 rows where A = "Not John" with all 3 rows have O,Q,S > 50%
Then,
Precision = 2 / (2 + 3) = 2/5
Recall = 2 / (2 + 1) = 2/3
You can fix the recall by putting an extra set of brackets round the <2 comparison:
=SUM(IF((A2:A7="John")*((O2:O7>=0.5)+(Q2:Q7>=0.5)+(S2:S7>=0.5))>=2,1))/(SUM(IF((A2:A7="John")*((O2:O7>=0.5)+(Q2:Q7>=0.5)+(S2:S7>=0.5))>=2,1))+(SUM(IF((A2:A7="John")*(((O2:O7>=0.5)+(Q2:Q7>=0.5)+(S2:S7>=0.5))<2),1))))
so you avoid multiplying by zero for the 'Not John' rows before doing the comparison and get the correct denominator.

Python - Negative numbers not adding, but positive numbers do

I am supposed to add up the rows and the grand total of all the numbers. I can add the grand total well, but I am unable to add the row that has negative numbers only. The following code adds up the positive numbers but do not add up the negative numbers correctly.
grandTotal = 0
sumRow = 0
for x in range(len(numbers)):
sumRow = (sumRow + x)
print(sumRow)
for x in range(len(numbers)):
for y in range(len(numbers[x])):
grandTotal = grandTotal + int(numbers[x][y])
print(grandTotal)
When the user input is:
1,1,-2 -1,-2,-3 1,1,1
My output is: 0
1
3
-3
instead of: 0
-6
3
-3
I know it has something to do with the first for loop, but I can't figure it out. When I try this:
grandTotal = 0
sumRow = 0
for x in range(len(numbers)):
sumRow = (sumRow + (numbers[x]))
print(sumRow)
for x in range(len(numbers)):
for y in range(len(numbers[x])):
grandTotal = grandTotal + int(numbers[x][y])
print(grandTotal)
I get the error message:
File "list.py", line 14, in
sumRow = (sumRow + (numbers[x]))
TypeError: unsupported operand type(s) for +: 'int' and 'list'
Why doesn't my code add up the negative numbers? Any help is greatly appreciated!
Where you say
sumRow = (sumRow + (numbers[x]))
To add integers you say 1+1, not (1+(1)) this would be adding to lists so you could change that.
From my understanding numbers is an array as well, so saying
numbers[x]
Will give you many numbers. What you want is the total for every row, and the total of all rows. Heres a program that does this. I am assuming that your program automatically gets numbers from the user input.
grandTotal = 0
for row in numbers:
#for each row we find total amount
rowTotl = 0
for value in row:
#for each column/ value we add tot the row total
rowTotl += value
print(rowTotl)
#add this row's value to the grandTotal before we move on
grandTotal += rowTotl
#After all adding we print grand total
print(grandTotal)
The reason your program doesn't add negative numbers, is really because the row totals are not adding numbers at all. They are just adding the indexes, rather than the value, so they don't work for positive number either. The grand total works because you are adding all the values properly, rather than adding the indexes. FYI,
for index in range(len(numbers)) :
does not give you the values, but rather : 0,1,2,3,4,5,6...(indexes) till the end of the range, to get the value numbers you would do
for value in numbers:

Calculation of average based on a weighted score

I'm trying to calculate an average score based on a list of parameter scores (between 0 and 5). The trick is that I want to be able to weight each parameter.
Eg:
Parameter A Parameter B Parameter C
Weight 100% 70% 0%
Score 4 5 0
In the above example, the average score should be 3,75 as parameter c is left out.
I've tried with this formula: =IF.ERROR(SUM((A3*A5);(B3*B5);(C3*C5))/COUNTA(A3:C3);""). The formula seems to work if none of the parameters weight is equal to 0. How can I adjust the formula, so it excludes a score if weight is equal to zero?
I think it should be rather easy, I just can't get it to work.
Check this :
=SUMPRODUCT( A2:A4, B2:B4 ) / SUM( B2:B4 )
Source : https://exceljet.net/formula/weighted-average
With COUNTA you are counting the non empty cells, while you should count the non zero cells. So, assuming that the weights are in A3:C3 and the scores in A5:C5:
=IFERROR(SUMPRODUCT(A3:C3;A5*C5)/COUNTIF(A3:C3;">0");"Error: all the weigths are 0")
It would be like this:
(1*4 + 0.7*5) / 2 = 3.75
In other world the formula is:
((WeightA/100 * scoreA) + (WeightB/100 * scoreB) + (WeightC/100 * scoreC)) / 3
=SUMPRODUCT(A1:A3;B1:B3) / COUNTIF(B1:B3;"<>0") / 100
Something like this would work

Excel - Multiply Until Total Reached

I want to multiply x*y until x>=20, then multiply z that value and have the results displayed as two values, the multiple and multiple*z
The question behind the formula is, how many boxes of x capacity do I need to have a total capacity of 20 liters and how much does that cost.
x = volume of bottle
y = number of bottles in a box
z = price per box
This could be done very easily by hand, but I've been playing (with little effect) in excel for a while and would like a solution.
I hope that makes sense
I rather think what you would like is the formula provided by #Jeeped but for:
I want to multiply x*y until x>=20, then multiply z that value and have the results displayed as two values, the multiple and multiple*z
label two arrays from 1 to 20 for columns and rows as shown, populate V1 with the price per box and in B2:
=IF(AND($A2*B$1>20,A2>=20),"",$A2*B$1)
and in X2:
=IF(B2="","",$V$1*B2)
with both formulae copied across 19 columns and those two sets of 20 formulae then copied down 19 rows. The result should be similar to:

Resources