Skip to content
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

Documentation updates for 8.2 #82

Merged
merged 22 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions documentation/configuration-utils/_tcp.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,9 @@
"line.tcp.disconnect.on.error": {
"default": "true",
"description": "Disconnect TCP socket that sends malformed messages."
},
"line.tcp.acl.enabled": {
"default": "true",
"description": "Enable or disable Access Control List (ACL) authentication for InfluxDB Line Protocol over TCP. Enterprise only."
}
}
116 changes: 116 additions & 0 deletions documentation/reference/function/finance.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,122 @@ SELECT mid(1.5760, 1.5763)
| :------ |
| 1.57615 |

## regr_intercept

`regr_intercept(y, x)` - Calculates the y-intercept of the linear regression line for the given numeric columns y (dependent variable) and x (independent variable).

- The function requires at least two valid (y, x) pairs to compute the intercept.
- If fewer than two pairs are available, the function returns null.
- Supported data types for x and y include `double`, `float`, and `integer` types.
- The `regr_intercept` function can be used with other statistical aggregation functions like `regr_slope` or `corr`.
- The order of arguments in `regr_intercept(y, x)` matters.
- Ensure that y is the dependent variable and x is the independent variable.

### Calculation

The y-intercept $b_0$ of the regression line $y = b_0 + b_1 x$ is calculated using the formula:

$$
b_0 = \bar{y} - b_1 \bar{x}
$$

Where:
- $\bar{y}$ is the mean of y values
- $\bar{x}$ is the mean of x values
- $b_1$ is the slope calculated by `regr_slope(y, x)`

### Arguments

- y: A numeric column representing the dependent variable.
- x: A numeric column representing the independent variable.

### Return value

Return value type is `double`.

The function returns the y-intercept of the regression line, indicating the predicted value of y when x is 0.

### Examples

#### Calculate the regression intercept between two variables

Using the same measurements table:

| x | y |
| --- | --- |
| 1.0 | 2.0 |
| 2.0 | 3.0 |
| 3.0 | 5.0 |
| 4.0 | 4.0 |
| 5.0 | 6.0 |

Calculate the y-intercept:

```questdb-sql
SELECT regr_intercept(y, x) AS y_intercept FROM measurements;
```
nwoolmer marked this conversation as resolved.
Show resolved Hide resolved

Result:

| y_intercept |
| ----------- |
| 1.4 |

Or: When x is 0, y is predicted to be 1.4 units.

#### Calculate the regression intercept grouped by category

Using the same sales_data table:

| category | advertising_spend | sales |
| -------- | ---------------- | ----- |
| A | 1000 | 15000 |
| A | 2000 | 22000 |
| A | 3000 | 28000 |
| B | 1500 | 18000 |
| B | 2500 | 26000 |
| B | 3500 | 31000 |

```questdb-sql
SELECT category, regr_intercept(sales, advertising_spend) AS base_sales
FROM sales_data
GROUP BY category;
```

Result:

| category | base_sales |
| -------- | ---------- |
| A | 9500 |
| B | 12000 |

Or:

- In category A, with no advertising spend, the predicted base sales are 9,500 units
- In category B, with no advertising spend, the predicted base sales are 12,000 units

#### Handling null values

The function ignores rows where either x or y is null:

```questdb-sql
SELECT regr_intercept(y, x) AS y_intercept
FROM (
SELECT 1 AS x, 2 AS y
UNION ALL SELECT 2, NULL
UNION ALL SELECT NULL, 4
UNION ALL SELECT 4, 5
);
```

Result:

| y_intercept |
| ----------- |
| 1.4 |

Only the rows where both x and y are not null are considered in the calculation.

## regr_slope

`regr_slope(y, x)` - Calculates the slope of the linear regression line for the
Expand Down
Loading