Im trying to understand some program that handles time in a certain system I am still to know of. Hopefully you can tell me what system is, if any.
One of the value in numbers is 170000000 and it represents the 26th of April 2037. Another example is 164632577 which represents the 20th of December 2022.
I tested both with an EPOCH converter but I get completely different dates so its not EPOCH for sure. Have any clue?
Thank you.
We assume that the fomula for converting from a date to those strange time units is of the following form:
f(x) = m*x + b
where x is in strange time units and f is in days:
f(2037*365.2425 + 31 + 28 + 31 + 26) = 170000000
f(2022*365.2425 + 365.2425 - (31 + 1 - 20) = 164632577
because we have two data points, we can make two formulas:
I : f1 = m * x1 + b
II: f2 = m * x2 + b
Now we’re looking for: m, b
We solve as follows:
I => III: b = f1 - m*x1
III into II: f2 = m*x2 + f1 - m*x1 => f2 - f1 = m(x2 - x1) => m = (f2 - f1) / (x2 - x1)
goes down to:
m = 1024.04 units/day and (exactly 1024, most likely, because that’s 2^10)
with b = f1 - m*x1
b = -591973731.84 (??)
so you get:
for converting from days since year 1 to those strange time units:
f(x) = 1024 * x - 591973731.84
where x is in days, so year * 365.2524 + (months-1) * 30 + days
testing it reveals that
f(Jan 1st 2038 = 2038*365.2425) = 1024*2038*365.2425 - 591973731.84 = 170255224.3, which is just a bit more than Dec 20th 2037, so it works.
Strangely, the 0 point of those strange time units represents about the year 1582 (solution for x of f(x) = 0).
170000000 seems very rounded. Do you know exact seconds within the game your numbers represent?
It should be a linear system, and you know that the value of (170000000 - 164632577) = 5367423 equals the difference between your days (2037-04-26 - 2022-12-20) = 5241 days.
This means that one day is (5241 / 5367423) = 0.00097644623873...
Counting back from 164632577 to zero places takes us back (164632577 * 0.00097644623873...) = 160754.86 days, from 2022-12-20 to 1582-11-02.
Same calculations done on 170000000 takes us back (170000000 * 0.00097644623873...) = 165995,86 days, from 2037-04-26 to 1582-11-02. Heuruka!
So, you have a system where timeFor($value) = [1582-11-02] + [0.00097644623873... * $value days].
Issues:
There are several rounding issues with these numbers. Your dates most probably include seconds, but we've calculating on whole days.
We're moving back in time, and time travel related issues will appear. These include, but are not limited to, non existing dates according to your calendar.
Things to consider; October 15th of 1582, or 1582-10-15 is the start of the Gregorian calender. This is probably the real start date for your data.
Edit: I previously wrote that the multiplier should probably be 0.001, but as Daniel noted in another answer, it's actually 1/1024 = 0.0009765625.
Related
For every mouse, it takes two months for them to become mature after birth, then they are able to give birth to baby mouses. A mature mouse can give birth to 12 baby mouses every month. We have one mouse initially, and what's the total numebr of mouse after ten months?
My transition equation is F(n) = F(n-1) + 12 * F(n-2), but my classmate told me this is not right. So what's the right equation of this question?
Your transition equation needs to be a matrix (3x3) so that given 3 states as a vector (newborn, 1m old, 2m+ old) gives you new vector of new states after 1m. Logic to build such matrix is similar as your reasoning.
new_state = matrix * current_state
EDIT:
matrix that we build represents 3 linear equations.
# abbreviations
ns = new state
nb = newborn
1m = 1 month old
2m = 2 mounts old or more
cs = current state
mXX = matrix index
# 3 scalar questions from matrix eqution ->
# ns = matrix * cs
ns_nb = m00 * cs_nb + m01 * cs_1m + m02 * cs_2m
ns_1m = m10 * cs_nb + m11 * cs_1m + m12 * cs_2m
ns_2m = m20 * cs_nb + m21 * cs_1m + m22 * cs_2m
Now you need to figure out what m00 - m22 are based on your requrements
Enlightened by answer by Luka above, I figure out these coefficients
ns_nb = 12 * cs_2m + 12 * cs_1m
ns_1m = cs_nb
ns_2m = cs_2m + cs_1m
From the posts of Luka Rahne and KningTG,
I think the python code below will work fine for the problem using dynamic programming:
# Initial Conditions
new_born = 1
one_month_old = 0
mature_mouse = 0
n = 10 # Month upto which we want to find
i = 1
while(i<n):
# Finding updated new value
new_born_update = 12*(one_month_old+mature_mouse)
one_month_old_update = new_born
mature_mouse_update = one_month_old + mature_mouse
# Updating values
new_born,one_month_old,mature_mouse = new_born_update,one_month_old_update,mature_mouse_update
i = i + 1
# Calculating the total number of mouses for month n
Total_mouses = new_born+one_month_old+mature_mouse
# Printing total number of mouses
print(Total_mouses)
I want to calculate an equation within a controller(Arduino)
y = -0.0000000104529251928664x^3 + 0.0000928316793270531x^2 - 0.282333029643959x + 297.661280719026
Now the decimal values of the coefficients are important because "x" varies in thousands so cube term cannot be ignored. I have tried manipulating the equation in excel to reduce the coefficients but R^2 is lost in the process and I would like to avoid that.
Max variable size available in Arduino is 4byte. And on google search, I was not able to find an appropriate solution.
Thank you for your time.
Since
-0.0000000104529251928664 ^ (1/3) = - 0.0021864822
0.0000928316793270531 ^ (1/2) = 0.00963491978
The formula
y = -0.0000000104529251928664x^3 + 0.0000928316793270531x^2 - 0.282333029643959x + 297.661280719026
Can be rewritten:
y = -(0.0021864822 * x)^3 + (0.00963491978 * x)^2 - 0.282333029643959 * x + 297.661280719026
Rounding all coefficients to 10 decimal places, we get:
y = -(0.0021864822 * x)^3 + (0.00963491978 * x)^2 - 0.2823330296 * x + 297.6612807
But I don't know Arduino, I'm not sure what the correct number of decimal places is, nor do I know what the compiler will accept or refuse.
My client is a financial advisor and helps people create retirement plans. His current process is to take all their financial data, type them into a spreadsheet and, based on the retiree's goals, discover the rate at which they can withdraw from their savings/assets/investments (as a percentage). That percentage is the solution to a problem, and the way he finds it now is to guess at it (e.g. "Let's try 5% (too high). OK, how about 1% (too low). Hmm, 2.5%? (too high) ... until he finds the percentage that satisfies the conditions of the retiree).
If I were to program this exactly how he does it, then I think it's just a binary search algorithm. But it feels like there's a more clever way to do this. He's basically using the compound interest formula, A=P(1+r/n)^nt, to discover 'r' in that equation, but it has to be done over a period of several decades, and each year requires about a dozen calculations in the backend. So roughly a dozen, times maybe 30 years equals ~300 calculations for one iteration of the binary search.
Sorry if this isn't detailed enough, but to be more specific requires and exhaustive level of detail.
Has anyone, perhaps someone in the financial sector, dealt with this kind of search?
Sorry if I've misunderstood what you're asking. If it's just to find r in the formula you give:
A = P (1 + r / n) ^ nt
A/P = (1 + r / n) ^ nt
log_nt A/P = 1 + r / n
log_nt A/P - 1 = r / n
n (log_nt A/P - 1) = r
More generally, if you think you might be able to get a closed-form solution, you should try really hard to write down your model and equations in such a way that you can find such a solution.
Some benefits to this approach:
The result is easier to implement and to understand. You can put the derivation of the formula in comments if you like.
The result is virtually guaranteed to be more precise than what you'd get from a search technique.
It will run fast.
Here's how I might model the problem:
P: principal
r: monthly interest gained on principal
w: amount withdrawn from principal
T: number of months over which the principal is to be withdrawn
B(t): balance at time 0 <= t <= T
B(0) = P
B(T) = 0
We want to find w. We write our recurrences:
B(0) = P
B(t+1) = B(t) * r - w
We can write out a few terms:
B(0) = P
B(1) = P * r - w
B(2) = (P * r - w) * r - w = P * r^2 - wr - w
B(3) = (P * r^2 - wr - w) * r - w = P * r^3 - wr^2 - wr - w
...
B(t) = P * r^t - w(r^(t-1) + r^(t-2) + ... + 1)
= P * r^t - w(r^t - 1)/(r - 1)
Now we set B(T) = 0 assuming we want the money to run out, then solve for w:
0 = B(T) = P * r^T - w(r^T - 1)/(r - 1)
w(r^T - 1)/(r - 1) = P * r^T
w = P * r^T * (r - 1) / (r^T - 1)
Suppose that P = $1,000,000, r = 1.0025 (just above 3% annually), and T = 360 (retirement savings to last 30 years). Then we have
w = $1,000,000 * 1.0025^360 * (1.0025 - 1) / (1.0025 ^ 360 - 1)
= $4,216
If you want to model the situation differently, you need only write it down and follow the same steps as this. With any luck, your model will have some closed form solution, as the two problems I've solved in this answer did.
I apologize if the answer for this is somewhere already, I've been searching for a couple of hours now and I can't find what I'm looking for.
I'm building a simple financial calculator to calculate the cash flows given the target IRR. For example:
I have an asset worth $18,000,000 (which depreciates at $1,000,000/year)
I have a target IRR of 10% after 5 years
This means that the initial investment is $18,000,000, and in year 5, I will sell this asset for $13,000,000
To reach my target IRR of 10%, the annual cash flows have to be $2,618,875. Right now, I calculate this by hand in an Excel sheet through guess-and-check.
There's other variables and functionality, but they're not important for what I'm trying to do here. I've found plenty of libraries and functions that can calculate the IRR for a given number of cash flows, but nothing comes up when I try to get the cash flow for a given IRR.
At this point, I think the only solution is to basically run a loop to plug in the values, check to see if the IRR is higher or lower than the target IRR, and keep calculating the IRR until I get the cash flow that I want.
Is this the best way to approach this particular problem? Or is there a better way to tackle it that I'm missing? Help greatly appreciated!
Also, as an FYI, I'm building this in Ruby on Rails.
EDIT:
IRR Function:
NPV = -(I) + CF[1]/(1 + R)^1 + CF[2]/(1 + R)^2 + ... + CF[n]/(1 + R)^n
NPV = the Net Present Value (this value needs to be as close to 0 as possible)
I = Initial investment (in this example, $18,000,000)
CF = Cash Flow (this is the value I'm trying to calculate - it would end up being $2,618,875 if I calculated it by hand. In my financial calculator, all of the cash flows would be the same since I'm solving for them.)
R = Target rate of return (10%)
n = the year (so this example would end at 5)
I'm trying to calculate the Cash Flows to within a .005% margin of error, since the numbers we're working with are in the hundreds of millions.
Let
v0 = initial value
vn = value after n periods
n = number of periods
r = annual rate of return
y = required annual net income
The one period discount factor is:
j = 1/(1+r)
The present value of the investment is:
pv = - v0 + j*y + j^2*y + j^3*y +..+ j^n*y + j^n*vn
= - v0 + y*(j + j^2 + j^3 +..+ j^n) + j^n*vn
= - v0 + y*sn + j^n*vn
where
sn = j + j^2 + j^3 + j^4 +..+ j^n
We can calulate sn as follows:
sn = j + j^2 + j^3 + j^4 +..+ j^n
j*sn = j^2 + j^3 + j^4 +..+ j^n + j^(n+1)
sn -j*sn = j*(1 - j^n)
sn = j*(1 - j^n)/(1-j)
= (1 - j^n)/[(1+r)(r/(1+r)]
= (1 - j^n)/r
Set pv = 0 and solve for y:
y*sn = v0 - vn * j^n
y = (v0 - vn * j^n)/sn
= r * (v0 - vn * j^n)/(1 - j^n)
Our Ruby method:
def ann_ret(v0, vn, n, r)
j = 1/(1+r)
(r * (v0 - vn * j**n)/(1 - j**n)).round(2)
end
With annual compounding:
ann_ret(18000000, 13000000, 5, 0.1) # => 2618987.4
With semi-annual compounding:
2 * ann_ret(18000000, 13000000, 10, 0.05) # => 2595045.75
With daily compounding:
365 * ann_ret(18000000, 13000000, 5*365, 0.10/365) # => 2570881.20
These values differ slightly from the required annual return you calculate. You should be able to explain the difference by comparing present value formulae.
There's a module called Newton in Ruby... it uses the Newton Raphson method.
I've been using this module to implement the IRR function into this library:
https://github.com/Noverde/exonio
If you need the IRR, you can use like this:
Exonio.irr([-100, 39, 59, 55, 20]) # ==> 0.28095
So I'm creating a Activation class for a VB6 project and I've run into a brain fart. I've designed how I want to generate the Serial Number for this particular product in a following way.
XXXX-XXXX-XXXX-XXXX
Each group of numbers would be representative of data that I can read if I'm aware of the matching document that allows me to understand the codes with the group of digits. So for instance the first group may represent the month that the product was sold to a customer. But I can't have all the serial numbers in January all start with the same four digits so there's some internal math that needs to be done to calculate this value. What I've landed on is this:
A B C D = digits in the first group of the serial number
(A + B) - (C + D) = #
Now # would relate to a table of Hex values that would then represent the month the product was sold. Something like...
1 - January
2 - February
3 - March
....
B - November
C - December
My question lies here - if I know I need the total to equal B(11) then how exactly can I code backwards to generate (A + B) - (C + D) = B(11)?? It's a pretty simple equation, I know - but something I've just ran into and can't seem to get started in the right direction. I'm not asking for a full work-up of code but just a push. If you have a full solution available and want to share I'm always open to learning a bit more.
I am coding in VB6 but VB.NET, C#, C++ solutions could work as well since I can just port those over relatively easily. The community help is always greatly appreciated!
There's no single solution (you have one equation with four variables). You have to pick some random numbers. Here's one that works (in Python, but you get the point):
from random import randint
X = 11 # the one you're looking for
A_plus_B = randint(X, 30)
A = randint(max(A_plus_B - 15, 0), min(A_plus_B, 15))
B = A_plus_B - A
C_plus_D = A_plus_B - X
C = randint(max(C_plus_D - 15, 0), min(C_plus_D, 15))
D = C_plus_D - C
I assume you allow hexadecimal digits; if you just want 0 to 9, replace 15 by 9 and 30 by 18.
OK - pen and paper is always the solution... so here goes...
Attempting to find what values should be for (A + B) - (C + D) to equal a certain number called X. First I know that I want HEX values so that limits me to 0-F or 0-15. From there I need a better starting place so I'll generate a random number that will represent the total of (A + B), we'll call this Y, but not be lower than value X. Then subtract from that number Y value of X to determine that value that will represent (C + D), which we'll call Z. Use similar logic to break down Y and Z into two numbers each that can represent (A + B) = Y and (C + D) = Z. After it's all said and done I should have a good randomization of creating 4 numbers that when plugged into my equation will return a suitable result.
Just had to get past the brain fart.
This may seem a little hackish, and it may not take you where you're trying to go. However it should produce a wider range of values for your key strings:
Option Explicit
Private Function MonthString(ByVal MonthNum As Integer) As String
'MonthNum: January=1, ... December=12. Altered to base 0
'value for use internally.
Dim lngdigits As Long
MonthNum = MonthNum - 1
lngdigits = (Rnd() * &H10000) - MonthNum
MonthString = Right$("000" & Hex$(lngdigits + (MonthNum - lngdigits Mod 12)), 4)
End Function
Private Function MonthRecov(ByVal MonthString As String) As Integer
'Value returned is base 1, i.e. 1=January.
MonthRecov = CInt(CLng("&H" & MonthString) Mod 12) + 1
End Function
Private Sub Form_Load()
Dim intMonth As Integer
Dim strMonth As String
Dim intMonthRecov As Integer
Dim J As Integer
Randomize
For intMonth = 1 To 12
For J = 1 To 2
strMonth = MonthString(intMonth)
intMonthRecov = MonthRecov(strMonth)
Debug.Print intMonth, strMonth, intMonthRecov, Hex$(intMonthRecov)
Next
Next
End Sub