I`m tring to calculate a weighted average but the formula a little bit different.
The weight calculates in a different way:
When the value of the weight is higher the score is lower, for example:
I want to give a score to my customers, the
Weight (Wi): The period time from the last purchases till today.
Values (Xi): The value is the sale amount
So let's say I have 2 customers and one of them bought 2 months ago and the sale amount is 2000$. The other customer bought 12 months ago and the sale amount is 2000$. so the calculation is:
(2*2000 + 12*2000)/14
But in my case when the weight is higher it means that customer didn`t visit my business for a long time. But formula give to a customer with high number more score.
There is any way to manipulate the formula to calculate the weighted average for my needs ?
Thank you
You can't simply invert the time to get the weight (divide by rather than multiply by), as that would open up divide by zero errors, therefore the weight function has to be something like
w(0) = 1
w(very large) = 0
A simple candidate is w = 1 / (t + 1).
So in your example, that would be ($2000 / (2 + 1) + $2000 / (12 + 1)).
That, of course, is a weighted total - if you want the weighted average, sum all the weights and divide the total by that.
($2000 / 3 + $2000 / 13) / (1/3 + 1/13)
Related
I need your help!
I have to replicate a table in which there are some variables ( gdp growth, labor share, etc)
I have a database that cointains for each country the times series ( 1960-2000) of the base variables.
How can I compute a unique value starting from a time series??
For example:
I need to compute the Capital Share ( K / Y ) of USA
In the database I have the time serie of K and the time serie of Y
If I make K / Y with R , I get a time series as a result, but I need instead a single number ( as in the table that I have to replicate )
Should I make an average of all the observations in K and an average of all the observations in Y an then make the ratio?
Or should I compute the difference from the first observation and the last one for both variables and then make the ratio?
thanks to anyone who will help me
I am working with a problem in R that has the demand for a product that is normally distributed with a mean of a and standard deviation b. It is estimated to increase c% each year. The market share for year 1 is anywhere between d% and e% with each number in between equally likely, and is expected to grow at f% each year.
I know I need to use the rnorm function for the demand
rnorm(1000,a,b) and runif function for the market share runif(1000,1.d,1.e) (adding the 1 in front for easier calculation) year 1 values.
The problem is asking for what the market share and demand will be over 3 years. I know year 1, but I am not sure how I would set the calculation up in R for years 2 and 3 given the growth rates c and f. I currently have something like MarketSizeGrowth<-cumprod(c(runif(1000,1.d,1.e),rep(1.f,2) for the market size but this is definitely wrong.
I have a vector of n scores in an arbitrary order and want to calculate the average absolute difference between any two scores. Note, although I may not be using the correct term with "average absolute difference", I am not asking for the mean/median absolute deviation (i.e., mad), which is analogous to a standard deviation. I want deviations from the scores themselves, not from a measure of central tendency.
First, I will want to calculate each absolute difference between any two scores. With a vector of 2 scores, there is only one absolute difference; with 3 scores, there are 3 absolute differences; with 4 scores, there are 6 absolute differences; a general formula is n*(n-1)/2 absolute differences. For example, say I have a vector:
c(3,4,5,6)
I can calculate each absolute difference manually by taking each score and subtracting it from every other score and then taking the absolute value of the resulting difference. For example:
|3-4| = 1
|3-5| = 2
|3-6| = 3
|4-5| = 1
|4-6| = 2
|5-6| = 1
Second, I will want to take the average of the absolute differences. For example:
(1 + 2 + 3 + 1 + 2 + 1)/6 = 1.666666666666666
Does anyone know of an R function that can do this? Obviously, when the number of scores n within the vector is small, a manual for loop can be created; but I want to apply the function to vectors with 1000s of scores.
Let's say that I have a diminishing value that should be portrayed both on a monthly basis and on a weekly basis.
For example. I know that the value, say 100 000, diminishes by 30 %/year. Which when I calculate (by normal "periodic compound" formulas) is 2.21 %/month and 0.51 %/week.
However, looking at the results from these calculations (calculating for a entire year) I do not get the same end valued. Only if I calculate it as a "interest" (=the percentage is ADDED to the value, NOT taken away) do I get matching values on both the weekly and monthly calculations.
What is the correct formula for calculating this "compound taxation" problem?
I don't know if I fully understand your question.
You cannot calculate diminushing interest the way you do it.
If your value (100 000) diminishes by 30 %/ year this means that at the end of year 1 your value is 70 000.
The way you calculated you compound would work if diminishing by 30% meant 100000/1.3
Your mistake:
You made your calculation this way:
(1+x)^12 - 1 =30% then x=0.0221 the monthly interest is 2.21%
(1+x)^52 - 1 = 30% then x=0.0051 the weekly interest is 0.51%
But what you should have done is:
(1-x)^12=1-30% then x =0.0292 the monthly interest is 2.92%
(1-x)^52=1-30% then x=0.0068 the monthly interest is 0.68 %
You cannot calculate the compound interest as if it was increasing 30% when it's decreasing 30%.
It's easy to understand that the compound interest for an increasing will be smallest than the one for decreasing:
Exemple:
Let's say your investment makes 30% per year.
At the end of first month you will have more money, and therefore you're investing more so you need a smaller return to make as much money as in the first month.
Therefore for increasing interest the coumpond interest i=2.21 is smaller than 30/12 = 2.5
same reasonning for the decreasing i =2.92 > 30/12=2.5
note:
(1+x)^12 - 1 =30% is not equivalent to (1-x)^12=1-30%
negative interest cannot be treated as negative interest:
following what you did adding 10% to one then taking away 10% to the result would return one:
(1+10%)/(1+10%)=1
The way it's calculated won't give the same result : (1+10%)*(1-10%)=0.99
Hope I understood your question and it helps .
Engaging psychic debugging...
diminishes by 30 %/year. Which when I
calculate (by normal "periodic
compound" formulas) is 2.21 %/month
and 0.51 %/week.
You are doing an inappropriate calculation.
You are correct in saying that 30% annual growth is approx 2.21% monthly growth. The reason for this is because 30% annual growth is expressed as multiplication by 1.30 (since 100% + 30% = 130%, or 1.30), and making this monthly is:
1.30 ^ (1/12) = 1.0221 (approx)
However, it does not follow from this that 30% annual shrinkage is approx 2.21% monthly shrinkage. To work out the monthly shrinkage we must note that 30% shrinkage is multiplication by 0.70 (since 100% - 30% = 70%, or 0.70), and make this monthly in the same way:
0.70 ^ (1/12) = 0.9707 (approx)
Multiplication by 0.9707 is monthly shrinkage of 2.929% (approx).
Hopefully this will give you the tools you need to correct your calculations.
I have a stream of data that trends over time. How do I determine the rate of change using C#?
It's been a long time since calculus class, but now is the first time I actually need it (in 15 years). Now when I search for the term 'derivatives' I get financial stuff, and other math things I don't think I really need.
Mind pointing me in the right direction?
If you want something more sophisticated that smooths the data, you should look into a a digital filter algorithm. It's not hard to implement if you can cut through the engineering jargon. The classic method is Savitzky-Golay
If you have the last n samples stored in an array y and each sample is equally spaced in time, then you can calculate the derivative using something like this:
deriv = 0
coefficient = (1,-8,0,8,-1)
N = 5 # points
h = 1 # second
for i range(0,N):
deriv += y[i] * coefficient[i]
deriv /= (12 * h)
This example happens to be a N=5 filter of "3/4 (cubic/quartic)" filter. The bigger N, the more points it is averaging and the smoother it will be, but also the latency will be higher. You'll have to wait N/2 points to get the derivative at time "now".
For more coefficients, look here at the Appendix
https://en.wikipedia.org/wiki/Savitzky%E2%80%93Golay_filter
You need both the data value V and the corresponding time T, at least for the latest data point and the one before that. The rate of change can then be approximated with Eulers backward formula, which translates into
dvdt = (V_now - V_a_moment_ago) / (T_now - T_a_moment_ago);
in C#.
Rate of change is calculated as follows
Calculate a delta such as "price minus - price 20 days ago"
Calculate rate of change such as "delta / price 99 days ago"
Total rate of change, i.e. (new_value - original_value)/time?