{"record":{"id":"99fa2ecbd4cd94fc","repo":"jax-ml/jax","slug":"x-and-n-must-be-of-integer-type-got-x-dtype-x-dt","errorCode":null,"errorMessage":"x and n must be of integer type; got x.dtype={x.dtype}, n.dtype={n.dtype}","messagePattern":"x and n must be of integer type; got x\\.dtype=(.+?), n\\.dtype=(.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/stats/multinomial.py","lineNumber":52,"sourceCode":"     f(x, n, p) = n! \\prod_{i=1}^k \\frac{p_i^{x_i}}{x_i!}\n\n  with :math:`n = \\sum_i x_i`.\n\n  Args:\n    x: arraylike, value at which to evaluate the PMF\n    n: arraylike, distribution shape parameter\n    p: arraylike, distribution shape parameter\n\n  Returns:\n    array of logpmf values.\n\n  See Also:\n    :func:`jax.scipy.stats.multinomial.pmf`\n  \"\"\"\n  p, = promote_args_inexact(\"multinomial.logpmf\", p)\n  x, n = promote_args_numeric(\"multinomial.logpmf\", x, n)\n  if not dtypes.issubdtype(x.dtype, np.integer):\n    raise ValueError(f\"x and n must be of integer type; got x.dtype={x.dtype}, n.dtype={n.dtype}\")\n  x = x.astype(p.dtype)\n  n = n.astype(p.dtype)\n  logprobs = gammaln(n + 1) + jnp.sum(xlogy(x, p) - gammaln(x + 1), axis=-1)\n  return jnp.where(jnp.equal(jnp.sum(x), n), logprobs, -np.inf)\n\n\ndef pmf(x: ArrayLike, n: ArrayLike, p: ArrayLike) -> Array:\n  r\"\"\"Multinomial probability mass function.\n\n  JAX implementation of :obj:`scipy.stats.multinomial` ``pmf``.\n\n  The multinomial probability distribution is given by\n\n  .. math::\n\n     f(x, n, p) = n! \\prod_{i=1}^k \\frac{p_i^{x_i}}{x_i!}\n\n  with :math:`n = \\sum_i x_i`.","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/stats/multinomial.py#L34-L70","documentation":"jax.scipy.stats.multinomial.logpmf (and pmf which calls it) requires the count vector x and total count n to have integer dtypes. After promotion via promote_args_numeric, the code checks dtypes.issubdtype(x.dtype, np.integer) and rejects floating-point inputs. This mirrors scipy's requirement that multinomial counts be integers, since non-integer counts are mathematically undefined for a discrete distribution.","triggerScenarios":"Calling jax.scipy.stats.multinomial.logpmf or .pmf with x or n as float arrays, e.g. x=jnp.array([1.0, 2.0]) or n=10.0, or passing Python floats that promote to float32/float64. Also occurs when data loaded from float sources (e.g. CSVs, normalized probabilities) is passed as counts.","commonSituations":"Users coming from continuous distributions, data pipelines that produce float arrays by default, or JAX's x64-disabled mode where integer division produces floats. Version changes that made dtype checking stricter also surface latent float inputs.","solutions":["Cast x and n to an integer dtype before calling: x.astype(jnp.int32), n=int(n) or jnp.asarray(n, dtype=jnp.int32)","Verify your count data actually represents integer counts; if x holds probabilities instead of counts, you're calling the wrong function","Ensure n equals sum(x) along the last axis, otherwise the result is -inf even with correct dtypes"],"exampleFix":"// before\np = jnp.array([0.5, 0.5])\nx = jnp.array([1.0, 1.0])  # float -> raises\nn = 2.0\njax.scipy.stats.multinomial.logpmf(x, n, p)\n\n// after\nx = jnp.array([1, 1])\nn = 2\njax.scipy.stats.multinomial.logpmf(x, n, p)","handlingStrategy":"validation","validationCode":"import numpy as np\nimport jax.numpy as jnp\n\ndef check_multinomial_inputs(x, n):\n    x, n = jnp.asarray(x), jnp.asarray(n)\n    assert np.issubdtype(x.dtype, np.integer), f\"x must be int, got {x.dtype}\"\n    assert np.issubdtype(n.dtype, np.integer), f\"n must be int, got {n.dtype}\"\n    return x, n","typeGuard":"def is_int_array(a) -> bool:\n    return jnp.issubdtype(jnp.asarray(a).dtype, jnp.integer)","tryCatchPattern":"try:\n    lp = multinomial.logpmf(x, n, p)\nexcept ValueError as e:\n    if 'integer type' in str(e):\n        x, n = x.astype(jnp.int32), int(n)\n        lp = multinomial.logpmf(x, n, p)\n    else: raise","preventionTips":["Keep count arrays in int32/int64 from ingestion onward","Add a dtype assert in data-loading tests","Remember sum(x) must equal n or logpmf silently returns -inf"],"tags":["jax","scipy","multinomial","dtype","statistics"],"backgroundTag":"dtype-validation-failed","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}