Skip to content

Commit

Permalink
Add normdiff function
Browse files Browse the repository at this point in the history
Fix fidelity function
  • Loading branch information
HauptMachine\vonGostev committed Apr 27, 2021
1 parent 78af798 commit 0fe07fb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion fpdet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ._dcore import P2Q, Q2P, d_matrix, invd_matrix
from ._numpy_core import *

__version__ = "1.0.1"
__version__ = "1.0.2"
33 changes: 27 additions & 6 deletions fpdet/_numpy_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import functools
import numpy as np
from numpy.lib import scimath as sm
from scipy.special import factorial

__all__ = ['compose', 'lrange', 'fact', 'p_convolve', 'moment', 'mean', 'g2',
Expand Down Expand Up @@ -46,7 +47,7 @@ def pack(x): return x if type(x) is tuple else (x,)
lambda acc, f: lambda *y: f(*pack(acc(*pack(y)))), reversed(functions), lambda *x: x)


def lrange(iterable):
def lrange(iterable) -> np.ndarray:
"""
Make np.arange with len identical to given iterable
Expand All @@ -62,7 +63,7 @@ def lrange(iterable):
Returns
-------
numpy.array
numpy.ndarray
arange from 0 to len(iterable).
"""
Expand Down Expand Up @@ -182,13 +183,33 @@ def normalize(p) -> np.ndarray:
return np.array(p) / moment(p, 0)


def normdiff(p, q) -> float:
"""
Alias to the 2-norm of a difference between two vectors
Parameters
----------
p : iterable
q : iterable
Returns
-------
float
Norm discrepancy.
"""
return np.linalg.norm(p - q)


def fidelity(p, q) -> float:
r"""
Fidelity between two distributions.
Fidelity or square of the Bhattacharyya distance between two distributions.
Distributions must have the same length.
Formula modification allows to calculate 'fidelity' between distributions' estimations with negative elements:
Formula modification allows to calculate 'fidelity'
between distributions' estimations with negative elements as
.. math:: F(p, q) = (\sum_ksign(p_kq_k)\sqrt{|p_kq_k|})^2
.. math:: F(p, q) = (|\sum_k \sqrt{|p_kq_k|}|)^2
Parameters
----------
Expand All @@ -206,7 +227,7 @@ def fidelity(p, q) -> float:
raise ValueError(
f'Distributions must have the same length, not {len(p)}, {len(q)}')
prod = np.array(p) * np.array(q)
return sum(np.sign(prod)*np.sqrt(np.abs(prod))) ** 2
return abs(np.sum(sm.sqrt(prod))) ** 2


def entropy(p) -> float:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setuptools.setup(
name="fpdet",
version="1.0.1",
version="1.0.2",
author="Pavel Gostev",
author_email="[email protected]",
description=" Basic analytical functions for a few-photon light detection",
Expand Down

0 comments on commit 0fe07fb

Please sign in to comment.