0

numpy.convolve(a, v, mode='full') only admits two inputs:

  • a(N,) array_like First one-dimensional input array.
  • v(M,) array_like Second one-dimensional input array.

How can I instead calculate the convolution of more than 2 probability distributions in Python?

Example

The following code generates 3 random variables. I would like to form a convolution of all of them and also extract the weights used to form that convolution:

import numpy as np
from numpy.random import randn

n=100
x=randn(n)
y=randn(n)+0.2
z=randn(n)*0.3
print(np.convolve(x,y))

However, numpy.convolve only accepts two inputs, and even in the 2-input case, how can I even extract the weights used for forming the convolution?

develarist
  • 1,224
  • 1
  • 13
  • 34
  • Can you specify what you mean by more than 2 probability distributions? I am not familiar with it. – yonatansc97 Dec 04 '20 at 11:38
  • if we normalized a real-valued dataset by its sum, we have frequencies of each outcome/value in the dataset called probabilities, each being non-negative and sum to 1. A probability distribution (regardless pdf, pmf or cdf) is a dataset filled with these probabilities corresponding to the original dataset's samples. It is likely we are resorting to the discrete version of the distribution rather than a continuous, parametric distribution, but all we really need to recognize is that the inputs for the `numpy.convolve` function is likely the pmf of the real data it was derived from – develarist Dec 04 '20 at 11:49
  • Convolution is associative, just use `functools.reduce(np.convolve, *inputs)` – Daniel F Dec 04 '20 at 12:28
  • how can I extract the weights applied to each component distribution in the convolution afterwards? – develarist Dec 04 '20 at 12:33
  • You'll need to make up a [mcve] of what you want, right now your inputs and expected outputs are not clear. – Daniel F Dec 04 '20 at 12:40
  • hope the edit helps – develarist Dec 04 '20 at 12:45

1 Answers1

2

Just use functools.reduce to chain the convolutions together (since convolution is associative):

from functools import reduce

def multi_conv(*arrs, conv = lambda x, y: np.convolve(x, y, mode='full')):
    return reduce(conv, arrs)

multi_conv(x, y, z)

Now with mode='full' you'll probably need to trim it down to be 100 values again, or whatever else you want to do with it.

Daniel F
  • 13,620
  • 2
  • 29
  • 55
  • thanks, how can the weights applied to the individual components be extracted that were used to form the convolution? – develarist Dec 04 '20 at 13:11
  • I know what those words mean, but I don't understand them in that order. If you have a problem specification with a certain expected output, please include it in your question. – Daniel F Dec 04 '20 at 13:21
  • Given 3 variables, the convolution assigns 3 different weights to each variable in order to form the overall convolution of all 3. Currently there is no output from the function or the code regarding the weight vector containing the 3 different weights. How can I make the `convolve` function output the weight vector? The weight vector is my expected output but it's absent – develarist Dec 04 '20 at 13:23
  • I don't know how else to explain this. I don't understand what you want based on the jargon you are using. We are not statisticians here, we can only help with implementation based on code. There is no code in your question to define what you mean by "weight vector," and thus unless you get lucky and find someone who knows what you mean in the context you meant it, it's unlikely you will get a useful answer. – Daniel F Dec 04 '20 at 13:29
  • 3 random variables: x1, x2, x3. to each the convolution applies a fractional weight: w1, w2, w3. The convolution therefore is w1*x1 + w2*x2 + w3*x3. the weights, w1, w2, w3, how can i extract the weights from `convolve`? – develarist Dec 04 '20 at 13:31
  • I think you're fundamentally misunderstanding what `np.convolve` does. It doesn't convolve by a distribution, it convolves the two signals against each other using window functions. – Daniel F Dec 04 '20 at 13:35
  • which function in python should I use if I want to convolve two distributions, not signals? Be aware that, in my example, i did not send probability distributions into the function as inputs. I sent the raw data in – develarist Dec 04 '20 at 13:42
  • It depends what you mean by "convolve two distributions." It would be helpful if you explained that (with notional input and matching expected output) in your question. – Daniel F Dec 04 '20 at 13:45
  • I think the jargon I should really be using is "characteristic function" which is the probability theory analogue of convolutions. sorry about that https://en.wikipedia.org/wiki/Characteristic_function_%28probability_theory%29 – develarist Dec 04 '20 at 13:56
  • By the way your code gives the following error: `TypeError: reduce expected at most 3 arguments, got 4` – develarist Dec 04 '20 at 14:48
  • thanks, looking at the solution of the last line though, it generates an array of length 298, even though the individual variables were only 100 long. what am I looking at really in this array – develarist Dec 04 '20 at 16:30