Skip to content

New file numpy.random.geometric #7343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions content/numpy/concepts/random-module/terms/geometric/geometric.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
Title: '.geometric()'
Description: 'Genereates random samples from the specified geometric distribution.'
Subjects:
- 'Computer Science'
- 'Data Science'
- 'Data Visualization'
Tags:
- 'Data'
- 'Numpy'
- 'Random'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

The **`.geometric()`** function in `numpy.random` returns random samples from a geometric distribution based on a given probability of success, with the option to control the number of samples through the `size` parameter.

## Syntax

```pseudo
numpy.random.geometric(p, size=None)
```

**Parameters:**

- `p` (float or array_like): Probability of success for each trial. Must be in the range (0, 1].
- `size` (int or tuple of ints, optional): Output shape. If `None`, a single value is returned.

**Return value:**

Returns random samples drawn from the geometric distribution, representing the number of trials until the first success.

The probability mass function of geometric distribution is:

$$
f(k) = (1 - p)^{k - 1} \ p
$$

`p` in this case is the success probability of an individual trial.

## Example

The following code returns 10 random samples from a geometric distribution with the probability of success set to 0.35:

```py
import numpy as np

# Setting the seed ensures reproducible results
np.random.seed(42)

results = np.random.geometric(p=0.35, size=10)
print(results)
```

The output of this code would be:

```shell
[2 7 4 3 1 1 1 5 3 3]
```

## Codebyte Example

This Codebyte simulates 100 coin flips with an unfair coin (35% chance of Heads), then counts how many trials resulted in a success on the first attempt:

```codebyte/python
import numpy as np

# Set the seed for reproduceability
np.random.seed(33)

# Simulate 100 trials
results = np.random.geometric(p=0.35, size=100)

# How many trials succeeded on the first attempt?
print((results == 1).sum())
```

Here:

- `p=0.35`: Probability of success (e.g., the coin landing on Heads).
- `size=100`: Generates 100 random samples.
- `np.random.geometric(p, size)`: Returns the number of trials needed to get the first success.

This example demonstrates how geometric distributions can model binary outcome processes like coin tosses where the goal is to measure how many attempts are needed to succeed once.