Logic for rate approximation - algorithm

I am looking for some logic to solve the below problem.
There are n transaction amounts : T1,T2,T3.. Tn.
Commission for these transactions are calculated using a rate table provided as below.
if amount between 0 and A1 -> rate is r1
if amount between A1 and A2 -> rate is r2
if amount between A2 and A1 -> rate is r3
...
...
if amount greater than An -> rate is r4
So if T1 < A1 then rate table returns r1 else if r1 < T1 < r2;it returns r2.
So,lets says the rate table results for T1,T2 and T3 are r1,r2 and r3 respectively.
Commission C = T1 * r1 + T2 * r2 + T3 * r3
e.g; if rate table is defined(rates are in %)
0 - 2500 -> 1
2501 - 5000 -> 2
5001 - 10000 -> 4
10000 or more-> 6
If T1 = 6000,T2 = 3000, T3 = 2000, then
C= 6000 * 0.04 + 3000* 0.02 + 2000 * 0.01 = 320
Now my problem is whether we can approximate the commission amount if instead of individual values of T1,T2 and T3 we are provided with T1+T2+T3 (T)
In the above example if T (11000) is applied to the rate tablewe would get 6% and which would result in a commision of 600.
Is there a way to approximate the commission value given T instead of individual values of T1,T2,T3?

Come up with some probabilities for the percentage of transactions that fall into each bracket.
P(r1) = x // The probability of a transaction falling into a rate 1 bracket
P(r2) = y // The probability of a transaction falling into a rate 2 bracket
P(r3) = z // The probability of a transaction falling into a rate 3 bracket
You can therefore estimate the amounts from the transaction that fell into each bracket
Estimated amount of commission at
rate 1: x * (T1 + T2 + T3) * r1
rate 2: y * (T1 + T2 + T3) * r2
rate 3: z * (T1 + T2 + T3) * r3
You can get estimates for x y and z by looking at historic transactions. The amount of money from a transaction you are actually charging at any rate could be less than the requirement for the bracket, but it doesn't matter. On average transactions will accrue the average rate of commission across all bands.
Though I'd like to disclaim, this will give you an estimate but the accuracy of the estimate for a single transaction depends on the standard deviation of your data set, but will on average give you a fair estimate across a large number of transactions. You need to decide if this is sensible in your use case.

Related

Why does coxph() combined with cluster() give much smaller standard errors than other methods to adjust for clustering (e.g. coxme() or frailty()?

I am working on a dataset to test the association between empirical antibiotics (variable emp, the antibiotics are cefuroxime or ceftriaxone compared with a reference antibiotic) and 30-day mortality (variable mort30). The data comes from patients admitted in 6 hospitals (variable site2) with a specific type of infection. Therefore, I would like to adjust for this clustering of patients on hospital level.
First I did this using the coxme() function for mixed models. However, based on visual inspection of the Schoenfeld residuals there were violations of the proportional hazards assumption and I tried adding a time transformation (tt) to the model. Unfortunately, the coxme() does not offer the possibility for time transformations.
Therfore, I tried other options to adjust for the clustering, including coxph() combined with frailty() and cluster. Surprisingly, the standard errors I get using the cluster() option are much smaller than using the coxme() or frailty().
**Does anyone know what is the explanation for this and which option would provide the most reliable estimates?
**
1) Using coxme:
> uni.mort <- coxme(Surv(FUdur30, mort30num) ~ emp + (1 | site2), data = total.pop)
> summary(uni.mort)
Cox mixed-effects model fit by maximum likelihood
Data: total.pop
events, n = 58, 253
Iterations= 24 147
NULL Integrated Fitted
Log-likelihood -313.8427 -307.6543 -305.8967
Chisq df p AIC BIC
Integrated loglik 12.38 3.00 0.0061976 6.38 0.20
Penalized loglik 15.89 3.56 0.0021127 8.77 1.43
Model: Surv(FUdur30, mort30num) ~ emp + (1 | site2)
Fixed coefficients
coef exp(coef) se(coef) z p
empCefuroxime 0.5879058 1.800214 0.6070631 0.97 0.33
empCeftriaxone 1.3422317 3.827576 0.5231278 2.57 0.01
Random effects
Group Variable Std Dev Variance
site2 Intercept 0.2194737 0.0481687
> confint(uni.mort)
2.5 % 97.5 %
empCefuroxime -0.6019160 1.777728
empCeftriaxone 0.3169202 2.367543
2) Using frailty()
uni.mort <- coxph(Surv(FUdur30, mort30num) ~ emp + frailty(site2), data = total.pop)
> summary(uni.mort)
Call:
coxph(formula = Surv(FUdur30, mort30num) ~ emp + frailty(site2),
data = total.pop)
n= 253, number of events= 58
coef se(coef) se2 Chisq DF p
empCefuroxime 0.6302 0.6023 0.6010 1.09 1.0 0.3000
empCeftriaxone 1.3559 0.5221 0.5219 6.75 1.0 0.0094
frailty(site2) 0.40 0.3 0.2900
exp(coef) exp(-coef) lower .95 upper .95
empCefuroxime 1.878 0.5325 0.5768 6.114
empCeftriaxone 3.880 0.2577 1.3947 10.796
Iterations: 7 outer, 27 Newton-Raphson
Variance of random effect= 0.006858179 I-likelihood = -307.8
Degrees of freedom for terms= 2.0 0.3
Concordance= 0.655 (se = 0.035 )
Likelihood ratio test= 12.87 on 2.29 df, p=0.002
3) Using cluster()
uni.mort <- coxph(Surv(FUdur30, mort30num) ~ emp, cluster = site2, data = total.pop)
> summary(uni.mort)
Call:
coxph(formula = Surv(FUdur30, mort30num) ~ emp, data = total.pop,
cluster = site2)
n= 253, number of events= 58
coef exp(coef) se(coef) robust se z Pr(>|z|)
empCefuroxime 0.6405 1.8975 0.6009 0.3041 2.106 0.035209 *
empCeftriaxone 1.3594 3.8937 0.5218 0.3545 3.834 0.000126 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
exp(coef) exp(-coef) lower .95 upper .95
empCefuroxime 1.897 0.5270 1.045 3.444
empCeftriaxone 3.894 0.2568 1.944 7.801
Concordance= 0.608 (se = 0.027 )
Likelihood ratio test= 12.08 on 2 df, p=0.002
Wald test = 15.38 on 2 df, p=5e-04
Score (logrank) test = 10.69 on 2 df, p=0.005, Robust = 5.99 p=0.05
(Note: the likelihood ratio and score tests assume independence of
observations within a cluster, the Wald and robust score tests do not).
>

Calculate contributions changing at set rate toward known future value

I need to solve a financial math problem. I have a revenue goal set based on target company growth rate. Given this total revenue goal for next year, I need to set sales goals each month that have the growth rate (monthly) applied to them. They will total the annual revenue goal. What this looks like is contributions that increase every occurrence by a set rate. Once I determine either the first or last month's goal, I can discount back or find the future values easily.
The problem I have is that I know what these goals need to total, but not what the first or last goal would equal. Hypothetically, I supposed I could use the mean goal (annual goal/12) to give me the goal for the middle of the year and discount back and scale up from June. However, since there is a growth rate, the compounding causes exponential rather than linear growth of the goals. What kind of formula can I use to solve this? Would I treat this as ongoing (but changing) contributions toward an investment with a set future value and growth rate? Or is there some sort of Goal Solver functionality that will help? I am currently doing this in Google Sheets but can switch to Excel or another medium. (I use R heavily, so not afraid of some programmatic methods).
If I cannot figure this out, I will just apply a linear function to it and use the difference in revenue each year as the slope.
Approach:
Let's assume your business starts in Sep-2017, a Month 0, with S units sold.
The constant growth rate, for each next month, was defined in your Business Case as a q, equal to 8% ( 1.08 )
Month 0: S [units], be it 1, 3 or 76,538,112,257
Month 1: S * q
Month 2: S * q * q
Month 3: S * q * q * q
..
Month 11: S * q * q * q * ... * q
>>> S = 1
>>> q = 1.08
>>> [ S * ( q ** i ) for i in range( 12 ) ]
[ 1.0,
1.08,
1.1664,
1.2597120000000002,
1.3604889600000003,
1.4693280768000005,
1.5868743229440005,
1.7138242687795209,
1.8509302102818825,
1.9990046271044333,
2.158924997272788,
2.331638997054611
]
The S units "Scale-free" sum ( independent on the initial amount )
help determine the relation between the target T units sold in total and any S, given q
>>> sum( [ S * ( q**i ) for i in range( 12 ) ] )
18.977126460237237
Here one can see, how inaccurate would be any attempt to use averages and similar guesses to approximate the progress of the powers of q during the period of compounding a constant growth rate ( yielding a T of ~ 19 x the S over 12 months at a given constant rate q of just 8% -- do not hesitate to experiment with other values of q to see the effect sharper and sharper ).
So for an example of a total T of 19,000 units sold during the Year 0, keeping the growth rate of 8% p.m.:
The initial seed for S would be a target T divided by the sum of ( constant growth ) scaling coefficients:
T / sum( [ S * ( q**i ) for i in range( 12 ) ] )
To be on the safer side,
>>> int( 1 + T / sum( [ S * ( q**i ) for i in range( 12 ) ] ) )
1002
>>> sum( [ 1002 * ( q**i ) for i in range( 12 ) ] )
19015.08 ...
>>> [ int( 1002 * ( q**i ) ) for i in range( 12 ) ]
[ 1002,
1082,
1168,
1262,
1363,
1472,
1590,
1717,
1854,
2003,
2163,
2336
]
Month 0: S ~ 1,002 [units]
Month 1: S * q ~ 1,082
Month 2: S * q * q ~ 1,168
Month 3: S * q * q * q ~ 1,262
.. ~ 1,363
. ~ 1,472
~ 1,590
~ 1,717
~ 1,854
. ~ 2,003
.. ~ 2,163
Month 11: S * q * q * q * ... * q ~ 2,336
_____________________________________________________________
19,012 [unit] per Year 0
So Good Luck & Go Get It Sold!

How to calculate execution time (speedup)

I was stuck when trying to calculate for the speedup. So the question given was:
Question 1
If 50% of a program is enhanced by 2 times and the rest 50% is enhanced by 4 times then what is the overall speedup due to the enhancements? Hints: Consider that the execution time of program in the machine before enhancement (without enhancement) is T. Then find the total execution time after the enhancements, T'. The speedup is T/T'.
The only thing I know is speedup = execution time before enhancement/execution time after enhancement. So can I assume the answer is:
Speedup = T/((50/100x1/2) + (50/100x1/4))
Total execution time after the enhancement = T + speedup
(50/100x1/2) because 50% was enhanced by 2 times and same goes to the 4 times.
Question 2
Let us hypothetically imagine that the execution of (2/3)rd of a program could be made to run infinitely fast by some kind of improvement/enhancement in the design of a processor. Then how many times the enhanced processor will run faster compared with the un-enhanced (original) machine?
Can I assume that it is 150 times faster as 100/(2/3) = 150
Any ideas? Thanks in advance.
Let's start with question 1.
The total time is the sum of the times for the two halves:
T = T1 + T2
Then, T1 is enhanced by a factor of two. T2 is improved by a factor of 4:
T' = T1' + T2'
= T1 / 2 + T2 / 4
We know that both T1 and T2 are 50% of T. So:
T' = 0.5 * T / 2 + 0.5 * T / 4
= 1/4 * T + 1/8 * T
= 3/8 * T
The speed-up is
T / T' = T / (3/8 T) = 8/3
Question two can be solved similarly:
T' = T1' + T2'
T1' is reduced to 0. T2 is the remaining 1/3 of T.
T' = 1/3 T
The speed-up is
T / T' = 3
Hence, the program is three times as fast as before (or two times faster).

Chain Matrix Multiplication

Im trying to learn chain matrix multiplication.
Suppose A is a 10 × 30 matrix, B is a 30 × 5 matrix, and C is a 5 × 60 matrix. Then,
How do we get the following number of operations? (Is it number of rows into columns ???)
(AB)C = (10×30×5) + (10×5×60) = 1500 + 3000 = 4500 operations
A(BC) = (30×5×60) + (10×30×60) = 9000 + 18000 = 27000 operations.
http://www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/
The number of operations is the number of multiplications required to calculate the result. A * B will result in a 10 x 5 matrix. Each entry in this matrix is the dotproduct of the respective row of A with the column of B with the same index. Thus: A * B requires calculation of 10 x 5 cells, where each cell is the sum of 30 multiplication, so 10 x 5 x 30. Though this is a rather strange representation.

Scheduling: advance deadline for implicit-deadline rate monotonic algorithm

Given a set of tasks:
T1(20,100) T2(30,250) T3(100,400) (execution time, deadline=peroid)
Now I want to constrict the deadlines as Di = f * Pi where Di is new deadline for ith task, Pi is the original period for ith task and f is the factor I want to figure out. What is the smallest value of f that the tasks will continue to meet their deadlines using rate monotonic scheduler?
This schema will repeat (synchronize) every 2000 time units. During this period
T1 must run 20 times, requiring 400 time units.
T2 must run 8 times, requiring 240 time units.
T3 must run 5 times, requiring 500 time units.
Total is 1140 time units per 2000 time unit interval.
f = 1140 / 2000 = 0.57
This assumes long-running tasks can be interrupted and resumed, to allow shorter-running tasks to run in between. Otherwise there will be no way for T1 to meet it's deadline once T3 has started.
The updated deadlines are:
T1(20,57)
T2(30,142.5)
T3(100,228)
These will repeat every 1851930 time units, and require the same time to complete.
A small simplification: When calculating factor, the period-time cancels out. This means you don't really need to calculate the period to get the factor:
Period = 2000
Required time = (Period / 100) * 20 + (Period / 250) * 30 + (Period / 400) * 100
f = Required time / Period = 20 / 100 + 30 / 250 + 100 / 400 = 0.57
f = Sum(Duration[i] / Period[i])
To calculate the period, you could do this:
Period(T1,T2) = lcm(100, 250) = 500
Period(T1,T2,T3) = lcm(500, 400) = 2000
where lcm(x,y) is the Least Common Multiple.

Resources