Questions tagged [weighted-average]

The weighted average or weighted mean is similar to an arithmetic mean where instead of each of the data points contributing equally to the final average, some data points contribute more than others.

If all the weights are equal, then the weighted mean is the same as the arithmetic mean.

The mathematical expression for the weighted average is

enter image description here

Read more here

476 questions
4
votes
1 answer

SQL - Calculate Exponential Moving Average with CTEs or Aggregates?

The general formula for EMA: EMA(xn) = α * xn + (1 - α) * EMA(xn-1) Where: xn = PRICE α = 0.5 -- Given 3-day SMA The following recursive CTE does the job: WITH recursive ewma_3 (DATE, PRICE, EMA_3, rn) AS ( -- Anchor -- Feed SMA_3 to…
Ava Barbilla
  • 968
  • 2
  • 18
  • 37
4
votes
2 answers

Pandas/numpy weighted average ZeroDivisionError

Creating a lambda function to calculate weighted average and sending that to a dictionary. wm = lambda x: np.average(x, weights=df.loc[x.index, 'WEIGHTS']) # Define a dictionary with the functions to apply for a given column: f = {'DRESS_AMT':…
babz
  • 469
  • 6
  • 16
4
votes
3 answers

Moving average in Haskell

Given a list of weights: let weights = [0.1, 0.2, 0.4, 0.2, 0.1] and an array of measurements, I want to implement the weighted average. This is how I would do it in Python: y=[] w = length(weights) for n in range(w,len(x)-w): …
Uri Goren
  • 13,386
  • 6
  • 58
  • 110
4
votes
1 answer

Calculating weighted moving average using pandas Rolling method

I calculate simple moving average: def sma(data_frame, length=15): # TODO: Be sure about default values of length. smas = data_frame.Close.rolling(window=length, center=False).mean() return smas Using the rolling function is it possible…
xkcd
  • 2,538
  • 11
  • 59
  • 96
4
votes
1 answer

Standard deviation from center of mass along Numpy array axis

I am trying to find a well-performing way to calculate the standard deviation from the center of mass/gravity along an axis of a Numpy array. In formula this is (sorry for the misalignment): The best I could come up with is this: def…
NichtJens
  • 1,709
  • 19
  • 27
4
votes
2 answers

How would YOU compute IMDB movie rating?

I'm doing this only for learning purposes. I've no intentions of reversing the methods of IMDB. I asked myself I owned IMDB or similar website. How would I compute the movie rating? All I can think of is Weighted Average(which is nothing but…
claws
  • 52,236
  • 58
  • 146
  • 195
4
votes
2 answers

Calculate weighted average life in R

I would like to calculate the weighted average life (WAL) of a loan over time in R. The formula to calculate the WAL is given here. I have the following sample data created in R. Sample data library(data.table) DT<-data.table(date=c(rep(seq(from =…
Dave van Brecht
  • 514
  • 4
  • 16
4
votes
1 answer

Kibana weighted average for gross profit margin

I'm currently implementing Kibana 4 (v4.0.0) as a financial dashboard for our company, order data is originating from ElasticSearch. I'm struggling with the absence of a weighted average metric aggregation for calculation of the gross profit margin…
Bamato
  • 41
  • 5
4
votes
1 answer

Create a weighted mean for a irregular timeseries in pandas

from simulation data with variable timestep I have a irregular time-vector as index for my values, they are stored in a pandas.DataFrame. Let's consider a simplified test case: import pandas as pd import datetime time_vec =…
MichaelA
  • 1,866
  • 2
  • 23
  • 38
4
votes
2 answers

Weighted Average and Ratings

Maths isn't my strong point and I'm at a loss here. Basically, all I need is a simple formula that will give a weighted rating on a scale of 1 to 5. If there are very few votes, they carry less influence and the rating pressess more towards the…
Danten
  • 570
  • 6
  • 17
3
votes
2 answers

Replacing for-loops with apply to improve perfomance (with weighted.mean)

I am a R newbie so hopefully this is a solvable problem for some of you. I have a dataframe containing more than a million data-points. My goal is to compute a weighted mean with an altering starting point. To illustrate consider this frame (…
Ruben
  • 125
  • 1
  • 4
3
votes
3 answers

Data structure/algorithm to efficiently save weighted moving average

I'd like to sum up moving averages for a number of different categories when storing log records. Imagine a service that saves web server logs one entry at a time. Let's further imagine, we don't have access to the logged records. So we see them…
Ortwin Gentz
  • 52,648
  • 24
  • 135
  • 213
3
votes
1 answer

Why does Pytorch EmbeddingBag with mode "max" not accept `per_sample_weights`?

Pytorch's EmbeddingBag allows for efficient lookup + reduce operations on varying length collections of embedding indices. There are 3 modes: "sum", "average" and "max" for the reduce operation. With "sum", you can also provide per_sample_weights…
drevicko
  • 14,382
  • 15
  • 75
  • 97
3
votes
3 answers

Cumulative sum of a pandas column until a maximum value is met, and average adjacent rows

I'm a biology student who is fairly new to python and was hoping someone might be able to help with a problem I have yet to solve With some subsequent code I have created a pandas dataframe that looks like the example below: Distance. No. of…
James
  • 143
  • 1
  • 9
3
votes
1 answer

Compute mean value per pixel using weighted 2d histogram

I am using pyplot.hist2d to plot a 2D histogram (x vs.y) weighted by a third variable, z. Instead of summing z values in a given pixel [x_i,y_i] as done by hist2d, I'd like to obtain the average z of all data points falling in that pixel. Is there a…
Nuanda
  • 75
  • 7
1 2
3
31 32