FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mathjs/src/function/statistics/mad.js at develop · walney/mathjs · GitHub
walney
/
mathjs
Public
forked from
josdejong/mathjs
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
mathjs
/
src
/
function
/
statistics
/
mad.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
63 lines (57 loc) · 1.77 KB
Breadcrumbs
mathjs
/
src
/
function
/
statistics
/
mad.js
Copy path
File metadata and controls
63 lines (57 loc) · 1.77 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
{
flatten
}
from
'../../utils/array.js'
import
{
factory
}
from
'../../utils/factory.js'
import
{
improveErrorMessage
}
from
'./utils/improveErrorMessage.js'
const
name
=
'mad'
const
dependencies
=
[
'typed'
,
'abs'
,
'map'
,
'median'
,
'subtract'
]
export
const
createMad
=
/* #__PURE__ */
factory
(
name
,
dependencies
,
(
{
typed
,
abs
,
map
,
median
,
subtract
}
)
=>
{
/**
* Compute the median absolute deviation of a matrix or a list with values.
* The median absolute deviation is defined as the median of the absolute
* deviations from the median.
*
* Syntax:
*
* math.mad(a, b, c, ...)
* math.mad(A)
*
* Examples:
*
* math.mad(10, 20, 30) // returns 10
* math.mad([1, 2, 3]) // returns 1
* math.mad([[1, 2, 3], [4, 5, 6]]) // returns 1.5
*
* See also:
*
* median, mean, std, abs
*
*
@param
{
Array | Matrix
} array
* A single matrix or multiple scalar values.
*
@return
{
*
} The median absolute deviation.
*/
return
typed
(
name
,
{
// mad([a, b, c, d, ...])
'Array | Matrix'
:
_mad
,
// mad(a, b, c, d, ...)
'...'
:
function
(
args
)
{
return
_mad
(
args
)
}
}
)
function
_mad
(
array
)
{
array
=
flatten
(
array
.
valueOf
(
)
)
if
(
array
.
length
===
0
)
{
throw
new
Error
(
'Cannot calculate median absolute deviation (mad) of an empty array'
)
}
try
{
const
med
=
median
(
array
)
return
median
(
map
(
array
,
function
(
value
)
{
return
abs
(
subtract
(
value
,
med
)
)
}
)
)
}
catch
(
err
)
{
if
(
err
instanceof
TypeError
&&
err
.
message
.
indexOf
(
'median'
)
!==
-
1
)
{
throw
new
TypeError
(
err
.
message
.
replace
(
'median'
,
'mad'
)
)
}
else
{
throw
improveErrorMessage
(
err
,
'mad'
)
}
}
}
}
)
Back
|
FazBrowse Home
|
New Git URL