1

# distribution should be 't' or 'normal'

Package:
scipy
8546
Exception Class:
ValueError

## Raise code

``````df_denom = np.power(nx * Sx, 2.0) / (nx - 1)
df_denom += np.power(ny * Sy, 2.0) / (ny - 1)
df = df_numer / df_denom
p = distributions.t.cdf(wbfn, df)
elif distribution == "normal":
p = distributions.norm.cdf(wbfn)
else:
raise ValueError(
"distribution should be 't' or 'normal'")

if alternative == "greater":
pass
elif alternative == "less":
p = 1 - p
elif alternative == "two-sided":
``````

## Ways to fix

1

The valid values of distribution are

```{'normal','t'}
```

If any other value outside the above list is given this exception is raised as follows.

```import numpy as np
import scipy.stats.mstats as mstats
X = np.ma.masked_invalid([1, 2, 1, 1, 1, np.nan, 1, 1, 1, 1, 1, 2, 4, 1, 1, np.nan])
Y = np.ma.masked_invalid([3, 3, 4, 3, np.nan, 1, 2, 3, 1, 1, 5, 4])
significant = 14
u1, p1 = mstats.brunnermunzel(X,Y, alternative='less', distribution="norm")
```

```---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-7-76d46b1a7efd> in <module>()  4 Y = np.ma.masked_invalid([3, 3, 4, 3, np.nan, 1, 2, 3, 1, 1, 5, 4])  5 significant = 14 ----> 6 u1, p1 = mstats.brunnermunzel(X,Y, alternative='less', distribution="norm")
/usr/local/lib/python3.7/dist-packages/scipy/stats/mstats_basic.py in brunnermunzel(x, y, alternative, distribution)  2963 else:  2964 raise ValueError( -> 2965 "distribution should be 't' or 'normal'")  2966   2967 if alternative == "greater":
ValueError: distribution should be 't' or 'normal'
```

Fixed:

In the above sample code 'norm' is used instead of the valid value 'normal'. This is the causes the exception which means it is also the fix. We have to use a valid value like so.

```import numpy as np
import scipy.stats.mstats as mstats
X = np.ma.masked_invalid([1, 2, 1, 1, 1, np.nan, 1, 1, 1, 1, 1, 2, 4, 1, 1, np.nan])
Y = np.ma.masked_invalid([3, 3, 4, 3, np.nan, 1, 2, 3, 1, 1, 5, 4])
significant = 14
u1, p1 = mstats.brunnermunzel(X,Y, alternative='less', distribution="normal")
```
Nov 24, 2021
kellemnegasi 18.3k