Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Dec 16, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fracdiff/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .base import fdiff
from .fdiff import fdiff
from .fracdiff import Fracdiff
from .fracdiffstat import FracdiffStat
40 changes: 21 additions & 19 deletions fracdiff/base.py → fracdiff/fdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,39 +41,41 @@ def fdiff(
Parameters
----------
a : array_like
Input array.
The input array.
n : float, default=1.0
The order of differentiation.
For integer `n`, it returns the same output with ``numpy.diff``.
If ``n`` is an integer, returns the same output with ``numpy.diff``.
axis : int, default=-1
The axis along which the difference is taken, default is the last axis.
prepend, append : array_like, optional
Values to prepend or append to `a` along axis prior to performing
the difference.
The axis along which differentiation is performed, default is the last axis.
prepend : array_like, optional
Values to prepend to ``a`` along axis prior to performing the differentiation.
Scalar values are expanded to arrays with length 1 in the direction of axis and
the shape of the input array in along all other axes.
Otherwise the dimension and shape must match `a` except along axis.
Otherwise the dimension and shape must match ``a`` except along axis.
append : array_like, optional
Values to append.
window : int, default=10
Number of observations to compute each element in the output.
mode : {"full", "valid"}, default="full"
"full" (default) :
Return elements where at least one coefficient is used.
Output size along `axis` is `M` where `M` is the length of `a`
(plus the lengths of append and prepend if these are given).
At the beginning of a time-series, boundary effects may be seen.
Return elements where at least one coefficient of fracdiff is used.
Output size along ``axis`` is :math:`L_{\\mathrm{in}}`
where :math:`L_{\\mathrm{in}}` is the length of ``a`` along ``axis``
(plus the lengths of ``append`` and ``prepend``).
Boundary effects may be seen at the At the beginning of a time-series.
"valid" :
Return elements where all coefficients are used.
Output size along `axis` is `M - window` where `M` is the length of
`a` (plus the lengths of append and prepend if these are given).
At the beginning of a time-series, boundary effects is not seen.
Return elements where all coefficients of fracdiff are used.
Output size along ``axis`` is
:math:`L_{\\mathrm{in}} - \\mathrm{window} + 1` where
where :math:`L_{\\mathrm{in}}` is the length of ``a`` along ``axis``
(plus the lengths of ``append`` and ``prepend``).
Boundary effects are not seen.

Returns
-------
fdiff : numpy.array
fdiff : numpy.ndarray
The fractional differentiation.
The shape of the output is the same as `a` except along `axis`.
The dimension is `a.shape[axis] - window + d1 + d2` with
`d1` and `d2` being the dimension of ``prepend`` and ``append``, respectively.
The shape of the output is the same as ``a`` except along ``axis``.

Examples
--------
Expand Down
13 changes: 3 additions & 10 deletions fracdiff/fracdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from sklearn.utils.validation import check_array
from sklearn.utils.validation import check_is_fitted

from fracdiff.base import fdiff
from fracdiff.base import fdiff_coef
from fracdiff.fdiff import fdiff
from fracdiff.fdiff import fdiff_coef


class Fracdiff(TransformerMixin):
Expand All @@ -17,14 +17,7 @@ class Fracdiff(TransformerMixin):
window : int > 0 or None, default 10
Number of observations to compute each element in the output.
mode : {"full", "valid"}, default "full"
"full" (default) :
Return elements where at least one coefficient is used.
Shape of a transformed array is the same with the original array.
At the beginning of a transformed array, boundary effects may be seen.
"valid" :
Return elements where all coefficients are used.
Output size along axis 1 is `n_features - window`.
At the beginning of a time-series, boundary effects is not seen.
See :func:`fracdiff.fdiff` for details.
window_policy : {"fixed"}, default "fixed"
"fixed" (default) :
Fixed window method.
Expand Down
19 changes: 6 additions & 13 deletions fracdiff/fracdiffstat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sklearn.utils.validation import check_array
from sklearn.utils.validation import check_is_fitted

from .base import fdiff
from .fdiff import fdiff
from .fracdiff import Fracdiff
from .stat import StatTester

Expand All @@ -20,14 +20,7 @@ class FracdiffStat(TransformerMixin, BaseEstimator):
window : int > 0 or None, default 10
Number of observations to compute each element in the output.
mode : {"full", "valid"}, default "full"
"full" (default) :
Return elements where at least one coefficient is used.
Shape of a transformed array is the same with the original array.
At the beginning of a transformed array, boundary effects may be seen.
"valid" :
Return elements where all coefficients are used.
Output size along axis 1 is `n_features - window`.
At the beginning of a time-series, boundary effects is not seen.
See :func:`fracdiff.fdiff` for details.
window_policy : {"fixed"}, default "fixed"
If "fixed" :
Fixed window method.
Expand Down Expand Up @@ -63,10 +56,10 @@ class FracdiffStat(TransformerMixin, BaseEstimator):

Note
----
If `upper`th differentiation of series is still non-stationary,
order_ is set to ``numpy.nan``.
If `lower`th differentiation of series is already stationary,
order_ is set to `lower`, but the true value may be smaller.
If ``upper`` th differentiation of series is still non-stationary,
``order_`` is set to ``numpy.nan``.
If ``lower`` th differentiation of series is already stationary,
``order_`` is set to ``lower``, but the true value may be smaller.

Examples
--------
Expand Down
2 changes: 1 addition & 1 deletion fracdiff/tol.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np

from fracdiff.base import fdiff_coef
from fracdiff.fdiff import fdiff_coef


def window_from_tol_coef(n, tol_coef, max_window=2 ** 12) -> int:
Expand Down
10 changes: 6 additions & 4 deletions fracdiff/torch/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import torch
from torch import Tensor

from .. import base
from ..fdiff import fdiff_coef as fdiff_coef_numpy


def fdiff_coef(d: float, window: int) -> torch.Tensor:
Expand All @@ -29,7 +29,7 @@ def fdiff_coef(d: float, window: int) -> torch.Tensor:
>>> fdiff_coef(1.5, 4)
tensor([ 1.0000, -1.5000, 0.3750, 0.0625], dtype=torch.float64)
"""
return torch.as_tensor(base.fdiff_coef(d, window))
return torch.as_tensor(fdiff_coef_numpy(d, window))


def fdiff(
Expand All @@ -41,9 +41,11 @@ def fdiff(
window: int = 10,
mode: str = "same",
):
"""Return the `n`-th differentiation along the given dimension.
"""Computes the `n`-th differentiation along the given dimension.

This is an extension of `torch.diff` to a fractional order.
This is an extension of
`torch.diff <https://pytorch.org/docs/stable/generated/torch.diff.html>`_
to fractional differentiation.

See :class:`fracdiff.torch.Fracdiff`.

Expand Down
4 changes: 2 additions & 2 deletions fracdiff/torch/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


class Fracdiff(Module):
"""A `torch.nn.Module` to apply fractional differentiation.
"""A ``torch.nn.Module`` to compute fractional differentiation.

Args:
d (float): The order of differentiation.
Expand All @@ -33,7 +33,7 @@ class Fracdiff(Module):
number of additional dimensions.
- output: :math:`(N, *, L_{\\mathrm{out}})`, where :math:`L_{\\mathrm{out}}`
is given by :math:`L_{\\mathrm{in}}` if `mode="same"` and
:math:`L_{\\mathrm{in}} - \\mathrm{window} - 1` if `mode="valid"`.
:math:`L_{\\mathrm{in}} - \\mathrm{window} + 1` if `mode="valid"`.

Examples:

Expand Down
2 changes: 1 addition & 1 deletion tests/test_fdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from numpy.testing import assert_raises

from fracdiff import fdiff
from fracdiff.base import fdiff_coef
from fracdiff.fdiff import fdiff_coef


class TestFdiff:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_tol.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import pytest

from fracdiff.base import fdiff_coef
from fracdiff.fdiff import fdiff_coef
from fracdiff.tol import window_from_tol_coef
from fracdiff.tol import window_from_tol_memory

Expand Down