@@ -919,53 +919,53 @@ def kde(data, h, kernel='normal', *, cumulative=False):
919
919
sqrt2pi = sqrt (2 * pi )
920
920
sqrt2 = sqrt (2 )
921
921
K = lambda t : exp (- 1 / 2 * t * t ) / sqrt2pi
922
- I = lambda t : 1 / 2 * (1.0 + erf (t / sqrt2 ))
922
+ W = lambda t : 1 / 2 * (1.0 + erf (t / sqrt2 ))
923
923
support = None
924
924
925
925
case 'logistic' :
926
926
# 1.0 / (exp(t) + 2.0 + exp(-t))
927
927
K = lambda t : 1 / 2 / (1.0 + cosh (t ))
928
- I = lambda t : 1.0 - 1.0 / (exp (t ) + 1.0 )
928
+ W = lambda t : 1.0 - 1.0 / (exp (t ) + 1.0 )
929
929
support = None
930
930
931
931
case 'sigmoid' :
932
932
# (2/pi) / (exp(t) + exp(-t))
933
933
c1 = 1 / pi
934
934
c2 = 2 / pi
935
935
K = lambda t : c1 / cosh (t )
936
- I = lambda t : c2 * atan (exp (t ))
936
+ W = lambda t : c2 * atan (exp (t ))
937
937
support = None
938
938
939
939
case 'rectangular' | 'uniform' :
940
940
K = lambda t : 1 / 2
941
- I = lambda t : 1 / 2 * t + 1 / 2
941
+ W = lambda t : 1 / 2 * t + 1 / 2
942
942
support = 1.0
943
943
944
944
case 'triangular' :
945
945
K = lambda t : 1.0 - abs (t )
946
- I = lambda t : t * t * (1 / 2 if t < 0.0 else - 1 / 2 ) + t + 1 / 2
946
+ W = lambda t : t * t * (1 / 2 if t < 0.0 else - 1 / 2 ) + t + 1 / 2
947
947
support = 1.0
948
948
949
949
case 'parabolic' | 'epanechnikov' :
950
950
K = lambda t : 3 / 4 * (1.0 - t * t )
951
- I = lambda t : - 1 / 4 * t ** 3 + 3 / 4 * t + 1 / 2
951
+ W = lambda t : - 1 / 4 * t ** 3 + 3 / 4 * t + 1 / 2
952
952
support = 1.0
953
953
954
954
case 'quartic' | 'biweight' :
955
955
K = lambda t : 15 / 16 * (1.0 - t * t ) ** 2
956
- I = lambda t : 3 / 16 * t ** 5 - 5 / 8 * t ** 3 + 15 / 16 * t + 1 / 2
956
+ W = lambda t : 3 / 16 * t ** 5 - 5 / 8 * t ** 3 + 15 / 16 * t + 1 / 2
957
957
support = 1.0
958
958
959
959
case 'triweight' :
960
960
K = lambda t : 35 / 32 * (1.0 - t * t ) ** 3
961
- I = lambda t : 35 / 32 * (- 1 / 7 * t ** 7 + 3 / 5 * t ** 5 - t ** 3 + t ) + 1 / 2
961
+ W = lambda t : 35 / 32 * (- 1 / 7 * t ** 7 + 3 / 5 * t ** 5 - t ** 3 + t ) + 1 / 2
962
962
support = 1.0
963
963
964
964
case 'cosine' :
965
965
c1 = pi / 4
966
966
c2 = pi / 2
967
967
K = lambda t : c1 * cos (c2 * t )
968
- I = lambda t : 1 / 2 * sin (c2 * t ) + 1 / 2
968
+ W = lambda t : 1 / 2 * sin (c2 * t ) + 1 / 2
969
969
support = 1.0
970
970
971
971
case _:
@@ -974,27 +974,39 @@ def kde(data, h, kernel='normal', *, cumulative=False):
974
974
if support is None :
975
975
976
976
def pdf (x ):
977
+ n = len (data )
977
978
return sum (K ((x - x_i ) / h ) for x_i in data ) / (n * h )
978
979
979
980
def cdf (x ):
980
- return sum (I ((x - x_i ) / h ) for x_i in data ) / n
981
+
982
+ n = len (data )
983
+ return sum (W ((x - x_i ) / h ) for x_i in data ) / n
984
+
981
985
982
986
else :
983
987
984
988
sample = sorted (data )
985
989
bandwidth = h * support
986
990
987
991
def pdf (x ):
992
+ nonlocal n , sample
993
+ if len (data ) != n :
994
+ sample = sorted (data )
995
+ n = len (data )
988
996
i = bisect_left (sample , x - bandwidth )
989
997
j = bisect_right (sample , x + bandwidth )
990
998
supported = sample [i : j ]
991
999
return sum (K ((x - x_i ) / h ) for x_i in supported ) / (n * h )
992
1000
993
1001
def cdf (x ):
1002
+ nonlocal n , sample
1003
+ if len (data ) != n :
1004
+ sample = sorted (data )
1005
+ n = len (data )
994
1006
i = bisect_left (sample , x - bandwidth )
995
1007
j = bisect_right (sample , x + bandwidth )
996
1008
supported = sample [i : j ]
997
- return sum ((I ((x - x_i ) / h ) for x_i in supported ), i ) / n
1009
+ return sum ((W ((x - x_i ) / h ) for x_i in supported ), i ) / n
998
1010
999
1011
if cumulative :
1000
1012
cdf .__doc__ = f'CDF estimate with { h = !r} and { kernel = !r} '
0 commit comments