-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathdpnp_iface_statistics.py
More file actions
1767 lines (1431 loc) · 58.3 KB
/
dpnp_iface_statistics.py
File metadata and controls
1767 lines (1431 loc) · 58.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# *****************************************************************************
# Copyright (c) 2016-2025, Intel Corporation
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# - Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
# *****************************************************************************
"""
Interface of the statistics function of the DPNP
Notes
-----
This module is a face or public interface file for the library
it contains:
- Interface functions
- documentation for the functions
- The functions parameters check
"""
import math
import dpctl.tensor as dpt
import dpctl.tensor._tensor_elementwise_impl as ti
import dpctl.utils as dpu
import numpy
from dpctl.tensor._numpy_helper import normalize_axis_index
import dpnp
# pylint: disable=no-name-in-module
import dpnp.backend.extensions.statistics._statistics_impl as statistics_ext
from dpnp.dpnp_utils.dpnp_utils_common import (
result_type_for_device,
to_supported_dtypes,
)
from .dpnp_utils import get_usm_allocations
from .dpnp_utils.dpnp_utils_reduction import dpnp_wrap_reduction_call
from .dpnp_utils.dpnp_utils_statistics import dpnp_cov, dpnp_median
__all__ = [
"amax",
"amin",
"average",
"corrcoef",
"correlate",
"cov",
"max",
"mean",
"median",
"min",
"ptp",
"std",
"var",
]
def _count_reduce_items(arr, axis, where=True):
"""
Calculates the number of items used in a reduction operation
along the specified axis or axes.
Parameters
----------
arr : {dpnp.ndarray, usm_ndarray}
Input array.
axis : {None, int, tuple of ints}, optional
axis or axes along which the number of items used in a reduction
operation must be counted. If a tuple of unique integers is given,
the items are counted over multiple axes. If ``None``, the variance
is computed over the entire array.
Default: ``None``.
Returns
-------
out : int
The number of items should be used in a reduction operation.
Limitations
-----------
Parameters `where` is only supported with its default value.
"""
if where is True:
# no boolean mask given, calculate items according to axis
if axis is None:
axis = tuple(range(arr.ndim))
elif not isinstance(axis, tuple):
axis = (axis,)
items = 1
for ax in axis:
items *= arr.shape[normalize_axis_index(ax, arr.ndim)]
items = dpnp.intp(items)
else: # pragma: no cover
raise NotImplementedError(
"where keyword argument is only supported with its default value."
)
return items
def _divide_by_scalar(a, v):
"""
Divide input array by a scalar.
The division is implemented through dedicated ``ti._divide_by_scalar``
function which has a better performance comparing to standard ``divide``
function, because there is no need to have internal call of ``asarray``
for the denominator `v` and so it avoids allocating extra device memory.
Parameters
----------
a : {dpnp.ndarray, usm_ndarray}
Input array.
v : scalar
The scalar denominator.
Returns
-------
out : dpnp.ndarray
An array containing the result of division.
"""
usm_a = dpnp.get_usm_ndarray(a)
queue = usm_a.sycl_queue
_manager = dpu.SequentialOrderManager[queue]
dep_evs = _manager.submitted_events
# pylint: disable=protected-access
ht_ev, div_ev = ti._divide_by_scalar(
src=usm_a, scalar=v, dst=usm_a, sycl_queue=queue, depends=dep_evs
)
_manager.add_event_pair(ht_ev, div_ev)
return a
def amax(a, axis=None, out=None, keepdims=False, initial=None, where=True):
"""
Return the maximum of an array or maximum along an axis.
`amax` is an alias of :obj:`dpnp.max`.
See Also
--------
:obj:`dpnp.max` : alias of this function
:obj:`dpnp.ndarray.max` : equivalent method
"""
return max(
a, axis=axis, out=out, keepdims=keepdims, initial=initial, where=where
)
def amin(a, axis=None, out=None, keepdims=False, initial=None, where=True):
"""
Return the minimum of an array or minimum along an axis.
`amin` is an alias of :obj:`dpnp.min`.
See Also
--------
:obj:`dpnp.min` : alias of this function
:obj:`dpnp.ndarray.min` : equivalent method
"""
return min(
a, axis=axis, out=out, keepdims=keepdims, initial=initial, where=where
)
def average(a, axis=None, weights=None, returned=False, *, keepdims=False):
"""
Compute the weighted average along the specified axis.
For full documentation refer to :obj:`numpy.average`.
Parameters
----------
a : {dpnp.ndarray, usm_ndarray}
Input array.
axis : {None, int, tuple of ints}, optional
Axis or axes along which the averages must be computed. If
a tuple of unique integers, the averages are computed over multiple
axes. If ``None``, the average is computed over the entire array.
Default: ``None``.
weights : {array_like}, optional
An array of weights associated with the values in `a`. Each value in
`a` contributes to the average according to its associated weight.
The weights array can either be 1-D (in which case its length must be
the size of `a` along the given axis) or of the same shape as `a`.
If `weights=None`, then all data in `a` are assumed to have a
weight equal to one. The 1-D calculation is::
avg = sum(a * weights) / sum(weights)
The only constraint on `weights` is that `sum(weights)` must not be 0.
Default: ``None``.
returned : {bool}, optional
If ``True``, the tuple (`average`, `sum_of_weights`) is returned,
otherwise only the average is returned. If `weights=None`,
`sum_of_weights` is equivalent to the number of elements over which
the average is taken.
Default: ``False``.
keepdims : {None, bool}, optional
If ``True``, the reduced axes (dimensions) are included in the result
as singleton dimensions, so that the returned array remains
compatible with the input array according to Array Broadcasting
rules. Otherwise, if ``False``, the reduced axes are not included in
the returned array.
Default: ``False``.
Returns
-------
out, [sum_of_weights] : dpnp.ndarray, dpnp.ndarray
Return the average along the specified axis. When `returned` is
``True``, return a tuple with the average as the first element and
the sum of the weights as the second element. `sum_of_weights` is of
the same type as `out`. The result dtype follows a general pattern.
If `weights` is ``None``, the result dtype will be that of `a` , or
default floating point data type for the device where input array `a`
is allocated. Otherwise, if `weights` is not ``None`` and `a` is
non-integral, the result type will be the type of lowest precision
capable of representing values of both `a` and `weights`. If `a`
happens to be integral, the previous rules still applies but the result
dtype will at least be default floating point data type for the device
where input array `a` is allocated.
See Also
--------
:obj:`dpnp.mean` : Compute the arithmetic mean along the specified axis.
:obj:`dpnp.sum` : Sum of array elements over a given axis.
Examples
--------
>>> import dpnp as np
>>> data = np.arange(1, 5)
>>> data
array([1, 2, 3, 4])
>>> np.average(data)
array(2.5)
>>> np.average(np.arange(1, 11), weights=np.arange(10, 0, -1))
array(4.0)
>>> data = np.arange(6).reshape((3, 2))
>>> data
array([[0, 1],
[2, 3],
[4, 5]])
>>> np.average(data, axis=1, weights=[1./4, 3./4])
array([0.75, 2.75, 4.75])
>>> np.average(data, weights=[1./4, 3./4])
TypeError: Axis must be specified when shapes of a and weights differ.
With ``keepdims=True``, the following result has shape (3, 1).
>>> np.average(data, axis=1, keepdims=True)
array([[0.5],
[2.5],
[4.5]])
>>> a = np.ones(5, dtype=np.float64)
>>> w = np.ones(5, dtype=np.complex64)
>>> avg = np.average(a, weights=w)
>>> print(avg.dtype)
complex128
"""
dpnp.check_supported_arrays_type(a)
usm_type, exec_q = get_usm_allocations([a, weights])
if weights is None:
avg = dpnp.mean(a, axis=axis, keepdims=keepdims)
scl = dpnp.asanyarray(
avg.dtype.type(a.size / avg.size),
usm_type=usm_type,
sycl_queue=exec_q,
)
else:
if not dpnp.is_supported_array_type(weights):
weights = dpnp.asarray(
weights, usm_type=usm_type, sycl_queue=exec_q
)
a_dtype = a.dtype
if not dpnp.issubdtype(a_dtype, dpnp.inexact):
default_dtype = dpnp.default_float_type(a.device)
res_dtype = dpnp.result_type(a_dtype, weights.dtype, default_dtype)
else:
res_dtype = dpnp.result_type(a_dtype, weights.dtype)
# Sanity checks
wgt_shape = weights.shape
a_shape = a.shape
if a_shape != wgt_shape:
if axis is None:
raise TypeError(
"Axis must be specified when shapes of input array and "
"weights differ."
)
if weights.ndim != 1:
raise TypeError(
"1D weights expected when shapes of input array and "
"weights differ."
)
if wgt_shape[0] != a_shape[axis]:
raise ValueError(
"Length of weights not compatible with specified axis."
)
# setup weights to broadcast along axis
weights = dpnp.broadcast_to(
weights, (a.ndim - 1) * (1,) + wgt_shape
)
weights = weights.swapaxes(-1, axis)
scl = weights.sum(axis=axis, dtype=res_dtype, keepdims=keepdims)
if dpnp.any(scl == 0.0):
raise ZeroDivisionError("Weights sum to zero, can't be normalized")
avg = dpnp.multiply(a, weights).sum(
axis=axis, dtype=res_dtype, keepdims=keepdims
)
avg /= scl
if returned:
if scl.shape != avg.shape:
scl = dpnp.broadcast_to(scl, avg.shape).copy()
return avg, scl
return avg
def corrcoef(x, y=None, rowvar=True, *, dtype=None):
"""
Return Pearson product-moment correlation coefficients.
For full documentation refer to :obj:`numpy.corrcoef`.
Parameters
----------
x : {dpnp.ndarray, usm_ndarray}
A 1-D or 2-D array containing multiple variables and observations.
Each row of `x` represents a variable, and each column a single
observation of all those variables. Also see `rowvar` below.
y : {None, dpnp.ndarray, usm_ndarray}, optional
An additional set of variables and observations. `y` has the same
shape as `x`.
Default: ``None``.
rowvar : {bool}, optional
If `rowvar` is ``True``, then each row represents a variable,
with observations in the columns. Otherwise, the relationship
is transposed: each column represents a variable, while the rows
contain observations.
Default: ``True``.
dtype : {None, str, dtype object}, optional
Data-type of the result.
Default: ``None``.
Returns
-------
R : {dpnp.ndarray}
The correlation coefficient matrix of the variables.
See Also
--------
:obj:`dpnp.cov` : Covariance matrix.
Examples
--------
In this example we generate two random arrays, ``xarr`` and ``yarr``, and
compute the row-wise and column-wise Pearson correlation coefficients,
``R``. Since `rowvar` is true by default, we first find the row-wise
Pearson correlation coefficients between the variables of ``xarr``.
>>> import dpnp as np
>>> np.random.seed(123)
>>> xarr = np.random.rand(3, 3).astype(np.float32)
>>> xarr
array([[7.2858386e-17, 2.2066992e-02, 3.9520904e-01],
[4.8012391e-01, 5.9377134e-01, 4.5147297e-01],
[9.0728188e-01, 9.9387854e-01, 5.8399546e-01]], dtype=float32)
>>> R1 = np.corrcoef(xarr)
>>> R1
array([[ 0.99999994, -0.6173796 , -0.9685411 ],
[-0.6173796 , 1. , 0.7937219 ],
[-0.9685411 , 0.7937219 , 0.9999999 ]], dtype=float32)
If we add another set of variables and observations ``yarr``, we can
compute the row-wise Pearson correlation coefficients between the
variables in ``xarr`` and ``yarr``.
>>> yarr = np.random.rand(3, 3).astype(np.float32)
>>> yarr
array([[0.17615308, 0.65354985, 0.15716429],
[0.09373496, 0.2123185 , 0.84086883],
[0.9011005 , 0.45206687, 0.00225109]], dtype=float32)
>>> R2 = np.corrcoef(xarr, yarr)
>>> R2
array([[ 0.99999994, -0.6173796 , -0.968541 , -0.48613155, 0.9951523 ,
-0.8900264 ],
[-0.6173796 , 1. , 0.7937219 , 0.9875833 , -0.53702235,
0.19083664],
[-0.968541 , 0.7937219 , 0.9999999 , 0.6883078 , -0.9393724 ,
0.74857277],
[-0.48613152, 0.9875833 , 0.6883078 , 0.9999999 , -0.39783284,
0.0342579 ],
[ 0.9951523 , -0.53702235, -0.9393725 , -0.39783284, 0.99999994,
-0.9305482 ],
[-0.89002645, 0.19083665, 0.7485727 , 0.0342579 , -0.9305482 ,
1. ]], dtype=float32)
Finally if we use the option ``rowvar=False``, the columns are now
being treated as the variables and we will find the column-wise Pearson
correlation coefficients between variables in ``xarr`` and ``yarr``.
>>> R3 = np.corrcoef(xarr, yarr, rowvar=False)
>>> R3
array([[ 1. , 0.9724453 , -0.9909503 , 0.8104691 , -0.46436927,
-0.1643624 ],
[ 0.9724453 , 1. , -0.9949381 , 0.6515728 , -0.6580445 ,
0.07012729],
[-0.99095035, -0.994938 , 1. , -0.72450536, 0.5790461 ,
0.03047091],
[ 0.8104691 , 0.65157276, -0.72450536, 1. , 0.14243561,
-0.71102554],
[-0.4643693 , -0.6580445 , 0.57904613, 0.1424356 , 0.99999994,
-0.79727215],
[-0.1643624 , 0.07012729, 0.03047091, -0.7110255 , -0.7972722 ,
0.99999994]], dtype=float32)
"""
out = dpnp.cov(x, y, rowvar, dtype=dtype)
if out.ndim == 0:
# scalar covariance
# nan if incorrect value (nan, inf, 0), 1 otherwise
return out / out
d = dpnp.diag(out)
stddev = dpnp.sqrt(d.real)
out /= stddev[:, None]
out /= stddev[None, :]
# Clip real and imaginary parts to [-1, 1]. This does not guarantee
# abs(a[i, j]) <= 1 for complex arrays, but is the best we can do without
# excessive work.
dpnp.clip(out.real, -1, 1, out=out.real)
if dpnp.iscomplexobj(out):
dpnp.clip(out.imag, -1, 1, out=out.imag)
return out
def _get_padding(a_size, v_size, mode):
assert v_size <= a_size
if mode == "valid":
l_pad, r_pad = 0, 0
elif mode == "same":
l_pad = v_size // 2
r_pad = v_size - l_pad - 1
elif mode == "full":
l_pad, r_pad = v_size - 1, v_size - 1
else: # pragma: no cover
raise ValueError(
f"Unknown mode: {mode}. Only 'valid', 'same', 'full' are supported."
)
return l_pad, r_pad
def _choose_conv_method(a, v, rdtype):
assert a.size >= v.size
if rdtype == dpnp.bool:
# to avoid accuracy issues
return "direct"
if v.size < 10**4 or a.size < 10**4:
# direct method is faster for small arrays
return "direct"
if dpnp.issubdtype(rdtype, dpnp.integer):
max_a = int(dpnp.max(dpnp.abs(a)))
sum_v = int(dpnp.sum(dpnp.abs(v)))
max_value = int(max_a * sum_v)
default_float = dpnp.default_float_type(a.sycl_device)
if max_value > 2 ** numpy.finfo(default_float).nmant - 1:
# can't represent the result in the default float type
return "direct" # pragma: no covers
if dpnp.issubdtype(rdtype, dpnp.number):
return "fft"
raise ValueError(f"Unsupported dtype: {rdtype}") # pragma: no cover
def _run_native_sliding_dot_product1d(a, v, l_pad, r_pad, rdtype):
queue = a.sycl_queue
device = a.sycl_device
supported_types = statistics_ext.sliding_dot_product1d_dtypes()
supported_dtype = to_supported_dtypes(rdtype, supported_types, device)
if supported_dtype is None: # pragma: no cover
raise ValueError(
f"function does not support input types "
f"({a.dtype.name}, {v.dtype.name}), "
"and the inputs could not be coerced to any "
f"supported types. List of supported types: "
f"{[st.name for st in supported_types]}"
)
a_casted = dpnp.asarray(a, dtype=supported_dtype, order="C")
v_casted = dpnp.asarray(v, dtype=supported_dtype, order="C")
usm_type = dpu.get_coerced_usm_type([a_casted.usm_type, v_casted.usm_type])
out_size = l_pad + r_pad + a_casted.size - v_casted.size + 1
# out type is the same as input type
out = dpnp.empty_like(a_casted, shape=out_size, usm_type=usm_type)
a_usm = dpnp.get_usm_ndarray(a_casted)
v_usm = dpnp.get_usm_ndarray(v_casted)
out_usm = dpnp.get_usm_ndarray(out)
_manager = dpu.SequentialOrderManager[queue]
mem_ev, corr_ev = statistics_ext.sliding_dot_product1d(
a_usm,
v_usm,
out_usm,
l_pad,
r_pad,
depends=_manager.submitted_events,
)
_manager.add_event_pair(mem_ev, corr_ev)
return out
def _convolve_fft(a, v, l_pad, r_pad, rtype):
assert a.size >= v.size
assert l_pad < v.size
# +1 is needed to avoid circular convolution
padded_size = a.size + r_pad + 1
fft_size = 2 ** int(math.ceil(math.log2(padded_size)))
af = dpnp.fft.fft(a, fft_size) # pylint: disable=no-member
vf = dpnp.fft.fft(v, fft_size) # pylint: disable=no-member
r = dpnp.fft.ifft(af * vf) # pylint: disable=no-member
if dpnp.issubdtype(rtype, dpnp.floating):
r = r.real
elif dpnp.issubdtype(rtype, dpnp.integer) or rtype == dpnp.bool:
r = r.real.round()
start = v.size - 1 - l_pad
end = padded_size - 1
return r[start:end]
def correlate(a, v, mode="valid", method="auto"):
r"""
Cross-correlation of two 1-dimensional sequences.
This function computes the correlation as generally defined in signal
processing texts [1]_:
.. math:: c_k = \sum_n a_{n+k} \cdot \overline{v}_n
with `a` and `v` sequences being zero-padded where necessary and
:math:`\overline v` denoting complex conjugation.
For full documentation refer to :obj:`numpy.correlate`.
Parameters
----------
a : {dpnp.ndarray, usm_ndarray}
First input array.
v : {dpnp.ndarray, usm_ndarray}
Second input array.
mode : {"valid", "same", "full"}, optional
Refer to the :obj:`dpnp.convolve` docstring. Note that the default
is ``"valid"``, unlike :obj:`dpnp.convolve`, which uses ``"full"``.
Default: ``"valid"``.
method : {"auto", "direct", "fft"}, optional
Specifies which method to use to calculate the correlation:
- `"direct"` : The correlation is determined directly from sums.
- `"fft"` : The Fourier Transform is used to perform the calculations.
This method is faster for long sequences but can have accuracy issues.
- `"auto"` : Automatically chooses direct or Fourier method based on
an estimate of which is faster.
Note: Use of the FFT convolution on input containing NAN or INF
will lead to the entire output being NAN or INF.
Use method='direct' when your input contains NAN or INF values.
Default: ``"auto"``.
Returns
-------
out : dpnp.ndarray
Discrete cross-correlation of `a` and `v`.
Notes
-----
The definition of correlation above is not unique and sometimes
correlation may be defined differently. Another common definition is [1]_:
.. math:: c'_k = \sum_n a_{n} \cdot \overline{v_{n+k}}
which is related to :math:`c_k` by :math:`c'_k = c_{-k}`.
References
----------
.. [1] Wikipedia, "Cross-correlation",
https://en.wikipedia.org/wiki/Cross-correlation
See Also
--------
:obj:`dpnp.convolve` : Discrete, linear convolution of two one-dimensional
sequences.
Examples
--------
>>> import dpnp as np
>>> a = np.array([1, 2, 3], dtype=np.float32)
>>> v = np.array([0, 1, 0.5], dtype=np.float32)
>>> np.correlate(a, v)
array([3.5], dtype=float32)
>>> np.correlate(a, v, "same")
array([2. , 3.5, 3. ], dtype=float32)
>>> np.correlate([a, v, "full")
array([0.5, 2. , 3.5, 3. , 0. ], dtype=float32)
Using complex sequences:
>>> ac = np.array([1+1j, 2, 3-1j], dtype=np.complex64)
>>> vc = np.array([0, 1, 0.5j], dtype=np.complex64)
>>> np.correlate(ac, vc, 'full')
array([0.5-0.5j, 1. +0.j , 1.5-1.5j, 3. -1.j , 0. +0.j ], dtype=complex64)
Note that you get the time reversed, complex conjugated result
(:math:`\overline{c_{-k}}`) when the two input sequences `a` and `v` change
places:
>>> np.correlate(vc, ac, 'full')
array([0. +0.j , 3. +1.j , 1.5+1.5j, 1. +0.j , 0.5+0.5j], dtype=complex64)
"""
dpnp.check_supported_arrays_type(a, v)
if a.size == 0 or v.size == 0:
raise ValueError(
f"Array arguments cannot be empty. "
f"Received sizes: a.size={a.size}, v.size={v.size}"
)
if a.ndim != 1 or v.ndim != 1:
raise ValueError(
f"Only 1-dimensional arrays are supported. "
f"Received shapes: a.shape={a.shape}, v.shape={v.shape}"
)
supported_methods = ["auto", "direct", "fft"]
if method not in supported_methods:
raise ValueError(
f"Unknown method: {method}. Supported methods: {supported_methods}"
)
device = a.sycl_device
rdtype = result_type_for_device([a.dtype, v.dtype], device)
if dpnp.issubdtype(v.dtype, dpnp.complexfloating):
v = dpnp.conj(v)
revert = False
if v.size > a.size:
revert = True
a, v = v, a
l_pad, r_pad = _get_padding(a.size, v.size, mode)
if method == "auto":
method = _choose_conv_method(a, v, rdtype)
if method == "direct":
r = _run_native_sliding_dot_product1d(a, v, l_pad, r_pad, rdtype)
elif method == "fft":
r = _convolve_fft(a, v[::-1], l_pad, r_pad, rdtype)
else: # pragma: no cover
raise ValueError(f"Unknown method: {method}")
if revert:
r = r[::-1]
return dpnp.asarray(r, dtype=rdtype, order="C")
def cov(
m,
y=None,
rowvar=True,
bias=False,
ddof=None,
fweights=None,
aweights=None,
*,
dtype=None,
):
"""
Estimate a covariance matrix, given data and weights.
For full documentation refer to :obj:`numpy.cov`.
Parameters
----------
m : {dpnp.ndarray, usm_ndarray}
A 1-D or 2-D array containing multiple variables and observations.
Each row of `m` represents a variable, and each column a single
observation of all those variables. Also see `rowvar` below.
y : {None, dpnp.ndarray, usm_ndarray}, optional
An additional set of variables and observations. `y` has the same form
as that of `m`.
Default: ``None``.
rowvar : bool, optional
If `rowvar` is ``True``, then each row represents a variable, with
observations in the columns. Otherwise, the relationship is transposed:
each column represents a variable, while the rows contain observations.
Default: ``True``.
bias : bool, optional
Default normalization is by ``(N - 1)``, where ``N`` is the number of
observations given (unbiased estimate). If `bias` is ``True``, then
normalization is by ``N``. These values can be overridden by using the
keyword `ddof`.
Default: ``False``.
ddof : {None, int}, optional
If not ``None`` the default value implied by `bias` is overridden. Note
that ``ddof=1`` will return the unbiased estimate, even if both
`fweights` and `aweights` are specified, and ``ddof=0`` will return the
simple average. See the notes for the details.
Default: ``None``.
fweights : {None, dpnp.ndarray, usm_ndarray}, optional
1-D array of integer frequency weights; the number of times each
observation vector should be repeated.
It is required that ``fweights >= 0``. However, the function will not
raise an error when ``fweights < 0`` for performance reasons.
Default: ``None``.
aweights : {None, dpnp.ndarray, usm_ndarray}, optional
1-D array of observation vector weights. These relative weights are
typically large for observations considered "important" and smaller for
observations considered less "important". If ``ddof=0`` the array of
weights can be used to assign probabilities to observation vectors.
It is required that ``aweights >= 0``. However, the function will not
error when ``aweights < 0`` for performance reasons.
Default: ``None``.
dtype : {None, str, dtype object}, optional
Data-type of the result. By default, the return data-type will have
the default floating point data-type of the device on which the input
arrays reside.
Default: ``None``.
Returns
-------
out : dpnp.ndarray
The covariance matrix of the variables.
See Also
--------
:obj:`dpnp.corrcoef` : Normalized covariance matrix.
Notes
-----
Assume that the observations are in the columns of the observation array `m`
and let ``f = fweights`` and ``a = aweights`` for brevity. The steps to
compute the weighted covariance are as follows::
>>> import dpnp as np
>>> m = np.arange(10, dtype=np.float32)
>>> f = np.arange(10) * 2
>>> a = np.arange(10) ** 2.0
>>> ddof = 1
>>> w = f * a
>>> v1 = np.sum(w)
>>> v2 = np.sum(w * a)
>>> m -= np.sum(m * w, axis=None, keepdims=True) / v1
>>> cov = np.dot(m * w, m.T) * v1 / (v1**2 - ddof * v2)
Note that when ``a == 1``, the normalization factor
``v1 / (v1**2 - ddof * v2)`` goes over to ``1 / (np.sum(f) - ddof)``
as it should.
Examples
--------
>>> import dpnp as np
>>> x = np.array([[0, 2], [1, 1], [2, 0]]).T
Consider two variables, :math:`x_0` and :math:`x_1`, which correlate
perfectly, but in opposite directions:
>>> x
array([[0, 1, 2],
[2, 1, 0]])
Note how :math:`x_0` increases while :math:`x_1` decreases. The covariance
matrix shows this clearly:
>>> np.cov(x)
array([[ 1., -1.],
[-1., 1.]])
Note that element :math:`C_{0, 1}`, which shows the correlation between
:math:`x_0` and :math:`x_1`, is negative.
Further, note how `x` and `y` are combined:
>>> x = np.array([-2.1, -1, 4.3])
>>> y = np.array([3, 1.1, 0.12])
>>> X = np.stack((x, y), axis=0)
>>> np.cov(X)
array([[11.71 , -4.286 ], # may vary
[-4.286 , 2.14413333]])
>>> np.cov(x, y)
array([[11.71 , -4.286 ], # may vary
[-4.286 , 2.14413333]])
>>> np.cov(x)
array(11.71)
"""
arrays = [m]
if y is not None:
arrays.append(y)
dpnp.check_supported_arrays_type(*arrays)
if m.ndim > 2:
raise ValueError("m has more than 2 dimensions")
if y is not None:
if y.ndim > 2:
raise ValueError("y has more than 2 dimensions")
if ddof is not None:
if not isinstance(ddof, int):
raise ValueError("ddof must be integer")
else:
ddof = 0 if bias else 1
def_float = dpnp.default_float_type(m.sycl_queue)
if dtype is None:
dtype = dpnp.result_type(*arrays, def_float)
if fweights is not None:
dpnp.check_supported_arrays_type(fweights)
if not dpnp.issubdtype(fweights.dtype, numpy.integer):
raise TypeError("fweights must be integer")
if fweights.ndim > 1:
raise ValueError("cannot handle multidimensional fweights")
fweights = dpnp.astype(fweights, def_float)
if aweights is not None:
dpnp.check_supported_arrays_type(aweights)
if aweights.ndim > 1:
raise ValueError("cannot handle multidimensional aweights")
aweights = dpnp.astype(aweights, def_float)
return dpnp_cov(
m,
y=y,
rowvar=rowvar,
ddof=ddof,
dtype=dtype,
fweights=fweights,
aweights=aweights,
)
def max(a, axis=None, out=None, keepdims=False, initial=None, where=True):
"""
Return the maximum of an array or maximum along an axis.
For full documentation refer to :obj:`numpy.max`.
Parameters
----------
a : {dpnp.ndarray, usm_ndarray}
Input array.
axis : {None, int or tuple of ints}, optional
Axis or axes along which to operate. By default, flattened input is
used. If this is a tuple of integers, the minimum is selected over
multiple axes, instead of a single axis or all the axes as before.
Default: ``None``.
out : {None, dpnp.ndarray, usm_ndarray}, optional
Alternative output array in which to place the result. Must be of the
same shape and buffer length as the expected output.
Default: ``None``.
keepdims : {None, bool}, optional
If this is set to ``True``, the axes which are reduced are left in the
result as dimensions with size one. With this option, the result will
broadcast correctly against the input array.
Default: ``False``.
Returns
-------
out : dpnp.ndarray
Maximum of `a`. If `axis` is ``None``, the result is a zero-dimensional
array. If `axis` is an integer, the result is an array of dimension
``a.ndim - 1``. If `axis` is a tuple, the result is an array of
dimension ``a.ndim - len(axis)``.
Limitations
-----------
Parameters `where`, and `initial` are only supported with their default
values. Otherwise ``NotImplementedError`` exception will be raised.
See Also
--------
:obj:`dpnp.min` : Return the minimum of an array.
:obj:`dpnp.maximum` : Element-wise maximum of two arrays, propagates NaNs.
:obj:`dpnp.fmax` : Element-wise maximum of two arrays, ignores NaNs.
:obj:`dpnp.amax` : The maximum value of an array along a given axis,
propagates NaNs.
:obj:`dpnp.nanmax` : The maximum value of an array along a given axis,
ignores NaNs.
Examples
--------
>>> import dpnp as np
>>> a = np.arange(4).reshape((2, 2))
>>> a
array([[0, 1],
[2, 3]])
>>> np.max(a)
array(3)
>>> np.max(a, axis=0) # Maxima along the first axis
array([2, 3])
>>> np.max(a, axis=1) # Maxima along the second axis
array([1, 3])
>>> b = np.arange(5, dtype=float)
>>> b[2] = np.nan
>>> np.max(b)
array(nan)
"""
dpnp.check_limitations(initial=initial, where=where)
usm_a = dpnp.get_usm_ndarray(a)
return dpnp_wrap_reduction_call(
usm_a,