Skip to content

Commit

Permalink
Merge pull request #5 from OVVO-Financial/devel-spadim
Browse files Browse the repository at this point in the history
Devel spadim
  • Loading branch information
rspadim authored Dec 18, 2021
2 parents a2bb67b + 91a5808 commit 6283fb8
Show file tree
Hide file tree
Showing 7 changed files with 726 additions and 16 deletions.
676 changes: 676 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion NNS/FSD.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def NNS_FSD(x: pd.Series, y: pd.Series, type_cdf: str = "discrete", use_plot: bo
plt.plot(Combined_sort, LPM_x_sort, label="<Combined Sort> vs <LPM X Sort>")
plt.plot(Combined_sort, LPM_y_sort, label="<Combined Sort> vs <LPM Y Sort>")
plt.legend()
## TODO: plot
# plot(
# Combined_sort,
# LPM_x_sort,
Expand Down
31 changes: 18 additions & 13 deletions NNS/Numerical_Differentiation.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import numpy as np
from typing import Callable
import scipy.optimize
import scipy.linalg


def NNS_diff(
f: Callable,
point: float,
h: float = 0.1,
tol: float = 1e-10,
digits: int = 12, # not in use
print_trace: bool = False,
f: Callable,
point: float,
h: float = 0.1,
tol: float = 1e-10,
digits: int = 12, # not in use
print_trace: bool = False,
) -> dict:
r"""NNS Numerical Differentiation
Expand Down Expand Up @@ -48,7 +50,7 @@ def Finite_step(p: float, h: float) -> dict:
# Bs <- numeric()
# Bl <- numeric()
# Bu <- numeric()
Bs, Bl, Bu = 0, {}, {}
Bs, Bl, Bu = {}, {}, {}
### Step 1 initalize the boundaries for B

### Initial step size h
Expand Down Expand Up @@ -93,11 +95,14 @@ def Finite_step(p: float, h: float) -> dict:
Bu[i] = upper_B
# TODO: understand function(x)
# new.f <- function(x) - f.x + ((f.x - f(point - x)) / x) * point + new.B
new_f = function(x) - f_x + ((f_x - f(point - x)) / x) * point + new_B

### SOLVE FOR h, we just need the negative or positive sign from the tested B
# TODO: scipy root
inferred_h = uniroot(new_f, c(-2 * h, 2 * h), extendInt="yes")["root"]
# inferred_h = uniroot(new_f, c(-2 * h, 2 * h), extendInt="yes")["root"]

inferred_h = scipy.optimize.root_scalar(
f=lambda x: - f_x + ((f_x - f(point - x)) / x) * point + new_B,
# method='bisect',
bracket=[-2 * h, 2 * h],
).root

if print_trace:
print("Iteration", i, "h", inferred_h, "Lower B", lower_B, "Upper B", upper_B)
Expand All @@ -108,7 +113,7 @@ def Finite_step(p: float, h: float) -> dict:
if abs(inferred_h) < tol:
final_B = np.mean([upper_B, lower_B])
# TODO: scipy solver
slope = solve(point, f_x - final_B)
slope = scipy.linalg.solve(point, f_x - final_B)[0]
z = complex(real=point, imag=inferred_h)

# TODO: graph
Expand Down Expand Up @@ -147,7 +152,7 @@ def Finite_step(p: float, h: float) -> dict:
"Value of f(x) at point": f(point),
"Final y-intercept (B)": final_B,
"DERIVATIVE": slope,
"Inferred h": inferred_h,
"Inferred.h": inferred_h,
"iterations": i,
"Averaged Finite Step Initial h": Finite_step(point, h)["next"],
"Inferred h": Finite_step(point, inferred_h),
Expand Down
1 change: 0 additions & 1 deletion NNS/TSD.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def NNS_TSD(x: pd.Series, y: pd.Series, use_plot: bool = True) -> str:
plt.plot(Combined_sort, LPM_x_sort, label="<Combined Sort> vs <LPM X Sort>")
plt.plot(Combined_sort, LPM_y_sort, label="<Combined Sort> vs <LPM Y Sort>")
plt.legend()
## TODO: plot
# plot(
# LPM_x_sort,
# type="l",
Expand Down
11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[build-system]
requires = [
"wheel",
"pandas~=1.1.3",
"numpy~=1.19.2",
"scipy~=1.5.2",
"numba~=0.51.2",
"setuptools~=54.2.0",
"matplotlib~=3.3.2",
]
build-backend = "setuptools.build_meta"
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# Fields marked as "Optional" may be commented out.
setup(
name="NNS",
version="1.0.0",
version="0.1.0",
description="Nonlinear nonparametric statistics using partial moments",
long_description=long_description,
long_description_content_type="text/markdown", # Optional (see note above)
Expand Down Expand Up @@ -69,4 +69,7 @@
extras_require={
"test": ["pytest-cov==2.10.0", "pytest==5.4.3"],
},
url="https://github.com/OVVO-Financial/NNS-Python/",
# https://medium.com/@joel.barmettler/how-to-upload-your-python-package-to-pypi-65edc5fe9c56
download_url="https://github.com/OVVO-Financial/NNS-Python/archive/v_01.tar.gz",
)
17 changes: 17 additions & 0 deletions tests/unit/NNS/test_Numerical_differentiation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,21 @@ class TestNumerical_Differentiation(unittest.TestCase):
COMPARISON_PRECISION = 7

def test_Numerical_Differentiation(self):
# TODO
r = NNS_diff(f=lambda x: x ** 2, point=5, h=0.1, tol=1e-10, digits=12, print_trace=True)
ret_ok = {
"Value of f(x) at point": 2.50e+01,
"Final y-intercept (B)": -2.50e+01,
"DERIVATIVE": 1.00e+01,
"Inferred h": 9.30e-11,
"iterations": 3.10e+01,
"f(x-h)": 9.90e+00,
"f(x+h)": 1.01e+01,
"Averaged Finite Step Initial h": 1.00e+01,
"Inferred h.f(x-h)": 1.00e+01,
"Inferred h.f(x+h)": 1.00e+01,
"Inferred h Averaged Finite Step": 1.00e+01,
"Complex Step Derivative (Initial h)": 1.00e+01,
}
self.assertDictEqual(r, ret_ok)
print(r)

0 comments on commit 6283fb8

Please sign in to comment.