Skip to content

Commit

Permalink
Merge pull request #14 from tfm000/dev/module_names
Browse files Browse the repository at this point in the history
Dev/module names
  • Loading branch information
tfm000 authored Dec 4, 2023
2 parents ad8e1d9 + 0840a9e commit 8bf091a
Show file tree
Hide file tree
Showing 70 changed files with 338 additions and 358 deletions.
8 changes: 4 additions & 4 deletions docs/source/Copulas.rst
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ Producing a pairplot of the marginals::

mfitter2.pairplot()

.. image:: https://github.com/tfm000/sklarpy/blob/docs/readthedocs/media/mfitter_pairplot.png?raw=true
.. image:: https://github.com/tfm000/sklarpy/blob/main/media/mfitter_pairplot.png?raw=true
:alt: MarginalFitter Pair-Plot
:scale: 60%
:align: center
Expand Down Expand Up @@ -315,17 +315,17 @@ Plotting our fit::
fitted_copula.mc_cdf_plot(show=False)
plt.show()

.. image:: https://github.com/tfm000/sklarpy/blob/docs/readthedocs/media/PDF_Gh_PDF_Plot_Plot2.png?raw=true
.. image:: https://github.com/tfm000/sklarpy/blob/main/media/PDF_Gh_PDF_Plot_Plot2.png?raw=true
:alt: Generalized Hyperbolic PDF
:scale: 60%
:align: center

.. image:: https://github.com/tfm000/sklarpy/blob/docs/readthedocs/media/Copula_PDF_Gh_Copula_PDF_Plot_Plot2.png?raw=true
.. image:: https://github.com/tfm000/sklarpy/blob/main/media/Copula_PDF_Gh_Copula_PDF_Plot_Plot2.png?raw=true
:alt: Generalized Hyperbolic Copula PDF
:scale: 60%
:align: center

.. image:: https://github.com/tfm000/sklarpy/blob/docs/readthedocs/media/MC_CDF_Gh_MC_CDF_Plot_Plot2.png?raw=true
.. image:: https://github.com/tfm000/sklarpy/blob/main/media/MC_CDF_Gh_MC_CDF_Plot_Plot2.png?raw=true
:alt: Generalized Hyperbolic CDF
:scale: 60%
:align: center
Expand Down
6 changes: 3 additions & 3 deletions docs/source/Multivariate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,17 @@ Plotting our fitted distribution::
fitted_msh.marginal_pairplot(show=False)
plt.show()

.. image:: https://github.com/tfm000/sklarpy/blob/docs/readthedocs/media/PDF_Mvt_Shyperbolic_PDF_Plot_Plot.png?raw=true
.. image:: https://github.com/tfm000/sklarpy/blob/main/media/PDF_Mvt_Shyperbolic_PDF_Plot_Plot.png?raw=true
:alt: Symmetric Hyperbolic PDF
:scale: 60%
:align: center

.. image:: https://github.com/tfm000/sklarpy/blob/docs/readthedocs/media/MC_CDF_Mvt_Shyperbolic_MC_CDF_Plot_Plot.png?raw=true
.. image:: https://github.com/tfm000/sklarpy/blob/main/media/MC_CDF_Mvt_Shyperbolic_MC_CDF_Plot_Plot.png?raw=true
:alt: Symmetric Hyperbolic PDF
:scale: 60%
:align: center

.. image:: https://github.com/tfm000/sklarpy/blob/docs/readthedocs/media/mvt_shyperbolic_marginal_pair_plot.png?raw=true
.. image:: https://github.com/tfm000/sklarpy/blob/main/media/mvt_shyperbolic_marginal_pair_plot.png?raw=true
:alt: Symmetric Hyperbolic PDF
:scale: 60%
:align: center
Expand Down
6 changes: 3 additions & 3 deletions docs/source/Univariate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ And plot our fitted distribution::

fitted_gamma.plot()

.. image:: https://github.com/tfm000/sklarpy/blob/docs/readthedocs/media/univariate_continuous_example_figure1.png?raw=true
.. image:: https://github.com/tfm000/sklarpy/blob/main/media/univariate_continuous_example_figure1.png?raw=true
:alt: gamma plot
:align: center

Expand Down Expand Up @@ -258,7 +258,7 @@ And plot our fitted distribution::

fitted_poisson.plot()

.. image:: https://github.com/tfm000/sklarpy/blob/docs/readthedocs/media/univariate_discrete_example_figure1.png?raw=true
.. image:: https://github.com/tfm000/sklarpy/blob/main/media/univariate_discrete_example_figure1.png?raw=true
:alt: poisson plot
:align: center

Expand Down Expand Up @@ -347,7 +347,7 @@ finding our best fit::
Sum of Squared Error 9.18869
#Fitted Data Points 1000
.. image:: https://github.com/tfm000/sklarpy/blob/docs/readthedocs/media/univariate_fitter_example_figure1.png?raw=true
.. image:: https://github.com/tfm000/sklarpy/blob/main/media/univariate_fitter_example_figure1.png?raw=true
:alt: poisson plot
:align: center

Expand Down
4 changes: 2 additions & 2 deletions sklarpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from sklarpy._utils._serialize import load
from sklarpy._utils._object_printing import print_full
from sklarpy.utils._serialize import load
from sklarpy.utils._object_printing import print_full
2 changes: 0 additions & 2 deletions sklarpy/_plotting/__init__.py

This file was deleted.

12 changes: 0 additions & 12 deletions sklarpy/_utils/__init__.py

This file was deleted.

2 changes: 1 addition & 1 deletion sklarpy/copulas/_distributions/_archimedean.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from sklarpy.copulas._prefit_dists import PreFitCopula
from sklarpy.copulas._fitted_dists import FittedCopula
from sklarpy._utils import Params
from sklarpy.utils._params import Params
from sklarpy.copulas import MarginalFitter

__all__ = ['clayton_copula_gen', 'gumbel_copula_gen', 'frank_copula_gen']
Expand Down
2 changes: 1 addition & 1 deletion sklarpy/copulas/_distributions/_gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from sklarpy.copulas._prefit_dists import PreFitCopula
from sklarpy.copulas._fitted_dists import FittedCopula
from sklarpy._utils import Params
from sklarpy.utils._params import Params
from sklarpy.copulas import MarginalFitter

__all__ = ['gaussian_copula_gen']
Expand Down
2 changes: 1 addition & 1 deletion sklarpy/copulas/_distributions/_gaussian_kde.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sklarpy.copulas._prefit_dists import PreFitCopula
from sklarpy.copulas._fitted_dists import FittedCopula
from sklarpy.copulas import MarginalFitter
from sklarpy._utils import Params
from sklarpy.utils._params import Params

__all__ = ['gaussian_kde_copula_gen']

Expand Down
2 changes: 1 addition & 1 deletion sklarpy/copulas/_distributions/_generalized_hyperbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from sklarpy.copulas._prefit_dists import PreFitCopula
from sklarpy.copulas._fitted_dists import FittedCopula
from sklarpy._utils import Params
from sklarpy.utils._params import Params
from sklarpy.copulas import MarginalFitter
from sklarpy.univariate import gh

Expand Down
2 changes: 1 addition & 1 deletion sklarpy/copulas/_distributions/_hyperbolics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from sklarpy.copulas._distributions._generalized_hyperbolic import \
gen_hyperbolic_copula_gen
from sklarpy._utils import Params
from sklarpy.utils._params import Params

__all__ = ['marginal_hyperbolic_copula_gen', 'hyperbolic_copula_gen',
'nig_copula_gen']
Expand Down
2 changes: 1 addition & 1 deletion sklarpy/copulas/_distributions/_skewed_t.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from sklarpy.copulas._distributions._generalized_hyperbolic import \
gen_hyperbolic_copula_gen
from sklarpy._utils import Params
from sklarpy.utils._params import Params
from sklarpy.univariate.distributions import skewed_t

__all__ = ['skewed_t_copula_gen']
Expand Down
2 changes: 1 addition & 1 deletion sklarpy/copulas/_distributions/_student_t.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from sklarpy.copulas._prefit_dists import PreFitCopula
from sklarpy.copulas._fitted_dists import FittedCopula
from sklarpy._utils import Params
from sklarpy.utils._params import Params
from sklarpy.copulas import MarginalFitter

__all__ = ['student_t_copula_gen']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from sklarpy.copulas._distributions._generalized_hyperbolic import \
gen_hyperbolic_copula_gen
from sklarpy._utils import Params
from sklarpy.utils._params import Params

__all__ = ['sym_gen_hyperbolic_copula_gen']

Expand Down
5 changes: 4 additions & 1 deletion sklarpy/copulas/_fitted_dists.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import pandas as pd
from typing import Union, Iterable

from sklarpy._utils import TypeKeeper, Savable, Copyable, Params
from sklarpy.utils._type_keeper import TypeKeeper
from sklarpy.utils._serialize import Savable
from sklarpy.utils._copy import Copyable
from sklarpy.utils._params import Params

__all__ = ['FittedCopula']

Expand Down
67 changes: 48 additions & 19 deletions sklarpy/copulas/_prefit_dists.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@
from typing import Union, Iterable, Callable, Dict, List
import numpy as np
import pandas as pd
from collections import deque

from sklarpy.copulas import MarginalFitter
from sklarpy._utils import check_multivariate_data, TypeKeeper, Params, \
NotImplementedBase, get_mask
from sklarpy.utils._input_handlers import check_multivariate_data, get_mask
from sklarpy.utils._type_keeper import TypeKeeper
from sklarpy.utils._params import Params
from sklarpy.utils._not_implemented import NotImplementedBase
from sklarpy.multivariate._prefit_dists import PreFitContinuousMultivariate, \
FittedContinuousMultivariate
from sklarpy.univariate._fitted_dists import FittedUnivariateBase
from sklarpy._plotting import pair_plot, threeD_plot
from sklarpy.plotting._pair_plot import pair_plot
from sklarpy.plotting._threeD_plot import threeD_plot
from sklarpy.copulas._fitted_dists import FittedCopula

__all__ = ['PreFitCopula']


class PreFitCopula(NotImplementedBase):
"""A pre-fit copula model"""
__MAX_RVS_LOOPS: int = 100

def __init__(self, name: str, mv_object: PreFitContinuousMultivariate):
"""A pre-fit copula model.
Expand Down Expand Up @@ -796,17 +802,31 @@ def copula_rvs(self, size: int, copula_params: Union[Params, tuple],
distribution. These correspond to randomly sampled cdf /
pseudo-observation values of the univariate marginals.
"""
# generating random variables from multivariate distribution
raw_mv_rvs: np.ndarray = self._mv_object.rvs(size, copula_params)

# bounding these above and below
eps: float = 10 ** -5
rvs_df: pd.DataFrame = pd.DataFrame(raw_mv_rvs)
rvs_df[rvs_df < 0] = eps
rvs_df[rvs_df > 1] = 1 - eps
mv_rvs: np.ndarray = rvs_df.to_numpy()

return self._g_to_u(mv_rvs, copula_params)
num_loops: int = 0
d: int = self._mv_object._get_dim(
self._mv_object._get_params(copula_params))
valid_copula_rvs: deque = deque()
while size > 0:
# generating random variables from multivariate distribution
mv_rvs: np.ndarray = self._mv_object.rvs(size, copula_params)

# converting to copula rvs
raw_copula_rvs: np.ndarray = self._g_to_u(mv_rvs, copula_params)

# filtering out invalid copula rvs (not in [0, 1]^d)
mask: np.ndarray = ((raw_copula_rvs > 0) & (raw_copula_rvs < 1)
).sum(axis=1) == d
copula_rvs = raw_copula_rvs[mask]
valid_copula_rvs.append(copula_rvs)

# repeating until sample size reached
size -= copula_rvs.shape[0]
num_loops += 1
if num_loops > self.__MAX_RVS_LOOPS:
raise ArithmeticError(f"Unable to generate valid copula rvs. "
f"Max number of retries reached: "
f"{self.__MAX_RVS_LOOPS}")
return np.concatenate(valid_copula_rvs, axis=0)

def _get_components_summary(self,
fitted_mv_object: FittedContinuousMultivariate,
Expand Down Expand Up @@ -1008,11 +1028,20 @@ def fit(self, data: Union[pd.DataFrame, np.ndarray, None] = None,
"params do not match.")

# generating data to use when calculating statistics
data_array: np.ndarray = self.rvs(
size=10**3, copula_params=fitted_mv_object.params,
mdists=mdists_dict, ppf_approx=True
) if data is None \
else check_multivariate_data(data, allow_1d=True, allow_nans=True)
try:
data_array: np.ndarray = self.rvs(
size=10**3, copula_params=fitted_mv_object.params,
mdists=mdists_dict, ppf_approx=True) if data is None \
else check_multivariate_data(
data, allow_1d=True, allow_nans=True)

except ArithmeticError as e:
if str(e) != (f"Unable to generate valid copula rvs. Max number "
f"of retries reached: {self.__MAX_RVS_LOOPS}"):
raise
else:
data_array: np.ndarray = np.full((10**3, d), np.nan,
dtype=float)

# fitting TypeKeeper object
type_keeper: TypeKeeper = TypeKeeper(data_array)
Expand Down
9 changes: 6 additions & 3 deletions sklarpy/copulas/marginal_fitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import pandas as pd
from typing import Union, Iterable

from sklarpy._utils import FitError, TypeKeeper, check_multivariate_data, \
get_iterator, Savable
from sklarpy.utils._errors import FitError
from sklarpy.utils._type_keeper import TypeKeeper
from sklarpy.utils._input_handlers import check_multivariate_data
from sklarpy.utils._iterator import get_iterator
from sklarpy.utils._serialize import Savable
from sklarpy.univariate import UnivariateFitter
from sklarpy._plotting import pair_plot
from sklarpy.plotting._pair_plot import pair_plot

__all__ = ['MarginalFitter']

Expand Down
2 changes: 1 addition & 1 deletion sklarpy/misc/correlation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import warnings
from typing import Tuple, Union

from sklarpy._utils import check_multivariate_data
from sklarpy.utils._input_handlers import check_multivariate_data

__all__ = ['CorrelationMatrix']

Expand Down
2 changes: 1 addition & 1 deletion sklarpy/misc/gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Callable, Union, Iterable
from collections import deque

from sklarpy._utils import check_univariate_data
from sklarpy.utils._input_handlers import check_univariate_data

__all__ = ['gradient_1d']

Expand Down
4 changes: 3 additions & 1 deletion sklarpy/multivariate/_distributions/_archimedean.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

from sklarpy.multivariate._prefit_dists import PreFitContinuousMultivariate
from sklarpy.multivariate._fitted_dists import FittedContinuousMultivariate
from sklarpy._utils import get_iterator, FitError, Params
from sklarpy.utils._params import Params
from sklarpy.utils._iterator import get_iterator
from sklarpy.utils._errors import FitError
from sklarpy.misc import debye

__all__ = ['multivariate_clayton_gen', 'multivariate_gumbel_gen',
Expand Down
2 changes: 1 addition & 1 deletion sklarpy/multivariate/_distributions/_gaussian_kde.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from sklarpy.multivariate._prefit_dists import PreFitContinuousMultivariate
from sklarpy.multivariate._fitted_dists import FittedContinuousMultivariate
from sklarpy._utils import Params
from sklarpy.utils._params import Params

__all__ = ['multivariate_gaussian_kde_gen']

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from sklarpy.univariate import gig
from sklarpy.univariate._distributions import _gh
from sklarpy.misc import CorrelationMatrix, kv
from sklarpy._utils import Params
from sklarpy.utils._params import Params

__all__ = ['multivariate_gen_hyperbolic_gen']

Expand Down
2 changes: 1 addition & 1 deletion sklarpy/multivariate/_distributions/_hyperbolics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sklarpy.multivariate._distributions._generalized_hyperbolic import \
multivariate_gen_hyperbolic_gen
from sklarpy.multivariate._prefit_dists import PreFitContinuousMultivariate
from sklarpy._utils import Params
from sklarpy.utils._params import Params

__all__ = ['multivariate_marginal_hyperbolic_gen',
'multivariate_hyperbolic_gen', 'multivariate_nig_gen']
Expand Down
2 changes: 1 addition & 1 deletion sklarpy/multivariate/_distributions/_normal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sklarpy.multivariate._prefit_dists import PreFitContinuousMultivariate
from sklarpy.misc import CorrelationMatrix
from sklarpy.multivariate._fitted_dists import FittedContinuousMultivariate
from sklarpy._utils import Params
from sklarpy.utils._params import Params

__all__ = ['multivariate_normal_gen']

Expand Down
2 changes: 1 addition & 1 deletion sklarpy/multivariate/_distributions/_skewed_t.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
multivariate_gen_hyperbolic_gen
from sklarpy.multivariate._distributions._student_t import \
multivariate_student_t_gen
from sklarpy._utils import Params
from sklarpy.utils._params import Params
from sklarpy.misc import kv
from sklarpy.multivariate._prefit_dists import PreFitContinuousMultivariate
from sklarpy.univariate import ig
Expand Down
2 changes: 1 addition & 1 deletion sklarpy/multivariate/_distributions/_student_t.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from sklarpy.multivariate._prefit_dists import PreFitContinuousMultivariate
from sklarpy.multivariate._fitted_dists import FittedContinuousMultivariate
from sklarpy._utils import Params
from sklarpy.utils._params import Params
from sklarpy.misc import CorrelationMatrix

__all__ = ['multivariate_student_t_gen']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sklarpy.multivariate._prefit_dists import PreFitContinuousMultivariate
from sklarpy.multivariate._distributions._generalized_hyperbolic import \
multivariate_gen_hyperbolic_gen
from sklarpy._utils import Params
from sklarpy.utils._params import Params

__all__ = ['multivariate_sym_gen_hyperbolic_gen']

Expand Down
Loading

0 comments on commit 8bf091a

Please sign in to comment.