Commit 8279cb97 authored by dsbowen's avatar dsbowen
Browse files

Added class method to nonparametric.

parent 9651150e
Loading
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.245.2/containers/python-3/.devcontainer/base.Dockerfile

# [Choice] Python version (use -bullseye variants on local arm64/Apple Silicon): 3, 3.10, 3.9, 3.8, 3.7, 3.6, 3-bullseye, 3.10-bullseye, 3.9-bullseye, 3.8-bullseye, 3.7-bullseye, 3.6-bullseye, 3-buster, 3.10-buster, 3.9-buster, 3.8-buster, 3.7-buster, 3.6-buster
ARG VARIANT="3.10-bullseye"
FROM mcr.microsoft.com/vscode/devcontainers/python:0-${VARIANT}

# [Choice] Node.js version: none, lts/*, 16, 14, 12, 10
ARG NODE_VERSION="none"
RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi

# [Optional] If your pip requirements rarely change, uncomment this section to add them to the image.
COPY requirements.txt /tmp/pip-tmp/
RUN pip3 --disable-pip-version-check --no-cache-dir install -r /tmp/pip-tmp/requirements.txt \
   && rm -rf /tmp/pip-tmp
RUN pip3 install ipykernel

# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
#     && apt-get -y install --no-install-recommends <your-package-list-here>

# [Optional] Uncomment this line to install global node packages.
# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1
 No newline at end of file
+58 −0
Original line number Diff line number Diff line
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.245.2/containers/python-3
{
	"name": "multiple-inference",
	"build": {
		"dockerfile": "Dockerfile",
		"context": "..",
		"args": { 
			// Update 'VARIANT' to pick a Python version: 3, 3.10, 3.9, 3.8, 3.7, 3.6
			// Append -bullseye or -buster to pin to an OS version.
			// Use -bullseye variants on local on arm64/Apple Silicon.
			"VARIANT": "3.10-bullseye",
			// Options
			"NODE_VERSION": "lts/*"
		}
	},

	// Configure tool-specific properties.
	"customizations": {
		// Configure properties specific to VS Code.
		"vscode": {
			// Set *default* container specific settings.json values on container create.
			"settings": { 
				"python.defaultInterpreterPath": "/usr/local/bin/python",
				"python.linting.enabled": true,
				"python.linting.pylintEnabled": true,
				"python.formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8",
				"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
				"python.formatting.yapfPath": "/usr/local/py-utils/bin/yapf",
				"python.linting.banditPath": "/usr/local/py-utils/bin/bandit",
				"python.linting.flake8Path": "/usr/local/py-utils/bin/flake8",
				"python.linting.mypyPath": "/usr/local/py-utils/bin/mypy",
				"python.linting.pycodestylePath": "/usr/local/py-utils/bin/pycodestyle",
				"python.linting.pydocstylePath": "/usr/local/py-utils/bin/pydocstyle",
				"python.linting.pylintPath": "/usr/local/py-utils/bin/pylint"
			},
			
			// Add the IDs of extensions you want installed when the container is created.
			"extensions": [
				"ms-python.python",
				"ms-python.vscode-pylance",
				"ms-toolsai.jupyter",
                "njpwerner.autodocstring",
                "oderwat.indent-rainbow",
				"janisdd.vscode-edit-csv"
			]
		}
	},

	// Use 'forwardPorts' to make a list of ports inside the container available locally.
	// "forwardPorts": [],

	// Use 'postCreateCommand' to run commands after the container is created.
	"postCreateCommand": "pip3 install -e .",

	// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
	"remoteUser": "vscode"
}
+8 −2
Original line number Diff line number Diff line
@@ -79,7 +79,9 @@ def _test_hypotheses(
        # stepwise rejection will take a long time with more than 10000 parameters
        rejected, newly_rejected = np.full(z_stat_rvs.shape[1], False), None
        while newly_rejected is None or (newly_rejected.any() and not rejected.all()):
            critical_value = np.quantile(z_stat_rvs[:, ~rejected].max(axis=1), 1 - alpha)
            critical_value = np.quantile(
                z_stat_rvs[:, ~rejected].max(axis=1), 1 - alpha
            )
            newly_rejected = (z_stats > critical_value) & ~rejected
            rejected = rejected | newly_rejected
    else:
@@ -116,7 +118,11 @@ class ConfidenceSetResults(ResultsBase):
            )
            / self._std_diagonal
        )
        self._max_z_stats = self._z_stat_rvs.copy() if self.model.n_params == 1 else abs(self._z_stat_rvs).max(axis=1)
        self._max_z_stats = (
            self._z_stat_rvs.copy()
            if self.model.n_params == 1
            else abs(self._z_stat_rvs).max(axis=1)
        )
        self._set_pvalues()
        self._set_qvalues()

+38 −1
Original line number Diff line number Diff line
@@ -149,6 +149,28 @@ class nonparametric(rv_continuous):
        self._scale = 1
        self._scale = 1 / quad(self._pdf, self.xk[0], self.xk[-1])[0]

    @classmethod
    def from_cdf(cls, values, *args, **kwargs):
        """Create a nonparametric distribution using the CDF instead of the PMF.

        Args:
        values (tuple[np.array, np.array]): (n,) array of x values, (n,) array of the
            cumulative distribution function evaluated at x.
        *args (Any): Passed to :class:`nonparametric` constructor.
        **kwargs (Any): Passed to :class:`nonparametric` constructor.

        Returns:
            nonparametric: Nonparametric distribution.
        """
        xk, cdf_values = np.array(values[0], float), np.array(values[1], float)
        argsort = xk.argsort()
        diff = np.diff(cdf_values[argsort]) / np.diff(xk[argsort])
        pk = np.nanmean([np.insert(diff, 0, np.nan), np.append(diff, np.nan)], axis=0)
        pk = pk[argsort.argsort()]
        new = cls((xk, pk), *args, **kwargs)
        new._cdf_values = cdf_values
        return new

    def _pdf(self, x: np.ndarray) -> np.ndarray:
        x = np.atleast_1d(x)
        pdf = np.zeros(len(x))
@@ -164,7 +186,22 @@ class nonparametric(rv_continuous):
        cdf[in_range] = interp1d(self.xk, self._cdf_values, kind=self._kind)(
            x[in_range]
        )
        return cdf
        # make sure the CDF is monotonically increasing
        cdf = cdf[x.argsort()]
        min_correction = cdf.copy()
        while (np.diff(min_correction) < 0).any():
            min_correction = np.clip(
                min_correction, a_min=np.insert(min_correction[:-1], 0, 0), a_max=None
            )

        max_correction = cdf.copy()
        while (np.diff(max_correction) < 0).any():
            max_correction = np.clip(
                max_correction, a_min=None, a_max=np.append(max_correction[1:], 1)
            )

        cdf = 0.5 * (min_correction + max_correction)
        return cdf[x.argsort().argsort()]

    def _ppf(self, q: np.ndarray) -> np.ndarray:
        return weighted_quantile(self.xk, q, self.pk)
+6 −0
Original line number Diff line number Diff line
@@ -71,6 +71,12 @@ class TestNonparametric:
    x = np.linspace(-3, 3)
    dist = nonparametric((x, norm.pdf(x)))

    def test_from_cdf(self):
        dist_from_cdf = nonparametric.from_cdf((self.x, norm.cdf(self.x)))
        np.testing.assert_array_almost_equal(
            self.dist.pdf(self.x), dist_from_cdf.pdf(self.x), decimal=2
        )

    def test_pdf(self):
        np.testing.assert_array_almost_equal(
            self.dist.pdf(self.x), norm.pdf(self.x), decimal=2
+14 −14

File changed.

Contains only whitespace changes.