0

I want to generate random sample (vector X):

X = [x1, x2, x3, ..., x_d]

where X follows a custom multivariate Probability Density Function.

I am using Python - scipy and I overvide the PDF function to follow a combination of multivariate normal distributions.

For example:

import plotly.graph_objects as go
import numpy as np
from scipy.stats._multivariate import multivariate_normal_gen, multivariate_normal, _squeeze_output


class multivar_rv(multivariate_normal_gen):

    def pdf(self, x, mean=None, cov=1, allow_singular=True):
        mean = [
            [0, 1],
            [2, 5]
        ]
        cov = [
            [
                [1, 0],
                [0, 1]
            ],
            [
                [1.5, 0],
                [0, 1.5]
            ]
        ]
        weight = [.5, .5]
        return sum(weight[i] * multivariate_normal(mean[i], cov[i]).pdf(x) for i in range(len(mean)))

The following image shows the plot of the above code. enter image description here

Now I want to generate samples given the above distribution, however if I use rvs() (from scipy.stats._multivariate.multivariate_normal_gen) the function is not overrided and the results will be generated from the default normal distribution:

P.rvs()

What do I have to do in order to change this and override this function so that the generated samples follow my PDF?

Thank you!

  • does this help: https://stats.stackexchange.com/questions/70855/generating-random-variables-from-a-mixture-of-normal-distributions – piterbarg Jan 25 '22 at 22:48
  • or this: https://stackoverflow.com/questions/49106806/how-to-do-a-simple-gaussian-mixture-sampling-and-pdf-plotting-with-numpy-scipy – piterbarg Jan 25 '22 at 22:49
  • dear @piterbarg thank you for your comment in fact it helped generate some samples and the error from the actual PDF was not that big, so I used that – vasilis siatras Dec 27 '22 at 15:36

1 Answers1

0

That is because the the rvs method that your class inherits (i.e. that of multivariate_normal_gen) uses an instance of multivariate_normal_gen directly that is declared outside of the class definition (see lines 657 and 682 in the source) to generate samples. You have to write your own rvs method explicitly.

aafsar
  • 1
  • 1