weighted-average

Calculating weighted moving average using pandas Rolling method

半城伤御伤魂 提交于 2019-12-09 07:03:25
问题 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 to calculate weighted moving average? As I read in the documentation, I think that I have to pass win_type parameter. But I'm not sure which one I have to choose. Here is a definition for weighted moving average. Thanks in advance, 回答1: Yeah, that part of

How to compute weighted sum of all elements in a row in pandas?

走远了吗. 提交于 2019-12-08 16:36:00
问题 I have a pandas data frame with multiple columns. I want to create a new column weighted_sum from the values in the row and another column vector dataframe weight weighted_sum should have the following value: row[weighted_sum] = row[col0]*weight[0] + row[col1]*weight[1] + row[col2]*weight[2] + ... I found the function sum(axis=1) , but it doesn't let me multiply with weight . Edit: I changed things a bit. weight looks like this: 0 col1 0.5 col2 0.3 col3 0.2 df looks like this: col1 col2 col3

Calculate a series of weighted means in R for groups with different weightings

可紊 提交于 2019-12-07 10:27:00
问题 I have the following dataset (simple version of my actual data), 'data', and would like to calculate weighted means for variables x1 and x2, using weightings w1 and w2 respectively, split up into two groups (groups determined by the variable n). data <- data.frame(n = c(1,1,1,2,2,2), x1 = c(4,5,4,7,5,5), x2 = c(7,10,9,NaN,11,12), w1 = c(0,1,1,1,1,1), w2 = c(1,1,1,0,0,1)) I'm trying to do it using with() but get an error when I run this: with(data, aggregate(x = list(x1=x1, x2=x2), by = list(n

Calculate weighted average with pandas dataframe

感情迁移 提交于 2019-12-06 21:36:00
问题 I have te following pandas dataframe: data_df = pd.DataFrame({'ind':['la','p','la','la','p','g','g','la'], 'dist':[10.,5.,7.,8.,7.,2.,5.,3.], 'diff':[0.54,3.2,8.6,7.2,2.1,1.,3.5,4.5], 'cas':[1.,2.,3.,4.,5.,6.,7.,8.]}) that is cas diff dist ind 0 1 0.54 10 la 1 2 3.20 5 p 2 3 8.60 7 la 3 4 7.20 8 la 4 5 2.10 7 p 5 6 1.00 2 g 6 7 3.50 5 g 7 8 4.50 3 la I need to compute the weighted average of all the columns where the weights are in the 'dist' column and group the values by 'ind'. For example

How to add a weighted average summary to a DevExpress XtraGrid?

做~自己de王妃 提交于 2019-12-06 10:53:38
问题 The DevExpress Grid (XtraGrid) allows grids and their groups to have summary calculations. The available options are Count, Max, Min, Avg, Sum, None and Custom. Has anyone got some sample code that shows how to calculate a weighted average column, based upon the weightings provided as values in another column? 回答1: I ended up working this out, and will post my solution here in case others find it useful. If a weighted average consists of both a value and a weight per row, then column that

Joining data with weighted averages and multiple weights in R

戏子无情 提交于 2019-12-06 10:10:19
So I had this question but the scope got a little larger/more complicated. Basically I want to combine two tables and calculate the weighted average for any duplicate IDs. The problem is I will have multiple sets of columns that will need to use different weights. Here's my two datasets (RMS1 and RMS2) and the desired outcome (Joined): RMS1: id,freq1,sev1,count1,freq2,sev2,count2 111 0 2 50 1 2 25 222 1 3 75 2 4 50 RMS2: id,freq1,sev1,count1,freq2,sev2,count2 222 2 4 25 6 6 200 333 4 5 60 3 2 20 Joined: id freq1 sev1 freq2 sev2 111 0 2 1 2 222 1.25* 3.25* 5** 5.5** 333 4 5 3 2 So the * values

Weighted average using numpy.average

南楼画角 提交于 2019-12-06 03:32:18
问题 I have an array: In [37]: bias_2e13 # our array Out[37]: [1.7277990734072355, 1.9718263893212737, 2.469657573252167, 2.869022991373125, 3.314720313010104, 4.232269039271717] The error on each value in the array is: In [38]: bias_error_2e13 # the error on each value Out[38]: array([ 0.13271387, 0.06842465, 0.06937965, 0.23886647, 0.30458249, 0.57906816]) Now I divide the error on each value by 2: In [39]: error_half # error divided by 2 Out[39]: array([ 0.06635694, 0.03421232, 0.03468982, 0

Calculate a series of weighted means in R for groups with different weightings

*爱你&永不变心* 提交于 2019-12-05 15:36:52
I have the following dataset (simple version of my actual data), 'data', and would like to calculate weighted means for variables x1 and x2, using weightings w1 and w2 respectively, split up into two groups (groups determined by the variable n). data <- data.frame(n = c(1,1,1,2,2,2), x1 = c(4,5,4,7,5,5), x2 = c(7,10,9,NaN,11,12), w1 = c(0,1,1,1,1,1), w2 = c(1,1,1,0,0,1)) I'm trying to do it using with() but get an error when I run this: with(data, aggregate(x = list(x1=x1, x2=x2), by = list(n = n), FUN = weighted.mean, w = list(w1 = w1,w2 = w2))) On the otherhand, if weights aren't specified

Taking np.average while ignoring NaN's?

雨燕双飞 提交于 2019-12-05 04:26:04
I have a matrix with shape (64,17) correspond to time & latitude. I want to take a weighted latitude average, which I know np.average can do because, unlike np.nanmean, which I used to average the longitudes, weights can be used in the arguments. However, np.average doesn't ignore NaN like np.nanmean does, so my first 5 entries of each row are included in the latitude averaging and make the entire time series full of NaN. Is there a way I can take a weighted average without the NaN's being included in the calculation? file = Dataset("sst_aso_1951-2014latlon_seasavgs.nc") sst = file.variables[

Calculate weighted average with pandas dataframe

我怕爱的太早我们不能终老 提交于 2019-12-05 02:40:49
I have te following pandas dataframe: data_df = pd.DataFrame({'ind':['la','p','la','la','p','g','g','la'], 'dist':[10.,5.,7.,8.,7.,2.,5.,3.], 'diff':[0.54,3.2,8.6,7.2,2.1,1.,3.5,4.5], 'cas':[1.,2.,3.,4.,5.,6.,7.,8.]}) that is cas diff dist ind 0 1 0.54 10 la 1 2 3.20 5 p 2 3 8.60 7 la 3 4 7.20 8 la 4 5 2.10 7 p 5 6 1.00 2 g 6 7 3.50 5 g 7 8 4.50 3 la I need to compute the weighted average of all the columns where the weights are in the 'dist' column and group the values by 'ind'. For example for 'ind'='la' and the 'diff' column: ((10*0.54)+(8.60*7)+(7.20*8)+(4.50*3))/(10+7+8+3) = 4.882143 The