# How to decide weights for calculating weighted mean to penalize extreme values

I want to find a weighted mean of some positive numbers. Weights should be calculated to 'penalize' extreme values. What an extreme value is depends on the other values in the set.

For simplicity, we can think these numbers as exam scores. When a student under/over performs from his/her usual scores, I want to keep these extreme scores, but assign a smaller weight when calculating the weighted mean.

For example, you have these numbers:

1,5,5,6,6,7


I came up with this solution: I took the squared deviations (from the non-weighted mean) and divided them with the sum of squared deviations. I subtracted these from 1 to get weights.

The solution in R code:

d   <- c(1,5,5,6,6,7)
md  <- (d - mean(d))^2
w   <- (1 - md / sum(md))


And the results

> mean(d)
[1] 5
> weighted.mean(d, w)
[1] 5.490909


I try to write what I did in mathematical notation, but please help me out if there is a mistake in the notation:

$$w_i = 1 - \frac{(x_i - \bar{x} )^2 }{\sum_{j=0}^n (x_j - \bar{x})^2}$$

It looks like what I did is working for this example, but I am concerned that it might give weird results for different examples.

Is there an established way to do this task?

If what I did is acceptable, is there a name for this approach? I need to explain what I did and I am not well-versed in mathematical notation/terminology.