2

I am struggling to understand why the np.convolve method returns an N+M-1 set. I would appreciate your help.

Suppose I have two discrete probability distributions with values of [1,2] and [10,12] and probabilities of [.5,0.2] and [.5,0.4] respectively.

Using numpy's convolve function I get:

>>In[]: np.convolve([.5,0.2],[.5,0.4])
>>Out[]: array([[0.25, 0.3 , 0.08])

However I don't understand why the resulting probability distribution only has 3 datapoints. To my understanding the sum of my input variables can have the following values: [11,12,13,14] so I would expect 4 datapoints to reflect the probabilities of each of these occurrences.

What am I missing?

KPr
  • 73
  • 4
  • Does [this](https://stackoverflow.com/questions/20036663/understanding-numpys-convolve) answer your question ? – Jay Patel Feb 16 '21 at 03:38
  • I guess I understand why it gives 3 points now if we strictly look at the mathematical definition of convolution. But I still cannot relate it to my example where I am using convolution to calculate a sum of probability distributions, which should be doable. In my example I cannot relate the resulting probabilities to the events as the events are 4 and the probabilities are 3. Is convolution not appropriate for this example? Based on its theoretical definition it should be... – KPr Feb 16 '21 at 16:24

1 Answers1

2

I have managed to find the answer to my own question after understanding convolution a bit better. Posting it here for anyone wondering:

Effectively, the convolution of the two "signals" or probability functions in my example above is not correctly done as it is nowhere reflected that the events [1,2] of the first distribution and [10,12] of the second do not coincide.

Simply taking np.convolve([.5,0.2],[.5,0.4]) assumes the probabilities corresponding to the same events (e.g. [1,2] [1,2]).

Correct approach would be to bring the two series into alignment under a common X axis as in x \in [1,12] as below:

>>In[]: vector1 = [.5,0.2, 0,0,0,0,0,0,0,0,0,0]
>>In[]: vector2 = [0,0,0,0,0,0,0,0,0,.5, 0,0.4]
>>In[]: np.convolve(vector1, vector2)
>>Out[]: array([0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.25, 0.1 ,
                0.2 , 0.08, 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  , 0.  ,
                0.  ])

which gives the correct values for 11,12,13,14

KPr
  • 73
  • 4