Skip to content

Bias of random.integers() with int8 dtype #14774

Closed

Description

@wrwrwr

Output of integers() with dtype=int8 appears to be less uniform that one could expect it to.

import matplotlib.pyplot as plt
import numpy as np

samples = np.random.default_rng().integers(31, size=1111111, dtype=np.int8)
values, counts = np.unique(samples, return_counts=True)
plt.vlines(values, 0, counts)
plt.show()

random-int8

Similar periodicity shows up with various static seeds and MT19937; flipping the use_masked flag in random_bounded_uint8_fill() results in all values having approximately equal frequencies.
Same for 1.17 and 1.18.0.dev0+c10d66a 3.7.3 (default, Oct 7 2019, 12:56:13) [GCC 8.3.0].

Activity

  1. WarrenWeckesser commented on Oct 24, 2019

    @WarrenWeckesser
    Member

    It isn't just the int8 dtype. Biased results are also generated with dtype=np.int16.

    Here's a variation of your script that illustrates a problem when dtype=np.int16:

    import matplotlib.pyplot as plt
    import numpy as np
    
    
    sample_size = 10000000
    maxint = 25000
    
    samples = np.random.default_rng().integers(maxint, size=sample_size, dtype=np.int16)
    
    values, counts = np.unique(samples, return_counts=True)
    
    plt.plot(values, counts, '.', alpha=0.1)
    expected = sample_size / maxint
    plt.axhline(expected, color='r', linewidth=1)
    plt.show()
    

    It generates this plot:
    Figure_1

    A work-around is to not use the dtype argument, and instead use the astype() method to cast the values to the desired data type after they are generated, e.g.:

    samples = np.random.default_rng().integers(31, size=1111111).astype(np.int8)
    
  2. charris commented on Oct 24, 2019

    @charris
    Member

    Looks like a regression. ISTR that the new routines use a different algorithm for generating bounded integers. @bashtage @rkern.

  3. added
    09 - Backport-Candidate
    on Oct 24, 2019
  4. added this to the 1.17.4 release. milestone on Oct 24, 2019
  5. WarrenWeckesser commented on Oct 24, 2019

    @WarrenWeckesser
    Member

    The problem appears to be a bug in the implementation of Lemire's algorithm for random integers in an interval. Once I've confirmed the problem, I'll submit a PR with a fix.

  6. bashtage commented on Oct 24, 2019

    @bashtage
    Contributor

    This is not an issue in randomgen, which suggests something happened in the port over.

  7. bashtage commented on Oct 24, 2019

    @bashtage
    Contributor

    The implementation in randomgen has deviated a bit from what is here since there is more separation between the masked and the lemire generation code.

  8. bashtage commented on Oct 24, 2019

    @bashtage
    Contributor
    import randomgen as rg                                                                                                     
    g = rg.Generator()
    v = g.integers(0, 31, dtype=np.int8,size=1111111)                                                                             
    values, counts = np.unique(v, return_counts=True)                                                                            ounts                                                                                                                        
    import matplotlib.pyplot as plt                                                                                               
    import numpy as np                                                                                                            
    plt.vlines(values, 0, counts)                                                                                                 
    plt.show()                                                                                                                

    image

  9. bashtage commented on Oct 24, 2019

    @bashtage
    Contributor

    Other example

    import matplotlib.pyplot as plt
    import numpy as np
    import randomgen as rg
    sample_size = 10000000
    maxint = 25000
    samples = rg.Generator().integers(maxint, size=sample_size, dtype=np.int16)
    values, counts = np.unique(samples, return_counts=True)
    plt.plot(values, counts, '.', alpha=0.1)
    expected = sample_size / maxint
    plt.axhline(expected, color='r', linewidth=1)
    plt.show()

    produces:

    image

  10. WarrenWeckesser commented on Oct 24, 2019

    @WarrenWeckesser
    Member

    @bashtage, add use_masked=False to your calls to integers. I'm comparing randomgen to what is currently in NumPy, and it looks like the default in randomgen doesn't call the Lemire code.

  11. WarrenWeckesser commented on Oct 24, 2019

    @WarrenWeckesser
    Member

    The bug is this line in the implementation of the Lemire algorithm:

    const uint8_t threshold = -rng_excl % rng_excl;
    

    -rng_excl % rng_excl is not computing the desired mod. In this case, where the type is uint8_t, the result is 0 for any value of rng_excl.

  12. bashtage commented on Oct 24, 2019

    @bashtage
    Contributor

    Yes, I see it now.

    image

    orange is use_masked=False

  13. WarrenWeckesser commented on Oct 25, 2019

    @WarrenWeckesser
    Member

    Proposed fix is in #14777.

  14. added a commit that references this issue on Oct 25, 2019
    f22a525
  15. removed
    09 - Backport-Candidate
    on Nov 12, 2019
  16. removed this from the 1.17.4 release. milestone on Nov 12, 2019
  17. added a commit that references this issue on Dec 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions