tags:

views:

116

answers:

6

I want to create an array that sends a pixel in a specific direction.

It has four options: Forward, Backward, Left, Right

Each direction has an associated score that is its probability of sending the pixel in a specific direction.

Example:

Direction[Forward] = 20;
Direction[Backward] = 12;
Direction[Left] = -5;
Direction[Right] = 2;

How can I make the number for each key in the direction array equal the probability of moving the pixel? I want the negative number to play into the probability albeit a small chance.

Currently I copy each direction key into a new array the number of times as it's probability score, then generate a random number against the new array, but that doesn't work on negative numbers, so I change them to +1 in advance. Obviously this doesn't scale on negative numbers.

How can I create a probability for each array key regardless if it's a positive or negative number?

+3  A: 

In the mathematic sense there is no negative probability, but you can use a function which maps every number to a positive number like the exponential function:

double totalProbability = 0;
probabilty = new array
foreach (key in Direction)
{
  probability[key] = exp(Direction[key];
  totalProbability  += probability[key];
}
//ensure the sum of all probabilities is 1
foreach (key in Direction)
{
  probability[key] /= totalProbability;
}
Philipp
+1 - Exp turned it positive. This might be a workable solution. Will test further tomorrow.
JMC
"In the mathematic sense there is no negative probability" - not true; see below. Think of probability like Fourier transforms and you'll see that they can be negative in magnitude.
duffymo
I was thinking about probability in the sense of the Kolmogorov axioms (first axiom, see http://en.wikipedia.org/wiki/Probability_axioms). But I should have known that in mathematics things are always more complicated and general than an engineer like me assumes :-)
Philipp
+1  A: 

I don't see the point of a negative score here. If you want these numbers to correspond to probabilities, then negative has no meaning. If you want your pixel to go left very seldomly, you only need to ensure that the Left score is much smaller than the others. But as soon as you hit 0 it will never go left, whereas even smaller score (i.e. negative) has no real meaning. I would therefore simply replace negative scores with 0, unless you can provide a more meaningful interpretation of these scores.

As for making the scores probabilities, this can easily be done once all scores are non-negative:

  1. let S be the sum for all the scores.
  2. divide each score by S. The result is the probability.

In your example (with -5 replaced by 0) you have S = 20+12+2 = 34, which gives a probability for forward movement of 20/34 = 58.8%, backward with 12/34=35.3% and right with 2/34 = 5.9%. (As you see I made same roundings, so you don't get an exact sum of 1 for the probabilities, which will happen similarly if you compute with double or anything like that.)

Frank
Reason for negative numbers - Each direction starts at zero then is modified by +1 or -1 a random number of times based on criteria. Unfortunately that makes certain directions end up negative. So I'm trying to create probability based on the results as if negative and positive are arbitrary.
JMC
No, you've already got negatives covered with four directions. They're all positive. Make your range go from 0 (no motion) to 1 (certain motion) and you'll be fine.
duffymo
+1  A: 

I don't know what the answer to your question is, but here's an alternative view for all those who are saying that negative probabilities make no sense and aren't logical. Negative probabilities have been taken seriously in quantum physics for quite a while.

Maybe the real problem is the random number that ranges from -1 to +1. The random number seems to be setting the probability that a pixel moves in a given direction. If you changed that range to [0, 1] perhaps this problem goes away and the whole algorithm makes more sense.

You have four directions: right (positive x), forward (positive z?), left (negative x), and backward (negative z?). You've got directionality built-in already; there's no need for a sign on the negative number. Make the range [0, 1] for each direction and the problem might be moot.

duffymo
@duffymo - The modifiers -1 to +1 aren't randomly generated. The over-simplified explanation in my original question probably left that open for interpretation. When I change the numbers to floating point this might work, but I still run into the problem that a direction with three negative modifiers no positive will equal a direction with no positive or negative. I've found a workable solution though, thanks for your input.
JMC
A: 

Even after reading all this, I'm a bit perplexed as to as to why you're ending up with negative numbers, but every language has an absolute function that will always return a positive version of any number.

Jon Rimmer
A: 
new Random().Next(3) - 1; // returns random number between -1 and +1
codymanix
A: 

Find the smallest number and substract it (and an extra 1) from all probabilities (in this case substract -6 and get [26, 18, 1, 8]). This will, however, not keep 'double probability' of one event comparing to another... but however, using negative probabilities isn't very logical anyway...

eumiro