Calculating the APR - c#

I'm using the Microsoft.VisualBasic Assembly to use rate and Pmt for some reason when i run my code it keeps giving me a completely wrong number
I've thoroughly read the docs and cant seem to identify the issue:
origFee30YearRate = 0.04875M;
double Nper = 360;
double Pmt = Financial.Pmt((double)(origFee30YearRate / 12), Nper, 100000);
double PV = ((Pmt * Nper) * -1) - 1775.25;
var org30APR = (Financial.Rate(Nper, Pmt, PV)) * 12;
The values come out to be:
Nper: 360
Pmt: -529.20822384932933
PV: 188739.71058575856
I'm not sure whats going wrong here with calculating the Rate
The output from Rate is: 0.0006268633835987544 where is should be 5.14

To start your origFee30YearRate appears to be incorrect. 4.75% should be assigned as 0.0475 not 4.75. Also, you appear to be incorrectly changing the sign of the payment.
double origFee30YearRate = 0.0475;
double Nper = 360;
double Pmt = Financial.Pmt(origFee30YearRate / 12, Nper, 100000);
double PV = (100000 - (100000 * origFee30YearRate / 365 * 15) - (100000 * (1 / 100)) - 1775.25);
var org30APR = Financial.Rate(Nper, Pmt, PV) * 12;
This returns an interest rate of 4.92%, presumably you're trying to calculate back to 4.75%?
The reason you don't get 4.75% is because PVal is how much you borrowed and I'm not entirely sure why you're calculating it.
double origFee30YearRate = 0.0475;
double Nper = 360;
double Pmt = Financial.Pmt(origFee30YearRate / 12, Nper, 100000);
var org30APR = Financial.Rate(Nper, Pmt, 100000) * 12;
Also, if you want to change your units back to %, simply multiply by 100.
var org30APR = (Financial.Rate(Nper, Pmt, 100000) * 12) * 100;
For APR, I believe you just need to subtract the fees.
var apr = (Financial.Rate(Nper, Pmt, (100000 - 1775.25)) * 12) * 100;

Related

Continuous Perceptron Weights Change Zero

I am working on 2 class simple perceptron problem. My project work getting user mouse click from GUI panel and make classification. Class 1 expected output: 1 and Class 2 expected output -1. My problem is discrete perceptron working fine but continuous perceptron after one point stop decrease error. I don't know what I am doing wrong. I look so much code and source.
My formulas;
E=1/2 Σ(d-o)^2
f(net)=(2/(1+ⅇ^(-net)))-1
ΔW=n(d-o)(1-o^2)y
like this.
d: Expected output,
net: weight*input sum,
y: input matrix ([x1 x2 -1]) and
o: Actual output.
Code for continuous perceptron below;
while (totalError > Emax)
{
totalError = 0;
for(i=0; i<point.Count; i++)
{
double x1 = point[i].X1;
double x2 = point[i].X2;
double net = (x1 * w0) + (x2 * w1) + (x0 * w2);
double o = (2 / (1 + Math.Exp(-net))) - 1;
double error = Math.Pow(point[i].Class - o, 2);
w0 += (x1 * c * (point[i].Class - o) * (1 - Math.Pow(o, 2))) / 2;
w1 += (x2 * c * (point[i].Class - o) * (1 - Math.Pow(o, 2))) / 2;
w2 += (x0 * c * (point[i].Class - o) * (1 - Math.Pow(o, 2))) / 2;
totalError += error;
}
totalError = totalError / 2;
ErrorShow(cycle, totalError);
objGraphic.Clear(Color.White);
DrawSeperationLine();
cycle++;
}
Emax=0.001 selected. Project working like this. You can see it not correct line location. Class 1 is blue and class 2 red.
I think problem in for loop.
Console Output of Code:
Edit:
After discuss with #TaW (Thanks for showing road), I find out my problem in output (activation function). It always return 1 or -1. After that in weight change function [1-Math.Pow(o,2)] part return 0 and that make weight change equal 0. So my question how can I solve this problem. Type casting not work.
My question's solution is using normalization. For normalization I use standard deviation. Standart deviation code is below;
for(i=0;i<point.Count;i++){
x1 += point[i].X1;
x2 += point[i].X2;
}
meanx1 = x1 / point.Count;
meanx2 = x2 / point.Count;
for(i=0;i<point.Count;i++){
totalX1 += Math.Pow(point[i].X1 - meanx1, 2);
totalX2 += Math.Pow(point[i].X2 - meanx2, 2);
}
normX1 = totalX1 / (point.Count - 1);
normX2 = totalX2 / (point.Count - 1);
normX1 = normX1 / 100;
normX2 = normX2 / 100;
The last division is used to decrease the value.

startlat = (double)((int)(SECornerLat * 100) / 100.0); // does not work for 33.73 but otherwise fine

I want to have a double with only two digits after the decimal point
startlat = (double)((int)(SECornerLat * 100) / 100.0);
if SECornerLat is equal to 33.73 then startlat will be 33.72.
I tried a multiple other numbers it works fine but it returns the wrong number with 33.73.
same thing in Floor(33.73*100)/100.
This is what you want:
Double startlat = ((int) Math.Round((SECornerLat * 100), 0) / 100.0);
try this
startlat = (double)((33.73 * 100) / 100.0);

C# to mimic Excel SLOPE function

I'm trying to calculate the slope of two data lists. You can easily calculate this in EXCEL using the SLOPE function. =SLOPE(A1:A100, B1:B100). I'm trying to mimic this function in C# WinForm. Here is my code, it can calculate something, but not the correct number that you would get from the Excel function. Please help me find the error here. Thanks so much!
private double Getslope(List<double> ProductGrossExcessReturnOverRFR, List<double> primaryIndexExcessReturnOverRFR, int months, int go_back = 0)
{
double slope = 0;
double sumx = 0, sumy = 0, sumxy = 0, sumx2 = 0;
for (int i = ProductGrossExcessReturnOverRFR.Count - 1 - go_back; i > ProductGrossExcessReturnOverRFR.Count - (1 + months + go_back); i--)
{
sumxy += ProductGrossExcessReturnOverRFR[i] * primaryIndexExcessReturnOverRFR[i];
sumx += ProductGrossExcessReturnOverRFR[i];
sumy += primaryIndexExcessReturnOverRFR[i];
sumx2 += ProductGrossExcessReturnOverRFR[i] * ProductGrossExcessReturnOverRFR[i];
}
return slope = 1 / (((sumxy - sumx * sumy / months) / (sumx2 - sumx * sumx / months)));
}
Test data:
{1.085231224, 2.335034309, 0.346667278} and
{3.185231224,3.705034309 , -0.883332722} should have slope of 0.3373 if you calculate in Excel using =SLOPE function. But my code produces 0.47 somehow...
I think your formula is wrong
According to the Excel documentation the formula for SLOPE is
Note also that the first argument to the function is the the y values.
It's unclear how goback and months apply, but it looks like this might work:
private double Getslope(List<double> ProductGrossExcessReturnOverRFR,
List<double> primaryIndexExcessReturnOverRFR,
int months,
int go_back = 0)
{
// calc # of items to skip
int skip = ProductGrossExcessReturnOverRFR.Count - go_back - months;
// get list of x's and y's
var ys = ProductGrossExcessReturnOverRFR.Skip(skip).Take(months);
var xs = primaryIndexExcessReturnOverRFR.Skip(skip).Take(months);
// "zip" xs and ys to make the sum of products easier
var xys = Enumerable.Zip(xs,ys, (x, y) => new {x = x, y = y});
double xbar = xs.Average();
double ybar = ys.Average();
double slope = xys.Sum(xy => (xy.x - xbar) * (xy.y - ybar)) / xs.Sum(x => (x - xbar)*(x - xbar));
return slope;
}

Get value between a range of two values from percentage

Let's say I have a range of two values:
5...........98
and let's assume the user position's the slider at value 40
Now I want to get the value from another range of values at the exact percentage position as from range 1
let's say the second range of values are 10.........80
int nRange1 = 98 - 5;
int nRange2 = 80 - 10;
int nValue1 = 40;
int nPercentOnRange1 = ((nValue1 - 5) / nRange1)*100;
Now I have to get the value from Range2 at the exact percentage as nPercentOnRange1, but I don't know how
First need to find % from first range and apply that % to new range.
Here is what I will do:
Range1(A to B) Selected value: c
Range2(E to F)
Range1 % = (C-A) / (B-A) * 100
Range 2 corresponding value = ((F - E) * (Range 1 %) / 100) + E
C#:
int Range1Min = 5, Range1Max=90, Range1SelectedValue = 40;
int Range2Min = 6, Range2Max=80;
decimal range1Percent = (Range1SelectedValue-Range1Min ) / (Range1Max-Range1Min) * 100.0
decimal range2NewValue = (Range2Max - Range2Min) * range1Percent / 100 + Range2Min;
Watch out for
int nPercentOnRange1 = ((nValue1 - 5)/ nRange1) * 100;
ending up as zero since nValue1 and nRange1 are integers. This might be better:
int nPercentOnRange1 = ((nValue1 - 5) * 100 / nRange1);
Then you can do
int nValue2 = 10 + nPercentOnRange1*nRange2/100;
The value you need is
x = 10 + nRange2 * nPercentOnRange1 / 100.0
Let me explain why. You need a number x such that
((x - 10) / nRange2) * 100.0 = nPercentOnRange1
Therefore, just solve for x.
((x - 10) / nRange2) * 100.0 = nPercentOnRange1 =>
((x - 10) / nRange2) = nPercentOnRange1 / 100.0 =>
x - 10 = nRange2 * nPercentOnRange1 / 100.0 =>
x = 10 + nRange2 * nPercentOnRange1 / 100.0
And note that this actually makes intuitive sense. We're saying take the percentage, scale that into the length of the second range (that's what nRange2 * nPercentOnRange1 / 100.0) is doing and then add that to the lower bound of the second range. Basically we are saying step nPercentOnRange1 percent into the second range. That's exactly what the formula is expressing.
Perhaps this will work:
nValue2 = nPercentage1 * nRange2 / 100 + 10

Translating Excel formula to c# (annuity)

I have a excel formula I am trying to "translate" into c# code.
It is used to calculate an "annuity rate" over time (for example 20 years).
=(((1+E26/100)^D28*((1+E26/100)-1))/((1+E26/100)^D28-1))*100
D28 = 20 (years)
E26 = 5,00 (the rate in percent)
the ^ stands for exponent in Excel
As a result with these numbers I expect 8,02% per annum.
I tried several approaches using Math.Pow but wasn't successful.
Here is my first approach which gives me a result of 5 somehow.
double usagePanels = 20.0
double rate = 5.0
annPanels = (Math.Pow((1 + rate / 100), usagePanels) *
((1 + rate / 100) - 1) /
Math.Pow(1+rate/100, (usagePanels-1))) * 100;
Thank you.
Try:
double usagePanels = 20.0
double rate = 5.0
double annPanels = (Math.Pow((1 + rate / 100), usagePanels) *
(rate / 100.0)) /
Math.Pow(1+rate/100, usagePanels)-1)) * 100;
You've got the closing bracket, between usagePanels and the -1, wrong...
(I found this by breaking the formula apart in Excel and in C# and comparing each part.)
EDIT: Another handy tip for comparing Excel to C# is to give the cells in Excel a name (via the Named Range feature) that way the Excel formula can be made to look closer to variable names...
This should do the trick:
double rate = 5;
double years = 20;
double annunity = Math.Pow(1 + rate / 100, years) * (rate / 100) / Math.Pow(1 + rate / 100, years - 1) * 100;
For clarity, the working result is
double usagePanels = 20.0
double rate = 5.0
annPanels = (Math.Pow((1 + rate / 100), usagePanels) *
((1 + rate / 100) - 1) /
(Math.Pow(1+rate/100, (usagePanels))-1)) * 100;
Thanks to Jason Allen and Grhm who basically figured it out and gave great advice.

Categories