class_docstr: Heteregeneous Intrinsic Dimensionality Algorithm (Hidalgo) model.
  
  Hidalgo is a robust approach in discriminating regions with
  different local intrinsic dimensionality (topological feature
  measuring complexity). Hidalgo offers unsupervised segmentation
  of high-dimensional data.
  
  Parameters
  ----------
  metric : str, or callable, optional, default="euclidean"
      directly passed to sklearn KNearestNeighbors,
      must be str or callable that can be passed to KNearestNeighbors
      distance used in the nearest neighbors part of the algorithm
  K : int, optional, default=2
      number of manifolds used in algorithm
  zeta : float, optional, default=0.8
      "local homogeneity level" used in the algorithm, see equation (4)
  q : int, optional, default=3
      number of points for local Z interaction, "local homogeneity range"
      see equation (4)
  n_iter : int, optional, default=1000
      number of Gibbs sampling iterations
  n_replicas : int, optional, default=1
      number of random starts to run Gibbs sampling
  burn_in : float, optional, default=0.9
      percentage of Gibbs sampling iterations discarded, "burn-in fraction"
  fixed_Z : bool, optional, default=False
      estimate parameters with fixed z (joint posterior approximation via Gibbs)
      z = (z_1, ..., z_K) is a latent variable introduced, where z_i = k
      indicates point i belongs to manifold K
  use_Potts : bool, optional, default=True
      if using local interaction between z, see equation (4)
  estimate_zeta : bool, optional, default=False
      update zeta in the sampling
  sampling_rate: int, optional, default=10
      rate at which to save samples for each n_iter
  a : np.ArrayLike, optional, default=None
      prior parameters of d, the dimensionality of manifold k
  b : np.ArrayLike, optional, default=None
      prior parameters of d, the dimensionality of manifold k
  c : np.ArrayLike, optional, default=None
      prior parameters of p, the probability that point belongs to manifold k
  f : np.ArrayLike, optional, default=None
      parameters of zeta
  seed : int, optional, default = 1
      generate random numbers with seed
  
  References
  ----------
  Allegra, Michele, et al. "Data segmentation based on the local
  intrinsic dimension." Scientific reports 10.1 (2020): 1-12.
  https://www.nature.com/articles/s41598-020-72222-0
  
  
  Examples
  --------
  >>> from sktime.transformations.series.hidalgo import Hidalgo
  >>> import numpy as np
  >>> np.random.seed(123)
  >>> X = np.random.rand(10,3)
  >>> X[:6, 1:] += 10
  >>> X[6:, 1:] = 0
  >>> model = Hidalgo(K=2, burn_in=0.8, n_iter=100, seed=10)
  >>> fitted_model = model.fit(X)
  >>> Z = fitted_model.transform(X)
  >>> Z.tolist()
  [1, 1, 1, 1, 1, 1, 0, 0, 0, 0]
class_docstr_tok: ["Heteregeneous","Intrinsic","Dimensionality","Algorithm","(","Hidalgo",")","model",".","Hidalgo","is","a","robust","approach","in","discriminating","regions","with","different","local","intrinsic","dimensionality","(","topological","feature","measuring","complexity",")",".","Hidalgo","offers","unsupervised","segmentation","of","high-dimensional","data",".","Parameters","--","--","--","--","--","metric",":","str",",","or","callable",",","optional",",","default=","''","euclidean","''","directly","passed","to","sklearn","KNearestNeighbors",",","must","be","str","or","callable","that","can","be","passed","to","KNearestNeighbors","distance","used","in","the","nearest","neighbors","part","of","the","algorithm","K",":","int",",","optional",",","default=2","number","of","manifolds","used","in","algorithm","zeta",":","float",",","optional",",","default=0.8","``","local","homogeneity","level","''","used","in","the","algorithm",",","see","equation","(","4",")","q",":","int",",","optional",",","default=3","number","of","points","for","local","Z","interaction",",","``","local","homogeneity","range","''","see","equation","(","4",")","n_iter",":","int",",","optional",",","default=1000","number","of","Gibbs","sampling","iterations","n_replicas",":","int",",","optional",",","default=1","number","of","random","starts","to","run","Gibbs","sampling","burn_in",":","float",",","optional",",","default=0.9","percentage","of","Gibbs","sampling","iterations","discarded",",","``","burn-in","fraction","''","fixed_Z",":","bool",",","optional",",","default=False","estimate","parameters","with","fixed","z","(","joint","posterior","approximation","via","Gibbs",")","z","=","(","z_1",",","...",",","z_K",")","is","a","latent","variable","introduced",",","where","z_i","=","k","indicates","point","i","belongs","to","manifold","K","use_Potts",":","bool",",","optional",",","default=True","if","using","local","interaction","between","z",",","see","equation","(","4",")","estimate_zeta",":","bool",",","optional",",","default=False","update","zeta","in","the","sampling","sampling_rate",":","int",",","optional",",","default=10","rate","at","which","to","save","samples","for","each","n_iter","a",":","np.ArrayLike",",","optional",",","default=None","prior","parameters","of","d",",","the","dimensionality","of","manifold","k","b",":","np.ArrayLike",",","optional",",","default=None","prior","parameters","of","d",",","the","dimensionality","of","manifold","k","c",":","np.ArrayLike",",","optional",",","default=None","prior","parameters","of","p",",","the","probability","that","point","belongs","to","manifold","k","f",":","np.ArrayLike",",","optional",",","default=None","parameters","of","zeta","seed",":","int",",","optional",",","default","=","1","generate","random","numbers","with","seed","References","--","--","--","--","--","Allegra",",","Michele",",","et","al",".","``","Data","segmentation","based","on","the","local","intrinsic","dimension",".","''","Scientific","reports","10.1","(","2020",")",":","1-12.","https",":","\/\/www.nature.com\/articles\/s41598-020-72222-0","Examples","--","--","--","--",">",">",">","from","sktime.transformations.series.hidalgo","import","Hidalgo",">",">",">","import","numpy","as","np",">",">",">","np.random.seed","(","123",")",">",">",">","X","=","np.random.rand","(","10,3",")",">",">",">","X","[",":6",",","1",":","]","+=","10",">",">",">","X","[","6",":",",","1",":","]","=","0",">",">",">","model","=","Hidalgo","(","K=2",",","burn_in=0.8",",","n_iter=100",",","seed=10",")",">",">",">","fitted_model","=","model.fit","(","X",")",">",">",">","Z","=","fitted_model.transform","(","X",")",">",">",">","Z.tolist","(",")","[","1",",","1",",","1",",","1",",","1",",","1",",","0",",","0",",","0",",","0","]"]
docstr: Gibbs sampling method for joint posterior distribution of target variables.
  
  Notes
  -----
  Target parameters are d, p, Z
  zeta must be computed for the probability distribution of the q-Neighbourhood
  matrix
  
  Parameters
  ----------
  V : 1D np.ndarray of length K
      sum(log(mu_i)) for k in 0:K-1, when mu_i belongs to manifold k
  NN : 1D np.ndarray of length K
      count for k in 0:K-1, when data at index i belongs to manifold k
  a1 : 1D np.ndarray of length K
      prior parameters of d
  b1 : 1D np.ndarray of length K
      prior parameters of d
  c1 : 1D np.ndarray of length K
      prior parameters of p
  Z : 1D np.ndarray of length N
      segmentation based on manifold k
  f1 : 1D np.ndarray of length 2
      parameters of zeta
  N_in : int
      parameters of zeta
  
  Returns
  -------
  sampling : 2D np.ndarray of shape (n_iter, Npar), where Npar = N + 2 * K + 2 + 1
      posterior samples of d, p, Z and likelihood samples, respectively.
docstr_tok: ["Gibbs","sampling","method","for","joint","posterior","distribution","of","target","variables",".","Notes","--","--","-","Target","parameters","are","d",",","p",",","Z","zeta","must","be","computed","for","the","probability","distribution","of","the","q-Neighbourhood","matrix","Parameters","--","--","--","--","--","V",":","1D","np.ndarray","of","length","K","sum","(","log","(","mu_i",")",")","for","k","in","0",":","K-1",",","when","mu_i","belongs","to","manifold","k","NN",":","1D","np.ndarray","of","length","K","count","for","k","in","0",":","K-1",",","when","data","at","index","i","belongs","to","manifold","k","a1",":","1D","np.ndarray","of","length","K","prior","parameters","of","d","b1",":","1D","np.ndarray","of","length","K","prior","parameters","of","d","c1",":","1D","np.ndarray","of","length","K","prior","parameters","of","p","Z",":","1D","np.ndarray","of","length","N","segmentation","based","on","manifold","k","f1",":","1D","np.ndarray","of","length","2","parameters","of","zeta","N_in",":","int","parameters","of","zeta","Returns","--","--","--","-","sampling",":","2D","np.ndarray","of","shape","(","n_iter",",","Npar",")",",","where","Npar","=","N","+","2","*","K","+","2","+","1","posterior","samples","of","d",",","p",",","Z","and","likelihood","samples",",","respectively","."]
code: def _gibbs_sampling(
      self,
      N,
      mu,
      Iin,
      Iout,
      Iout_count,
      Iout_track,
      V,
      NN,
      a1,
      b1,
      c1,
      Z,
      f1,
      N_in,
      _rng,
  ):
      """Gibbs sampling method for joint posterior distribution of target variables.
  
      Notes
      -----
      Target parameters are d, p, Z
      zeta must be computed for the probability distribution of the q-Neighbourhood
      matrix
  
      Parameters
      ----------
      V : 1D np.ndarray of length K
          sum(log(mu_i)) for k in 0:K-1, when mu_i belongs to manifold k
      NN : 1D np.ndarray of length K
          count for k in 0:K-1, when data at index i belongs to manifold k
      a1 : 1D np.ndarray of length K
          prior parameters of d
      b1 : 1D np.ndarray of length K
          prior parameters of d
      c1 : 1D np.ndarray of length K
          prior parameters of p
      Z : 1D np.ndarray of length N
          segmentation based on manifold k
      f1 : 1D np.ndarray of length 2
          parameters of zeta
      N_in : int
          parameters of zeta
  
      Returns
      -------
      sampling : 2D np.ndarray of shape (n_iter, Npar), where Npar = N + 2 * K + 2 + 1
          posterior samples of d, p, Z and likelihood samples, respectively.
      """
      zeta = self.zeta
      q = self.q
      K = self.K
      n_iter = self.n_iter
      fixed_Z = self.fixed_Z
      use_Potts = self.use_Potts
      estimate_zeta = self.estimate_zeta
  
      sampling = np.empty(shape=0)
      pp = (K - 1) / K
      p = np.ones(shape=K) / K
  
      def sample_d(K, a1, b1, _rng):
          """Sample d, the dimension of manifold k."""
          d = np.empty(shape=K)
          for k in range(K):
              stop = False
  
              while stop is False:
                  r1 = _rng.random() * 200  # random sample for d[k]
                  r2 = _rng.random()  # random number for accepting
  
                  rmax = (a1[k] - 1) / b1[k]
  
                  if a1[k] - 1 > 0:
                      assert rmax > 0
                      frac = np.exp(
                          -b1[k] * (r1 - rmax)
                          - (a1[k] - 1) * (np.log(rmax) - np.log(r1))
                      )
                  else:
                      frac = np.exp(-b1[k] * r1)
  
                  if frac > r2:
                      stop = True
                      d[k] = r1
  
          return d
  
      def sample_p(K, p, pp, c1, _rng):
          """Sample p, the prior probability that a point belongs to manifold k."""
          for k in range(K - 1):
              stop = False
  
              while stop is False:
                  r1 = _rng.random()  # random sample for p[k]
                  r2 = _rng.random()  # random number for accepting
  
                  rmax = (c1[k] - 1) / (c1[k] - 1 + c1[K - 1] - 1)
                  frac = ((r1 / rmax) ** (c1[k] - 1)) * (
                      ((1 - r1) / (1 - rmax)) ** (c1[K - 1] - 1)
                  )
  
                  if frac > r2:
                      stop = True
                      r1 = r1 * (1.0 - pp + p[k])
                      p[K - 1] += p[k] - r1
                      pp -= p[k] - r1
                      p[k] = r1
  
          return (p, pp)
  
      def sample_zeta(
          N, K, zeta, use_Potts, estimate_zeta, q, NN, f1, it, _rng
      ):
          """Sample zeta, sample probability neighbour of point from same manifold."""
          stop = False
          maxval = -100000
  
          if use_Potts and estimate_zeta:
              for zeta_candidates in range(10):
                  zeta1 = 0.5 + 0.05 * zeta_candidates
                  ZZ = [
                      partition_function(N, NN[k], zeta1, q)
                      for k in range(K)
                  ]
                  h = [NN[k] * np.log(ZZ[k]) for k in range(K)]
                  val = (
                      (f1[0] - 1) * np.log(zeta1)
                      + (f1[1] - 1) * np.log(1 - zeta1)
                      - h
                  )
  
                  if val > maxval:
                      maxval = val
  
              while stop is False:
                  r1 = _rng.random()  # random sample for zeta
                  r2 = _rng.random()  # random number for accepting
  
                  ZZ = [
                      partition_function(N, NN[k], r1, q)
                      for k in range(K)
                  ]
                  h = [NN[k] * np.log(ZZ[k]) for k in range(K)]
                  val = (
                      (f1[0] - 1) * np.log(r1)
                      + (f1[1] - 1) * np.log(1 - r1)
                      - h
                  )
                  frac = np.exp(val - maxval)
  
                  if frac > r2:
                      stop = True
                      if it > 0:
                          zeta = r1
  
          return zeta
  
      def sample_Z(
          N,
          mu,
          Iin,
          Iout,
          Iout_count,
          Iout_track,
          Z,
          NN,
          a1,
          c1,
          V,
          b1,
          zeta,
          fixed_Z,
          q,
          _rng,
      ):
          """Sample z, latent variable indicating point i belongs to manifold k."""
          if (abs(zeta - 1) < 1e-5) or fixed_Z:
              return Z, NN, a1, c1, V, b1
  
          for i in range(N):
              stop = False
              gg = np.empty(shape=K)
              gmax = 0
  
              for k1 in range(K):
                  g = 0
                  if use_Potts:
                      n_in = sum(
                          [Z[Iin[q * i + j]] == k1 for j in range(q)]
                      )
  
                      m_in = sum(
                          [
                              Iout[Iout_track[i] + j] > -1
                              and Z[Iout[Iout_track[i] + j]] == k1
                              for j in range(Iout_count[i])
                          ]
                      )
  
                      g = (n_in + m_in) * np.log(
                          zeta / (1 - zeta)
                      ) - np.log(partition_function(N, NN[k1], zeta, q))
                      var = partition_function(
                          N, NN[k1] - 1, zeta, q
                      ) / partition_function(N, NN[k1], zeta, q)
                      assert var > 0
                      g = g + np.log(var) * (NN[k1] - 1)
  
                  if g > gmax:
                      gmax = g
                  gg[k1] = g
  
              gg = [np.exp(gg[k1] - gmax) for k1 in range(K)]
  
              prob = p * d * mu[i] ** (-(d + 1)) * gg
              prob /= prob.sum()
  
              while stop is False:
                  r1 = int(
                      np.floor(_rng.random() * K)
                  )  # random sample for Z
                  r2 = _rng.random()  # random number for accepting
  
                  if prob[r1] > r2:
                      stop = True
                      # minus values
                      NN[Z[i]] -= 1
                      a1[Z[i]] -= 1
                      c1[Z[i]] -= 1
                      V[Z[i]] -= np.log(mu[i])
                      b1[Z[i]] -= np.log(mu[i])
                      # change Z, add values
                      Z[i] = r1
                      NN[Z[i]] += 1
                      a1[Z[i]] += 1
                      c1[Z[i]] += 1
                      V[Z[i]] += np.log(mu[i])
                      b1[Z[i]] += np.log(mu[i])
  
          return Z, NN, a1, c1, V, b1
  
      def sample_likelihood(N, mu, p, d, Z, N_in, zeta, NN):
          """Sample likelihood values of mu, and local inhomogeneity penalisation."""
          lik0 = 0
          for i in range(N):
              lik0 = (
                  lik0
                  + np.log(p[Z[i]])
                  + np.log(d[Z[i]])
                  - (d[Z[i]] + 1) * np.log(mu[i])
              )
  
          lik1 = lik0 + np.log(zeta / (1 - zeta)) * N_in
  
          for k1 in range(K):
              lik1 = lik1 - (
                  NN[k1]
                  * np.log(partition_function(N, NN[k1], zeta, q))
              )
  
          return lik0, lik1
  
      for it in range(n_iter):
          d = sample_d(K, a1, b1, _rng)
          sampling = np.append(sampling, d)
  
          (p, pp) = sample_p(K, p, pp, c1, _rng)
          sampling = np.append(sampling, p[: K - 1])
          sampling = np.append(sampling, (1 - pp))
  
          zeta = sample_zeta(
              N, K, zeta, use_Potts, estimate_zeta, q, NN, f1, it, _rng
          )
          sampling = np.append(sampling, zeta)
  
          Z, NN, a1, c1, V, b1 = sample_Z(
              N,
              mu,
              Iin,
              Iout,
              Iout_count,
              Iout_track,
              Z,
              NN,
              a1,
              c1,
              V,
              b1,
              zeta,
              fixed_Z,
              q,
              _rng,
          )
          sampling = np.append(sampling, Z)
  
          N_in, f1 = self._update_zeta_prior(Z, N, Iin)
  
          lik = sample_likelihood(N, mu, p, d, Z, N_in, zeta, NN)
          sampling = np.append(sampling, lik)
  
      return sampling
  
file_path: sktime/sktime/transformations/series/hidalgo.py
imports: from functools import reduce
  import numpy
  from sklearn.neighbors import NearestNeighbors
  from sklearn.utils.validation import check_random_state
  from sktime.transformations.base import BaseTransformer
label_desc: Use image node_id 5 for calling the Hidalgo obj's underlying member method code with example usage: obj._gibbs_sampling(N, mu, Iin, Iout, Iout_count, Iout_track, V, NN, a1, b1, c1, Z, f1, N_in, _rng) and returns: sampling, d, p,  pp, zeta, Z,  NN,  a1,  c1,  V,  b1, lik0,  lik1, Z,  NN,  a1,  c1,  V,  b1