0

I have to build a2D array in python starting from a 1D array, where I use each element to start a new "orthogonal" 1D array. Suppose that I have an array that for each x gives me the probability given an underlying gaussian distribution. To better explain, I have a 1D array of x points, and for each x I want to generate a gaussian distribution N(x|mu,x1), where mu = x0-y[j]. Here y[j] is clearly the jth element along another 1D array. Then, I have to wait the gaussian I get by the probability of having that y[j], i.e. another gaussian. I did the following and it works, but it is too much time consuming, and I was wondering if there is an faster and more elegant way to implement this. Thank you.

import numpy
import matplotlib.pyplot as plt

parameters = [1.5, 0.17, 0.5,0.2]
x0, x1, y0, y1 = parameters
x = numpy.linspace(0,numpy.pi,600)
y = numpy.linspace(0, numpy.pi/2,500)
ygaussian = (1/(numpy.sqrt(2*numpy.pi)*y1))*numpy.exp(-0.5*((y-y0)/y1)**2)

W, V = numpy.meshgrid(x, y)
G = numpy.zeros((len(x),len(y)))
for i in range(len(x)):
    for j in range(len(y)):
        mu = x0 - y[j]
        G[i,j] = (1/(numpy.sqrt(2*numpy.pi)*x1))*numpy.exp(-0.5*((x[i]-mu)/x1)**2)*ygaussian[j]
W, V = numpy.meshgrid(x,y)
plt.contourf(W,V,G.T)
plt.show()

enter image description here

3
  • Please put a more descriptive title. See How to Ask for tips.
    – wjandrea
    Commented Nov 13, 2023 at 17:51
  • "wait the gaussian" should be "weight the gaussian", right? (They're pronounced the same.)
    – wjandrea
    Commented Nov 13, 2023 at 17:52
  • Yes, of course. Sorry for that
    – Alberto
    Commented Nov 13, 2023 at 19:43

1 Answer 1

1

You don't really "need to wait", as the vector x0 - y can be precomputed, and, as such, can be vectorized. Do this:

prefactor = 1.0 / (2.0 * numpy.pi * x1 * y1)                                                                          
def gaussian(x, y):                                                                                                   
    return prefactor * numpy.exp(-0.5 * ((                                                                            
        (y - y0) / y1)**2.0 + ((x + y - x0) / x1)**2.0))                                                              
                                                                                                                      
                                                                                                                      
X, Y = numpy.meshgrid(x, y)                                                                                           
G = gaussian(X, Y)                                                                                                
                                                                                                                      
plt.contourf(X, Y, G)                                                                                             
plt.show() 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.