Percentage from negative target - percentage

I have these set of targets and actuals:
Actual: "-20" / Target: "-10"
Actual" "50" / Target: "-5"
Actual: "-10" / target: "30"
Target values are anticipated values for each of the 3 categories and actual values are year to date actual values.
On the first category; in was anticipated that there would be -10 sales compared to the previous period. It turned out to be -20 at the end of the current period. The answer could be -100% or -200%. None of these percentages make sense since percentage completed shouldn't be a negative amount. Another reason that makes the percentages unreasonable is that I cannot perceive the difference between 100% and -100% in this case.
On the 2nd category, it was anticipated that there would be 5 less sales in the current period but turns out there was actually 50 sales in the current period. The answer should be +1100% if we agree that every amount of 5 is a 100%.
EDIT: Same as above, the answer for the third category should be -133%
I want to see how much of the target is fulfilled. If actual=target then the answer is 100% although this doesn't make sense if both the actual and the target are negative amount.
If I use (actual/target)*100 negative amounts are always wrong. I need a general formula to calculate the correct answer. I don't mind if the formula has many conditional definitions. How can I do this?

When involving negative amount, you should always know what it is that you are looking for.
example 1:
if you use the absolute value, you should agree that target=10 and actual=-5 is and should be 50%. however, the 'pure' mathematical way to look at it is -50%.
A logical explanation for this is that actual=0 is, as logic predicts, 0%, -5 is even worse! since not only no progress was made, but rather a regression occurred, hence -50% is an understandable result.
example 2:
When both are negative then for target=-10 and actual=-20, since anchoring point is 0, the 'pure' mathematical result is 200% - and is correct (depending on your point of view of course) since you wanted a decrease of 10 and got a decrease of 20.
Note:if you want to define your wanted output differently, do so and we will try and come up with a 'custom' percentage calculation method.
Edit:
What you could try in your case (Although I must say I don't agree with this approach):
if target>0 and actual > 0 : (the usual) :
(actual/target)*100
if target < 0 and actual < 0 : (the usual negative) :
if (target>actual) - actual is worse than expected :
-(actual/target)*100
if (target < actual) - actual is better than expected :
(actual/target)*100
if target>0 and actual < 0 :
((actual-target)/target)*100
corresponds with target=50 , actual = -100 -> result = -300%
if target<0 and actual > 0 :
(abs(target)+actual)/abs(target))*100
so that for target = -50 , but actual = 100 -> result = 300%
I believe that covers your options and suits your needs.
Edit:
A good approach to your issue from my point of view is to look at absolute values (rather than differential values):
Lets say your sales in month A is 200, and you want a 10% increase in month A+1 -> set your target at 220 and then compare the actual to it, you can also compare it month's A actual and overall a report would use the absolute values for comparison, those are always positive, and can be understood more clearly.
now this:
target = -10% , actual +5% and base value of last month 100
will simply be this:
target = 90 actual =105 => Overall performance of 105/90 , or (105/90)-1 higher than expected.

If you want to treat Actual “-50” / Target “50” as 100% fulfilled, you should use the absolute value function in your formula.
| ((actual / target) * 100) |
How you use it in your code depends on the language. In JavaScript, your formula would be like this:
Math.abs((actual / target) * 100)
If this is not how you want your scoring to work, please provide an example of what the score should be when the target or actual is negative.

Based on your edit with more details about what you want, here is some JavaScript that implements that formula:
function percent_difference(target, actual) {
if (target === 0 || actual === 0) {
if (actual > target) {
return 100;
} else if (actual < target) {
return -100;
} else {
return 0;
}
}
var relative_to = Math.min(Math.abs(actual), Math.abs(target));
var distance_from_target_to_actual = actual - target;
var fraction_difference = distance_from_target_to_actual / relative_to;
return 100 * fraction_difference;
}
I tried to avoid unnecessary if statements to keep the code simple.
The function passes these tests:
function test_percent_difference() {
console.log("percent_difference(-10, -20)", percent_difference(-10, -20), "should be", -100);
console.log("percent_difference(-5, 50)", percent_difference(-5, 50), "should be", 1100);
console.log("percent_difference(30, -10)", percent_difference(30, -10), "should be", -400);
console.log("percent_difference(15, 0)", percent_difference(15, 0), "should be", 100);
console.log("percent_difference(0, 0)", percent_difference(0, 0), "should be", 0);
}
You can run it for yourself in your browser in this jsFiddle.

Here is the solution with R.
Assume your data is sample with Target and Actual columns:
sample<-structure(list(Actual = c(-20L, 50L, -10L), Target = c(-10L,
-5L, 30L)), .Names = c("Actual", "Target"), row.names = c(NA,
-3L), class = "data.frame")
sample<-
Actual Target
1 -20 -10
2 50 -5
3 -10 30
#first I compute the percentage deviation as ((Actual-Target)/Actual)*100
#then I will use following two conditions:
# if Actual<0 and Target>0 multiply by -1
#if Actual<0 and Target<0 and if absolute(Actual)>absolute(Target) multiply by -1 else leave as original percent
sample$diff<-with(sample,((Actual-Target)/Actual)*100)
> sample
Actual Target diff
1 -20 -10 50
2 50 -5 110
3 -10 30 400
sample$percent<-with(sample,ifelse((Actual<0 & Target>0),diff*(-1),ifelse((Actual<0 & Target<0),ifelse((abs(Actual)>abs(Target)),diff*-1,diff),diff)))
> sample
Actual Target diff percent
1 -20 -10 50 -50
2 50 -5 110 110
3 -10 30 400 -400
#delete diff column
sample$diff<-NULL
#your final output
> sample
Actual Target percent
1 -20 -10 -50
2 50 -5 110
3 -10 30 -400
Updated:
To match your answers:
sample$diff<-with(sample,((Actual-Target)/Target)*100)
sample$percent<-with(sample,ifelse((Actual<0 & Target>0),diff,ifelse((Actual<0 & Target<0),ifelse((abs(Actual)>abs(Target)),diff*(-1),diff),diff*(-1))))
> sample
Actual Target diff percent
1 -20 -10 100.0000 -100.0000
2 50 -5 -1100.0000 1100.0000
3 -10 30 -133.3333 -133.3333

Related

Gravityform easy calculation - but total is not beginning from 0

I have a pretty simple calculation in gravityform, but because I add +1 and +0.5 to what people are filling out in the input fields, then the total from the begining is showing a number instead of only 0.
Example:
Field A +1
Field B +0.5
Calculation Field A x B x 295. But because I add +1 and +0.5 to what people put in the input field, then the total from the beginning, shows 147,5. Because it calculate 1*.0.5*295 = 147,5.
But I want the total just to show 0 until people are filling out the input field.
How can I avoid this?
Your best bet for a code-based solution will probably be to use the gform_calculation_result filter and check a value of 1 or 0.5 and return 0 instead.
gform.addFilter( 'gform_calculation_result', function( result, formulaField, formId, calcObj ) {
if ( result == 1 || result == 0.5 ) {
result = 0;
}
return result;
} );
An alternate approach would be to use conditional statements right in the calculation formula, powered by our Gravity Forms Advanced Calculations plugin.
You could check if the field has a value and provide a formula for that and return 0 otherwise.
if ( F1 > 0 ):
F1 + 1
else:
0
endif;

how to conduct blackjack simulation with replacement

how would I Write code to simulate 5,000 repetitions of 2 cards being dealt to a player, whereby the cards are drawn with replacement. Using the relative freq of of a blackjack incidence In 5,000 repetitions of two cards being drawn (example; at a casino), provide an estimate of the prob of attaining a blackjack.
Ive tried something like this:
set.seed(5000)
handValue = function(cards) {
value = sum(cards)
# Check for an Ace and change value if it doesn't bust
if (any(cards == 1) && value <= 11)
value = value + 10
# Check bust (set to 0); check Blackjack (set to 21.5)
if(value > 21)
0
else if (value == 21 && length(cards) == 2)
21.5 # Blackjack
else
value
}
But I'm not sure how to exactly simulate with replacement and this code was some rough ideas so it may be well off the mark.
How to simulate drawing 2 cards without replacement
sample(c(1:9,rep(10,4)),2,replace=F)
Edit: with replacement
sample(c(1:9,rep(10,4)),2,replace=T)
Edit2: example with 5 replications
replicate(5,{sample(c(1:9,rep(10,4)),2,replace=T)})

Sum of the digits of a number?

I am a newbie to programming .here I have been solving a simple problem in functional programming (OZ) which is finding the sum of the Digits of a 6 digit positive integer.
Example:- if n = 123456 then
output = 1+2+3+4+5+6 which is 21.
here I found a solution like below
fun {SumDigits6 N}
{SumDigits (N Div 1000) + SumDigits (N mod 1000)}
end
and it says the argument (N Div 1000) gives first 3 digits and the argument (N mod 1000) gives us last 3 digits. and yes I getting the correct solution but my Doubt is how could they giving correct solutions. I mean in given example isn't (N Div 1000) of 123456 gives 123 right not 1+2+3 and similarly (N mod 1000) of123456 gives us 456 not 4+5+6 right ?. in that case, the answer should be 123+456 which is equals to 579 not 21 right ? what Iam missing here.I apologize for asking such simple question but any help would be appreciated.
Thank you :)
You are missing the most important thing here.
It is supposed to happen in a loop and each time the value of N changes.
For example
in the first iteration
the Div gives 1 and mod gives 6 so you add 1 and 6 and store the result and the number is also modified (it becomes 2345)
in the second iteration
the div gives 2 and mod gives 5 you add 2+5+previous result and the number is also modified..
This goes on till the number becomes zero
Your function is recursive, so every time the number get smaller, untill is just 0, then it goes back summing all the partial result. You can do it with an accumulator to store the result, in this simple way:
declare
fun {SumDigit N Accumulator}
if N==0 then Accumulator
else {SumDigit (N div 10) Accumulator+(N mod 10)}
end
end
{Browse {SumDigit 123456 0}}
i think the most elegant way is the function --
static int SumOfDigit(int n)
{
if (n < 10) return n;
return SumOfDigit(SumOfDigit(n/10)+n%10);
}
simple and true :-)
int main()
{
int n,m,d,s=0;
scanf("%d",&n);
m=n;
while(m!=0)
{
d=m%10;
s=s+d;
m=m/10;
}
printf("Sum of digits of %d is %d",n,s);
}

Simple recursive series in matlab

I'm trying to get the sum of the first 120 terms of a series:
a(n) = (10+a(n-1))(1.005)^n
I've tried two approaches, but I don't know which mistakes I'm making.
Approach #1:
nval = 1
mval = zeros(120,1)
for nval=1:120
if nval <= 120 %counting up from 1 to 120
if nval == 1 %modifying first term to avoid 0 index
mval(1) = (10)*(1.005)
nval = nval +1;
else
mval(nval) = (10+mval(nval-1))*(1.005)^nval; %Assigning
nval = nval +1;
end
end
end
which results in a column vector with 10.05 at the top and zeroes for the rest. If if omit the opening definition of mval as zeroes, I get the same result as
Approach #2:
a(1)=10.05; %defining first term to avoid 0 index
for n=2:120, a(n)= (10+a(n-1))*(1.005)^n; end
which results in something far too large.
(The correct value for a(120) is ~1646.99)
Edit: my mistake and apologies - my series was wrong; the (1.005)^n is NOT to the nth power, but merely 1.005. Using this, both methods work.
Thanks to one and all for answers and suggestions

if a<x<b in matlab

I need any help for Matlab's thinking method.Ithink I can explaine my problem with a simple example better. Let's say that I have a characteristic function x=y+x0, x0's are may starting values.Then I want to define my function in a grid.Then I define a finer grid and I want to ask him if he knows where an arbitrary (x*,y*) is.To determine it mathematically I should ask where the corresponding starting point (x0*) is. If this startig point stay between x(i,1)
clear
%%%%%%%%%%&First grid%%%%%%%%%%%%%%%%%%%%
x0=linspace(0,10,6);
y=linspace(0,5,6);
for i=1:length(x0)
for j=1:length(y)
x(i,j)=y(j)+x0(i);
%%%%%%%%%%%%%%%%%%%Second grid%%%%%%%%%%%%%%%%%%
x0fine=linspace(0,10,10);
yfine=linspace(0,5,10);
for p=1:length(x0fine)
for r=1:length(yfine)
xfine(p,r)=yfine(r)+x0fine(p);
if (x(i,1)<xfine(p,1)')&(x0fine(p,1)'<x(i+1,1))%%%%I probabliy have my first mistake %here
% if y(j)<yfine(r)<y(j+1)
% xint(i,j)=(x(i,j)+x(i,j+1)+x(i+1,j)+x(i+1,j+1))./4;
% else
% xint(i,j)= x(i,j);
%end
end
end
end
end
While a < b < c is legal MATLAB syntax, I doubt that it does what you think it does. It does not check that a < b and b < c. What it does is, it checks whether a < b, returning a logical value (maybe an array of logicals) and then, interpreting this logical as 0 or 1, compares it against c:
>> 2 < 0 < 2
ans =
1
>> 2 < 0 < 1
ans =
1
>> 0 < 0 < 1
ans =
1
First in matlab you should avoid as much as possible to do loops.
For instance you can compute x and xfine, with the following code:
x0=linspace(0,10,6);
y=linspace(0,5,6);
x=bsxfun(#plus,x0',y);
x0fine=linspace(0,10,10);
yfine=linspace(0,5,10);
xfine=bsxfun(#plus,x0fine',yfine);
Then given (X*,y*) your want to fine x0*, in your simple example, you can just do: x0*=x*-y*, I think.

Resources