von_mises.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import math
  2. import torch
  3. import torch.jit
  4. from torch.distributions import constraints
  5. from torch.distributions.distribution import Distribution
  6. from torch.distributions.utils import broadcast_all, lazy_property
  7. def _eval_poly(y, coef):
  8. coef = list(coef)
  9. result = coef.pop()
  10. while coef:
  11. result = coef.pop() + y * result
  12. return result
  13. _I0_COEF_SMALL = [1.0, 3.5156229, 3.0899424, 1.2067492, 0.2659732, 0.360768e-1, 0.45813e-2]
  14. _I0_COEF_LARGE = [0.39894228, 0.1328592e-1, 0.225319e-2, -0.157565e-2, 0.916281e-2,
  15. -0.2057706e-1, 0.2635537e-1, -0.1647633e-1, 0.392377e-2]
  16. _I1_COEF_SMALL = [0.5, 0.87890594, 0.51498869, 0.15084934, 0.2658733e-1, 0.301532e-2, 0.32411e-3]
  17. _I1_COEF_LARGE = [0.39894228, -0.3988024e-1, -0.362018e-2, 0.163801e-2, -0.1031555e-1,
  18. 0.2282967e-1, -0.2895312e-1, 0.1787654e-1, -0.420059e-2]
  19. _COEF_SMALL = [_I0_COEF_SMALL, _I1_COEF_SMALL]
  20. _COEF_LARGE = [_I0_COEF_LARGE, _I1_COEF_LARGE]
  21. def _log_modified_bessel_fn(x, order=0):
  22. """
  23. Returns ``log(I_order(x))`` for ``x > 0``,
  24. where `order` is either 0 or 1.
  25. """
  26. assert order == 0 or order == 1
  27. # compute small solution
  28. y = (x / 3.75)
  29. y = y * y
  30. small = _eval_poly(y, _COEF_SMALL[order])
  31. if order == 1:
  32. small = x.abs() * small
  33. small = small.log()
  34. # compute large solution
  35. y = 3.75 / x
  36. large = x - 0.5 * x.log() + _eval_poly(y, _COEF_LARGE[order]).log()
  37. result = torch.where(x < 3.75, small, large)
  38. return result
  39. @torch.jit.script_if_tracing
  40. def _rejection_sample(loc, concentration, proposal_r, x):
  41. done = torch.zeros(x.shape, dtype=torch.bool, device=loc.device)
  42. while not done.all():
  43. u = torch.rand((3,) + x.shape, dtype=loc.dtype, device=loc.device)
  44. u1, u2, u3 = u.unbind()
  45. z = torch.cos(math.pi * u1)
  46. f = (1 + proposal_r * z) / (proposal_r + z)
  47. c = concentration * (proposal_r - f)
  48. accept = ((c * (2 - c) - u2) > 0) | ((c / u2).log() + 1 - c >= 0)
  49. if accept.any():
  50. x = torch.where(accept, (u3 - 0.5).sign() * f.acos(), x)
  51. done = done | accept
  52. return (x + math.pi + loc) % (2 * math.pi) - math.pi
  53. class VonMises(Distribution):
  54. """
  55. A circular von Mises distribution.
  56. This implementation uses polar coordinates. The ``loc`` and ``value`` args
  57. can be any real number (to facilitate unconstrained optimization), but are
  58. interpreted as angles modulo 2 pi.
  59. Example::
  60. >>> m = dist.VonMises(torch.tensor([1.0]), torch.tensor([1.0]))
  61. >>> m.sample() # von Mises distributed with loc=1 and concentration=1
  62. tensor([1.9777])
  63. :param torch.Tensor loc: an angle in radians.
  64. :param torch.Tensor concentration: concentration parameter
  65. """
  66. arg_constraints = {'loc': constraints.real, 'concentration': constraints.positive}
  67. support = constraints.real
  68. has_rsample = False
  69. def __init__(self, loc, concentration, validate_args=None):
  70. self.loc, self.concentration = broadcast_all(loc, concentration)
  71. batch_shape = self.loc.shape
  72. event_shape = torch.Size()
  73. # Parameters for sampling
  74. tau = 1 + (1 + 4 * self.concentration ** 2).sqrt()
  75. rho = (tau - (2 * tau).sqrt()) / (2 * self.concentration)
  76. self._proposal_r = (1 + rho ** 2) / (2 * rho)
  77. super(VonMises, self).__init__(batch_shape, event_shape, validate_args)
  78. def log_prob(self, value):
  79. if self._validate_args:
  80. self._validate_sample(value)
  81. log_prob = self.concentration * torch.cos(value - self.loc)
  82. log_prob = log_prob - math.log(2 * math.pi) - _log_modified_bessel_fn(self.concentration, order=0)
  83. return log_prob
  84. @torch.no_grad()
  85. def sample(self, sample_shape=torch.Size()):
  86. """
  87. The sampling algorithm for the von Mises distribution is based on the following paper:
  88. Best, D. J., and Nicholas I. Fisher.
  89. "Efficient simulation of the von Mises distribution." Applied Statistics (1979): 152-157.
  90. """
  91. shape = self._extended_shape(sample_shape)
  92. x = torch.empty(shape, dtype=self.loc.dtype, device=self.loc.device)
  93. return _rejection_sample(self.loc, self.concentration, self._proposal_r, x)
  94. def expand(self, batch_shape):
  95. try:
  96. return super(VonMises, self).expand(batch_shape)
  97. except NotImplementedError:
  98. validate_args = self.__dict__.get('_validate_args')
  99. loc = self.loc.expand(batch_shape)
  100. concentration = self.concentration.expand(batch_shape)
  101. return type(self)(loc, concentration, validate_args=validate_args)
  102. @property
  103. def mean(self):
  104. """
  105. The provided mean is the circular one.
  106. """
  107. return self.loc
  108. @property
  109. def mode(self):
  110. return self.loc
  111. @lazy_property
  112. def variance(self):
  113. """
  114. The provided variance is the circular one.
  115. """
  116. return 1 - (_log_modified_bessel_fn(self.concentration, order=1) -
  117. _log_modified_bessel_fn(self.concentration, order=0)).exp()