Skip to content

Commit

Permalink
testing without objects
Browse files Browse the repository at this point in the history
Took 1 hour 19 minutes
  • Loading branch information
tfm000 committed Sep 29, 2023
1 parent f43b154 commit 6f3e758
Show file tree
Hide file tree
Showing 28 changed files with 211 additions and 37 deletions.
163 changes: 162 additions & 1 deletion sklarpy/tests/multivariate/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Contains pytest fixtures for testing SklarPy multivariate code
import pandas as pd
import numpy as np
import pytest
import scipy.stats
import numpy as np

from sklarpy.multivariate import distributions_map

Expand Down Expand Up @@ -41,3 +41,164 @@ def pd_mvt_discrete_data():
@pytest.fixture(scope="session", autouse=True)
def mv_dists_to_test():
return distributions_map['all']


@pytest.fixture(scope="session", autouse=True)
def params_2d():
return {
'multivariate_normal': (
np.array([[-0.09739442], [0.06748115]]),
np.array([[1.22604687e+00, 9.60076079e-18],
[9.60076079e-18, 7.30245004e-01]])),

'multivariate_student_t': (
100.0,
np.array([[-0.09739442], [0.06748115]]),
np.array([[1.20152593, 0.19624622],
[0.19624622, 0.7156401]])),

'multivariate_gen_hyperbolic': (
-1.1788364839820724, 10.0, 10.0,
np.array([[0.84415713], [-0.45785498]]),
np.array([[1.2253664, 0.26909617],
[0.26909617, 0.75701993]]),
np.array([[-1.00453046], [0.56047505]])),

'multivariate_marginal_hyperbolic': (
6.430016040119274, 10.0,
np.array([[0.69052415], [-0.39804144]]),
np.array([[1.22869356, 0.2616279],
[0.2616279, 0.75174415]]),
np.array([[-0.82414258], [0.48692467]])),

'multivariate_hyperbolic': (
5.584106488470228, 10.0,
np.array([[0.65995643], [-0.3874908]]),
np.array([[1.2296575, 0.26089854],
[0.26089854, 0.75084492]]),
np.array([[-0.78967095], [0.47438798]])),

'multivariate_nig': (
8.986208938433768, 10.0,
np.array([[0.78169119], [-0.43129769]]),
np.array([[1.22628047, 0.26411374],
[0.26411374, 0.7542892]]),
np.array([[-0.92734838], [0.52616235]])),

'multivariate_skewed_t': (
10.0,
np.array([[0.15711164], [-0.10459758]]),
np.array([[1.22892318, 0.21921408],
[0.21921408, 0.7350084]]),
np.array([[-0.20360485], [0.13766299]])),

'multivariate_sym_gen_hyperbolic': (
-0.4268651272656109, 10.0, 10.0,
np.array([[-0.09739442], [0.06748115]]),
np.array([[1.22604687, 0.20025125],
[0.20025125, 0.730245]])),

'multivariate_sym_marginal_hyperbolic': (
7.523270258556909, 10.0,
np.array([[-0.09739442], [0.06748115]]),
np.array([[1.22604687, 0.20025125],
[0.20025125, 0.730245]])),

'multivariate_sym_hyperbolic': (
6.610028122530992, 10.0,
np.array([[-0.09739442], [0.06748115]]),
np.array([[1.22604687, 0.20025125],
[0.20025125, 0.730245]])),

'multivariate_sym_nig': (
10.0, 10.0,
np.array([[-0.09739442], [0.06748115]]),
np.array([[1.22604687, 0.20025125],
[0.20025125, 0.730245]]))
}


@pytest.fixture(scope="session", autouse=True)
def params_3d():
return {
'multivariate_normal': (
np.array([[-0.06068104], [-0.10572418], [0.03489784]]),
np.array([[1.21898949e+00, 9.34403992e-17, 8.06538495e-17],
[3.91669234e-18, 9.03438954e-01, 7.39590407e-19],
[5.18678009e-18, 3.39803556e-17, 1.05766110e+00]])),

'multivariate_student_t': (
100.0,
np.array([[-0.06068104], [-0.10572418], [0.03489784]]),
np.array([[1.1946097, 0.09775865, -0.00776843],
[0.09775865, 0.88537018, 0.08621716],
[-0.00776843, 0.08621716, 1.03650788]])),

'multivariate_gen_hyperbolic': (
-1.782382671254414, 10.0, 10.0,
np.array([[-0.62581127], [-1.87660553], [0.13564066]]),
np.array([[ 1.38090727e+00, -2.01241655e-04, -2.60836067e-03],
[-2.01241655e-04, 6.86684932e-01, 1.23061470e-01],
[-2.60836067e-03, 1.23061470e-01, 1.22915669e+00]]),
np.array([[0.63853468], [2.00090016], [-0.11382825]])),

'multivariate_marginal_hyperbolic': (
6.205679755772674, 10.0,
np.array([[-0.4760037], [-1.50649459], [0.0978438 ]]),
np.array([[1.33664701, 0.03084231, -0.0052302],
[0.03084231, 0.73684715, 0.1103901],
[-0.0052302, 0.1103901, 1.17990614]]),
np.array([[0.44089418], [1.4870162], [-0.06682156]])),

'multivariate_hyperbolic': (
4.669931146010268, 10.0,
np.array([[-0.43035465], [-1.40305972], [0.08801923]]),
np.array([[1.32964083, 0.03793152, -0.00580953],
[0.03793152, 0.74609703, 0.10788308],
[-0.00580953, 0.10788308, 1.17118297]]),
np.array([[0.38622221], [1.35541132], [-0.05549939]])),

'multivariate_nig': (
8.517549891625043, 10.0,
np.array([[-0.54136409], [-1.66194387], [0.11216012]]),
np.array([[1.34739657, 0.02110339, -0.00440445],
[0.02110339, 0.72340899, 0.11398558],
[-0.00440445, 0.11398558, 1.19285811]]),
np.array([[0.52083656], [1.68621739], [-0.08371633]])),

'multivariate_skewed_t': (
10.0,
np.array([[-0.1552794], [-0.58786565], [0.03910667]]),
np.array([[ 1.25348961, 0.0871567, -0.00803451],
[0.0871567, 0.8514119, 0.09138582],
[-0.00803451, 0.09138582, 1.09025713]]),
np.array([[0.07567869], [0.38571318], [-0.00336706]])),

'multivariate_sym_gen_hyperbolic': (
-0.33835936636262204, 10.0, 10.0,
np.array([[-0.06068104], [-0.10572418], [0.03489784]]),
np.array([[1.21898949, 0.09975372, -0.00792697],
[0.09975372, 0.90343895, 0.08797669],
[-0.00792697, 0.08797669, 1.0576611]])),

'multivariate_sym_marginal_hyperbolic': (
7.799728823974279, 10.0,
np.array([[-0.06068104], [-0.10572418], [0.03489784]]),
np.array([[1.21898949, 0.09975372, -0.00792697],
[0.09975372, 0.90343895, 0.08797669],
[-0.00792697, 0.08797669, 1.0576611]])),

'multivariate_sym_hyperbolic': (
5.992259894031568, 10.0,
np.array([[-0.06068104], [-0.10572418], [0.03489784]]),
np.array([[1.21898949, 0.09975372, -0.00792697],
[0.09975372, 0.90343895, 0.08797669],
[-0.00792697, 0.08797669, 1.0576611]])),

'multivariate_sym_nig': (
10.0, 9.96376429525435,
np.array([[-0.06068104], [-0.10572418], [0.03489784]]),
np.array([[1.21898949, 0.09975372, -0.00792697],
[0.09975372, 0.90343895, 0.08797669],
[-0.00792697, 0.08797669, 1.0576611]]))
}
21 changes: 14 additions & 7 deletions sklarpy/tests/multivariate/helpers.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
# Contains helper functions for testing SklarPy multivariate code
import os
import numpy as np

from sklarpy.multivariate import *
from sklarpy import load

__all__ = ['get_dist']


def get_dist(name: str, d: int = 3) -> tuple:
def get_dist(name: str, params_dict: dict, data: np.ndarray = None) -> tuple:
"""Fits a distribution to data.
Parameters
----------
name : str
The name of the distribution as in distributions_map
d: int
The dimension of the data.
params_dict: dict
Dictionary containing the parameters of each distribution.
data: np.ndarray
Multivariate data to fit the distribution too if its params are not
specified in params_dict.
Returns
-------
res: tuple
non-fitted dist, fitted dist, parameters for fitted dist
"""
dist = eval(name)
path: str = f'{os.getcwd()}\\sklarpy\\tests\\multivariate\\{name}_{d}D'
params = load(path)
fitted = dist.fit(params=params)
if name in params_dict:
params: tuple = params_dict[name]
fitted = dist.fit(params=params)
else:
fitted = dist.fit(data=data)
params: tuple = fitted.params.to_tuple
return dist, fitted, params
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed sklarpy/tests/multivariate/multivariate_nig_2D.pickle
Binary file not shown.
Binary file removed sklarpy/tests/multivariate/multivariate_nig_3D.pickle
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
38 changes: 20 additions & 18 deletions sklarpy/tests/multivariate/test_fitted_dists.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
import pytest
from typing import Callable
import matplotlib.pyplot as plt
import scipy.stats

from sklarpy.multivariate import *
from sklarpy.tests.multivariate.helpers import get_dist
from sklarpy._utils import Params


def test_fitted_logpdf_pdf_cdf_mc_cdf(
mvt_continuous_data, mvt_discrete_data, pd_mvt_continuous_data,
pd_mvt_discrete_data, mv_dists_to_test):
pd_mvt_discrete_data, mv_dists_to_test, params_2d):
"""Testing the logpdf, pdf, cdf and mc-cdf functions of fitted multivariate
distributions"""
eps: float = 10 ** -5
num_generate: int = 10

for name in mv_dists_to_test:
_, fitted, _ = get_dist(name, 2)
_, fitted, _ = get_dist(name, params_2d, mvt_continuous_data)
for func_str in ('logpdf', 'pdf', 'cdf', 'mc_cdf'):
func: Callable = eval(f'fitted.{func_str}')
cdf_num: int = 10
Expand Down Expand Up @@ -67,10 +67,10 @@ def test_fitted_logpdf_pdf_cdf_mc_cdf(
func(x=new_dataset, num_generate=num_generate)


def test_fitted_rvs(mv_dists_to_test):
def test_fitted_rvs(mv_dists_to_test, params_2d, mvt_continuous_data):
"""Testing the rvs functions of fitted multivariate distributions."""
for name in mv_dists_to_test:
_, fitted, _ = get_dist(name)
_, fitted, _ = get_dist(name, params_2d, mvt_continuous_data)
for size in (1, 2, 5, 101):
rvs = fitted.rvs(size=size)
np_rvs: np.ndarray = np.asarray(rvs)
Expand All @@ -87,11 +87,11 @@ def test_fitted_rvs(mv_dists_to_test):

def test_fitted_scalars(mvt_continuous_data, mvt_discrete_data,
pd_mvt_continuous_data, pd_mvt_discrete_data,
mv_dists_to_test):
mv_dists_to_test, params_2d):
"""Testing the likelihood, loglikelihood, AIC and BIC functions of
multivariate fitted distributions."""
for name in mv_dists_to_test:
_, fitted, _ = get_dist(name, 2)
_, fitted, _ = get_dist(name, params_2d, mvt_continuous_data)
for func_str in ('likelihood', 'loglikelihood', 'aic', 'bic'):
func: Callable = eval(f'fitted.{func_str}')
for data in (mvt_continuous_data, mvt_discrete_data,
Expand Down Expand Up @@ -124,12 +124,14 @@ def test_fitted_scalars(mvt_continuous_data, mvt_discrete_data,
func(data=new_dataset)


def test_fitted_plots(mv_dists_to_test):
def test_fitted_plots(mv_dists_to_test, params_2d, params_3d, mvt_continuous_data):
"""Testing the marginal_pairplot, pdf_plot, cdf_plot and mc_cdf_plot
methods of fitted multivariate distributions."""
mvt_continuous_data_3d: np.ndarray = scipy.stats.multivariate_normal.rvs(
size=(mvt_continuous_data.shape[0], 3))
for name in mv_dists_to_test:
_, fitted_2d, _ = get_dist(name, 2)
_, fitted_3d, _ = get_dist(name, 3)
_, fitted_2d, _ = get_dist(name, params_2d, mvt_continuous_data)
_, fitted_3d, _ = get_dist(name, params_3d, mvt_continuous_data_3d)
for func_str in ('marginal_pairplot', 'pdf_plot',
'cdf_plot', 'mc_cdf_plot'):
func_2d: Callable = eval(f'fitted_2d.{func_str}')
Expand All @@ -153,10 +155,10 @@ def test_fitted_plots(mv_dists_to_test):
plt.close()


def test_fitted_params(mv_dists_to_test):
def test_fitted_params(mv_dists_to_test, params_2d, mvt_continuous_data):
"""Testing the params attribute of fitted multivariate distributions."""
for name in mv_dists_to_test:
_, fitted, _ = get_dist(name, 2)
_, fitted, _ = get_dist(name, params_2d, mvt_continuous_data)

# checking exists
assert 'params' in dir(fitted), f"params is not an attribute of " \
Expand All @@ -167,11 +169,11 @@ def test_fitted_params(mv_dists_to_test):
f"params of fitted {name} is not a Params object."


def test_fitted_integers(mv_dists_to_test):
def test_fitted_integers(mv_dists_to_test, params_2d, mvt_continuous_data):
"""Testing the num_params, num_scalar_params, num_variables and
fitted_num_data_points of fitted multivariate distributions."""
for name in mv_dists_to_test:
_, fitted, params = get_dist(name)
_, fitted, params = get_dist(name, params_2d, mvt_continuous_data)

for func_str in ('num_params', 'num_scalar_params', 'num_variables',
'fitted_num_data_points'):
Expand All @@ -188,10 +190,10 @@ def test_fitted_integers(mv_dists_to_test):
f"object."


def test_fitted_converged(mv_dists_to_test):
def test_fitted_converged(mv_dists_to_test, params_2d, mvt_continuous_data):
"""Testing converged attributes of fitted multivariate distributions."""
for name in mv_dists_to_test:
_, fitted, _ = get_dist(name)
_, fitted, _ = get_dist(name, params_2d, mvt_continuous_data)

# testing exists
assert 'converged' in dir(fitted), \
Expand All @@ -202,10 +204,10 @@ def test_fitted_converged(mv_dists_to_test):
f"converged is not a boolean value for fitted {name}."


def test_fitted_summaries(mv_dists_to_test):
def test_fitted_summaries(mv_dists_to_test, params_2d, mvt_continuous_data):
"""Testing the summaries of fitted multivariate distributions."""
for name in mv_dists_to_test:
_, fitted, _ = get_dist(name)
_, fitted, _ = get_dist(name, params_2d, mvt_continuous_data)

# testing exists
assert 'summary' in dir(fitted), \
Expand Down
Loading

0 comments on commit 6f3e758

Please sign in to comment.