Reputation: 913
I have an array of doubles representing probabilities of certain events happening, [25,25,25,10,15] for event A,B..E. The numbers add up to 100.
Through my analysis I want to be able to cross off the possibility of a certain event happening or not.
So, If I find that event E is impossible, then I set that index to 0. How do I re-normalize the array so that the total adds up to 100, and the relative probability of each event is maintained?
I will use C# or Java.
Upvotes: 0
Views: 1214
Reputation: 533530
A more general solution is to normalise an array to give it a specific total.
public static void setTotal(double total, double[] values) {
double sum = 0;
for (double value : values) sum += value;
double factor = total / sum;
for (int i = 0; i < values.length; i++)
values[i] *= factor;
}
This allows you to alter any value and ensure the total is whatever you want (provided the sum is not zero)
Upvotes: 0
Reputation: 4380
I'm assuming the probabilities will stay the same (i.e the first double's new probably should be 25/85 in your example). Then what you need to do is save the double you've removed, subtract 100 by it and divide each double by it, and multiply by 100.
Java code:
double[] doubleArray = new double[] { 25, 25 ,25, 10, 15 }; //or any other array
double temp = 100-doubleArray[4]; //or any other specific value in the array
doubleArray[4] = 0;
for (int i; i<doubleArray.length; i++) {
doubleArray[i] = doubleArray[i]/temp*100;
}
Upvotes: 0
Reputation: 726639
You can re-distribute the probability of the removed event in many ways, for example
p/sum(p)
, where sum(p)
is the sum of remaining probabilities.The "correct" answer depends on the specifics of your problem.
Upvotes: 2
Reputation: 18286
sum = 100 - P(E)
for each event e:
if e is E:
P(E) = 0
else:
P(e) = P(e)*100/sum
Upvotes: 0