~L i ,ddlmZddlZGddZy))OptionalNc eZdZdZdZdZddZ ddedee jdee jd e jfd Z dd edee jdee jd e jfd Z d ZdZdZdZy) SobolEnginea The :class:`torch.quasirandom.SobolEngine` is an engine for generating (scrambled) Sobol sequences. Sobol sequences are an example of low discrepancy quasi-random sequences. This implementation of an engine for Sobol sequences is capable of sampling sequences up to a maximum dimension of 21201. It uses direction numbers from https://web.maths.unsw.edu.au/~fkuo/sobol/ obtained using the search criterion D(6) up to the dimension 21201. This is the recommended choice by the authors. References: - Art B. Owen. Scrambling Sobol and Niederreiter-Xing points. Journal of Complexity, 14(4):466-489, December 1998. - I. M. Sobol. The distribution of points in a cube and the accurate evaluation of integrals. Zh. Vychisl. Mat. i Mat. Phys., 7:784-802, 1967. Args: dimension (Int): The dimensionality of the sequence to be drawn scramble (bool, optional): Setting this to ``True`` will produce scrambled Sobol sequences. Scrambling is capable of producing better Sobol sequences. Default: ``False``. seed (Int, optional): This is the seed for the scrambling. The seed of the random number generator is set to this, if specified. Otherwise, it uses a random seed. Default: ``None`` Examples:: >>> # xdoctest: +SKIP("unseeded random state") >>> soboleng = torch.quasirandom.SobolEngine(dimension=5) >>> soboleng.draw(3) tensor([[0.0000, 0.0000, 0.0000, 0.0000, 0.0000], [0.5000, 0.5000, 0.5000, 0.5000, 0.5000], [0.7500, 0.2500, 0.2500, 0.2500, 0.7500]]) iRNc||jkDs|dkrtd|jd||_||_||_t j d}t j||j|t j|_ t j|j|j|js6t j|j|t j|_ n|j|jjt j|_|j d|jzz j#dd|_d |_y) Nz9Supported range of dimensionality for SobolEngine is [1, ]cpu)devicedtype) memory_formatr)MAXDIM ValueErrorseedscramble dimensiontorchr zerosMAXBITlong sobolstate_sobol_engine_initialize_state_shift _scrambleclonecontiguous_formatquasireshape _first_point num_generated)selfrrrr s W/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/quasirandom.py__init__zSobolEngine.__init__3s t{{ "i!m**.++a9     "ll5!++ t{{3ejj  --doot~~N}}T^^CuzzRDJ NN ZZ%%E4K4K%L !ZZ!T[[.8AA!RHnoutr returnc|tj}|jdk(r|dk(r|jj |}ntj |j |dz |j|j|j|\}|_tj|jj ||fd}nNtj |j ||j|j|jdz |\}|_|xj|z c_|"|j|j||S|S)ak Function to draw a sequence of :attr:`n` points from a Sobol sequence. Note that the samples are dependent on the previous samples. The size of the result is :math:`(n, dimension)`. Args: n (Int, optional): The length of sequence of points to draw. Default: 1 out (Tensor, optional): The output tensor dtype (:class:`torch.dtype`, optional): the desired data type of the returned tensor. Default: ``None`` rr)r )dim) rget_default_dtyper"r!to_sobol_engine_drawrrrcat resize_as_copy_)r#r'r(r results r$drawzSobolEngine.drawNs"& =++-E    "Av**--e4%*%=%=JJEOONN&& &" D$5$5$8$8$?#HbQ!&!9!9 ""Q& " FDJ a ? NN6 " ( ( 0J r&mc d|z}|j|z}||dz zdk(s,td|jd|jd|d|d |j||| S) aI Function to draw a sequence of :attr:`2**m` points from a Sobol sequence. Note that the samples are dependent on the previous samples. The size of the result is :math:`(2**m, dimension)`. Args: m (Int): The (base2) exponent of the number of points to draw. out (Tensor, optional): The output tensor dtype (:class:`torch.dtype`, optional): the desired data type of the returned tensor. Default: ``None`` rrrzFThe balance properties of Sobol' points require n to be a power of 2. z0 points have been previously generated, then: n=z+2**=zH. If you still want to do this, please use 'SobolEngine.draw()' instead.)r'r(r )r"rr4)r#r5r(r r'total_ns r$ draw_base2zSobolEngine.draw_base2s$ qD$$q(7Q;'1,))-););(<=1151C1C0DD1WIV00 yy1#Uy33r&c^|jj|jd|_|S)zF Function to reset the ``SobolEngine`` to base state. r)rr2rr")r#s r$resetzSobolEngine.resets' $ r&c`|jdk(rEtj|j|dz |j|j |jnDtj|j||j|j |jdz |xj|z c_|S)a Function to fast-forward the state of the ``SobolEngine`` by :attr:`n` steps. This is equivalent to drawing :attr:`n` samples without using the samples. Args: n (Int): The number of steps to fast-forward by. rr)r"r_sobol_engine_ff_rrr)r#r's r$ fast_forwardzSobolEngine.fast_forwards    "  # # AE4??DNNDDVDV   # # At@R@RUV@V  a r&c d}|j/tj}|j|jtjd}tj d|j |jf||}tj|tjdtjd|j||_ |j |j|jf}tj d|||j}tj|j||j y)Nr r)r generatorr)r )rr Generator manual_seedr randintrrmvpowarangertril_sobol_engine_scramble_r)r#gr shift_intsltm_dimsltms r$rzSobolEngine._scrambles'+ 99 !A MM$)) $ll5!]]  ,SA XX  !U\\!T[[%MN  NNDKK=mmAxqAFFH %%doosDNNKr&cd|jg}|jr|dgz }|j|d|jgz }|jjdzdj |zdzS)Nz dimension=z scramble=Truezseed=(z, ))rrr __class____name__join)r# fmt_strings r$__repr__zSobolEngine.__repr__st"4>>"234 == ?+ +J 99 U499+./ /J~~&&,tyy/DDsJJr&)FN)rNN)NN)rQ __module__ __qualname____doc__rrr%intrrTensorr r4r9r;r>rrTr&r$rrs&PF F:&*'+ 3 3ell #3 $ 3  3p'+'+ 4 4ell #4 $ 4  4<(L,Kr&r)typingrrrrZr&r$r\s RKRKr&