weighted_average¶
weighted_average(element, weight)
Description¶
Returns the weigthed average elements.
Examples¶
Call or Deploy weighted_average
?
Call weighted_average
directly
The easiest way to use bigfunctions
weighted_average
function is deployed in 39 public datasets for all of the 39 BigQuery regions.- It can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
- (You need to use the dataset in the same region as your datasets otherwise you may have a function not found error)
Public BigFunctions Datasets
Region | Dataset |
---|---|
eu |
bigfunctions.eu |
us |
bigfunctions.us |
europe-west1 |
bigfunctions.europe_west1 |
asia-east1 |
bigfunctions.asia_east1 |
... | ... |
Deploy weighted_average
in your project
Why deploy?
- You may prefer to deploy
weighted_average
in your own project to build and manage your own catalog of functions. - This is particularly useful if you want to create private functions (for example calling your internal APIs).
- Get started by reading the framework page
Deployment
weighted_average
function can be deployed with:
pip install bigfunctions
bigfun get weighted_average
bigfun deploy weighted_average
Calculate average unit price
with sample_data as (
select 10 as grade, 1 as ponderation
union all
select 13 as grade, 2 as ponderation
)
select bigfunctions.eu.weighted_average(grade, ponderation)
from sample_data
with sample_data as (
select 10 as grade, 1 as ponderation
union all
select 13 as grade, 2 as ponderation
)
select bigfunctions.us.weighted_average(grade, ponderation)
from sample_data
with sample_data as (
select 10 as grade, 1 as ponderation
union all
select 13 as grade, 2 as ponderation
)
select bigfunctions.europe_west1.weighted_average(grade, ponderation)
from sample_data
+------------------+
| weighted_average |
+------------------+
| 12 |
+------------------+
Need help or Found a bug using weighted_average
?
Get help using weighted_average
The community can help! Engage the conversation on Slack
We also provide professional suppport.
Report a bug about weighted_average
If the function does not work as expected, please
- report a bug so that it can be improved.
- or open the discussion with the community on Slack.
We also provide professional suppport.
Use cases¶
A teacher wants to calculate the weighted average grade for a student. The student has two grades:
- Quiz: Grade = 10, Weight = 1 (Quizzes are worth less)
- Exam: Grade = 13, Weight = 2 (Exams are worth more)
Using the weighted_average
function, the calculation would be:
SELECT bigfunctions.{region}.weighted_average(grade, weight) AS weighted_average
FROM (
SELECT 10 AS grade, 1 AS weight UNION ALL
SELECT 13 AS grade, 2 AS weight
);
This would return 12, as shown in the example. The exam grade (13) contributes more to the final average because it has a higher weight.
Other use cases:
- Calculating average stock prices: Where
element
is the price of the stock andweight
is the number of shares held at that price. - Determining the weighted average cost of capital: Where
element
is the cost of each type of capital (debt, equity, etc.) andweight
is the proportion of each type of capital in the company's capital structure. - Computing the weighted average of customer satisfaction scores: Where
element
is the satisfaction score andweight
is the number of customers who gave that score. - Creating a composite index from multiple indicators: Where
element
is the value of each indicator andweight
reflects the importance of each indicator in the overall index. For instance, a happiness index could be created weighting factors like GDP per capita, life expectancy, and social support.
In essence, anytime you need an average where some elements contribute more than others, the weighted_average
function is useful.
Spread the word!¶
BigFunctions is fully open-source. Help make it a success by spreading the word!