<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def marginalize(self, variables, inplace=True):
 """ Marginalize the distribution with respect to the given variables. Parameters variables: list, array-like List of variables to be removed from the marginalized distribution. inplace: boolean If inplace=True it will modify the factor itself, else would return a new CustomDistribution instance. Returns ------- Marginalized distribution or None: if inplace=True (default) returns None if inplace=False returns a new CustomDistribution instance. Examples -------- ['x1', 'x2'] 0.058549831524319168 ['x1'] 0.24197072451914328 """

        if len(variables) == 0:
            raise ValueError("Shouldn't be calling marginalize over no variable.")

        if not isinstance(variables, (list, tuple, np.ndarray)):
            raise TypeError("variables: Expected type iterable, "
                            "got: {var_type}".format(var_type=type(variables)))

        for var in variables:
            if var not in self.variables:
                raise ValueError("{var} not in scope.".format(var=var))

        phi = self if inplace else self.copy()

        all_var = [var for var in self.variables]
        var_to_keep = [var for var in self.variables if var not in variables]
        reordered_var_index = [all_var.index(var) for var in variables + var_to_keep]
        pdf = phi._pdf

        # The arguments need to be reordered because integrate.nquad
        # integrates the first n-arguments of the function passed.

        def reordered_pdf(*args):
            # ordered_args restores the original order as it was in self.variables
            ordered_args = [args[reordered_var_index.index(index_id)] for index_id in range(len(all_var))]
            return pdf(*ordered_args)

        def marginalized_pdf(*args):
            return integrate.nquad(reordered_pdf, [[-np.inf, np.inf] for i in range(len(variables))], args=args)[0]

        phi._pdf = marginalized_pdf
        phi.variables = var_to_keep

        if not inplace:
            return phi