Questions tagged [weighted]

questions about problems that use a weight function, e.g. weighted mean, weighted sampling

A weight function is a mathematical device used when performing a sum, integral, or average to give some elements more "weight" or influence on the result than other elements in the same set. The result of this application of a weight function is a weighted sum or weighted average.

Weight functions occur frequently in statistics and analysis, and are closely related to the concept of a measure. Weight functions can be employed in both discrete and continuous settings.

615 questions
116
votes
7 answers

Weighted standard deviation in NumPy

numpy.average() has a weights option, but numpy.std() does not. Does anyone have suggestions for a workaround?
YGA
  • 9,546
  • 15
  • 47
  • 50
53
votes
12 answers

Weighted percentile using numpy

Is there a way to use the numpy.percentile function to compute weighted percentile? Or is anyone aware of an alternative python function to compute weighted percentile? thanks!
user308827
  • 21,227
  • 87
  • 254
  • 417
40
votes
6 answers

Calculating weighted mean and standard deviation

I have a time series x_0 ... x_t. I would like to compute the exponentially weighted variance of the data. That is: V = SUM{w_i*(x_i - x_bar)^2, i=1 to T} where SUM{w_i} = 1 and x_bar=SUM{w_i*x_i} ref:…
Alex
  • 19,533
  • 37
  • 126
  • 195
38
votes
6 answers

How can I make a random choice according to probabilities stored in a list (weighted random distribution)?

Given a list of probabilities like: P = [0.10, 0.25, 0.60, 0.05] (I can ensure that the sum of all the variables in P is always 1) How can I write a function that randomly returns a valid index, according to the values in the list? In other words,…
Roughmar
  • 305
  • 1
  • 5
  • 9
22
votes
6 answers

Frequency tables with weighted data in R

I need to calculate the frequency of individuals by age and marital status so normally I'd use: table(age, marital_status) However each individual has a different weight after the sampling of the data. How do I incorporate this into my…
user2568648
  • 3,001
  • 8
  • 35
  • 52
17
votes
6 answers

How can I get a weighted random pick from Python's Counter class?

I have a program where I'm keeping track of the success of various things using collections.Counter — each success of a thing increments the corresponding counter: import collections scoreboard = collections.Counter() if test(thing): …
mattdm
  • 2,082
  • 25
  • 39
15
votes
3 answers

Weighted Pearson's Correlation?

I have a 2396x34 double matrix named y wherein each row (2396) represents a separate situation consisting of 34 consecutive time segments. I also have a numeric[34] named x that represents a single situation of 34 consecutive time segments.…
Mike Furlender
  • 3,869
  • 5
  • 47
  • 75
15
votes
1 answer

Adding a weighted least squares trendline in ggplot2

I am preparing a plot using ggplot2, and I want to add a trendline that is based on a weighted least squares estimation. In base graphics this can be done by sending a WLS model to abline: mod0 <- lm(ds$dMNP~ds$MNP) mod1 <- lm(ds$dMNP~ds$MNP,…
MEcon
  • 395
  • 4
  • 23
15
votes
3 answers

Maximize number of subgraphs with a given minimum weight

I have an undirected planar graph where each node has a weight. I want to split the graph into as many connected disjoint subgraphs as possible (EDIT: or to reach a minimum mean weight of the subgraphs possible), given the condition that each…
Jan Šimbera
  • 457
  • 3
  • 16
14
votes
1 answer

"weighted" regression in R

I have created a script like the one below to do something I called as "weighted" regression: library(plyr) set.seed(100) temp.df <- data.frame(uid=1:200, bp=sample(x=c(100:200),size=200,replace=TRUE), …
lokheart
  • 23,743
  • 39
  • 98
  • 169
13
votes
4 answers

Select a random item from a weighted list

I am trying to write a program to select a random name from the US Census last name list. The list format is Name Weight Cumulative line ----- ----- ----- - SMITH 1.006 1.006 1 JOHNSON 0.810 1.816 …
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
13
votes
3 answers

Plot weighted frequency matrix

This question is related to two different questions I have asked previously: 1) Reproduce frequency matrix plot 2) Add 95% confidence limits to cumulative plot I wish to reproduce this plot in R: I have got this far, using the code beneath the…
Frank Zafka
  • 829
  • 9
  • 30
12
votes
2 answers

More efficient weighted Gini coefficient in Python

Per https://stackoverflow.com/a/48981834/1840471, this is an implementation of the weighted Gini coefficient in Python: import numpy as np def gini(x, weights=None): if weights is None: weights = np.ones_like(x) # Calculate mean…
Max Ghenis
  • 14,783
  • 16
  • 84
  • 132
12
votes
6 answers

Gomoku array-based AI-algorithm?

Way way back (think 20+ years) I encountered a Gomoku game source code in a magazine that I typed in for my computer and had a lot of fun with. The game was difficult to win against, but the core algorithm for the computer AI was really simply and…
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
11
votes
5 answers

Whats the most concise way to pick a random element by weight in c#?

Lets assume: List which element is: public class Element { int Weight { get; set; } } What I want to achieve is, select an element randomly by the weight. For example: Element_1.Weight = 100; Element_2.Weight = 50; Element_3.Weight =…
Eric Yin
  • 8,737
  • 19
  • 77
  • 118
1
2 3
40 41