-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculateWeightedAverageOfShares.ts
More file actions
136 lines (109 loc) · 4.01 KB
/
calculateWeightedAverageOfShares.ts
File metadata and controls
136 lines (109 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* My friend was stuck on their accounting homework and decided to help them figure out how to calculate the weighted average number of shares
* https://www.investopedia.com/ask/answers/12/weighted-average-shares-basic-weight-shares.asp#:~:text=The%20weighted%20average%20number%20of,number%20applies%20for%20each%20period */
// Define a type for an individual change
type Change = {
month: string;
change: number;
};
// Define a type for the month mappings
type MonthMappings = {
[key: string]: number;
};
// Define a type for the array of changes
type ChangesArray = Change[];
const monthMappings: MonthMappings = {
'January': 1, // January
'February': 2, // January to February
'March': 3, // January to March
'April': 4, // January to April
'May': 5, // January to May
'June': 6, // January to June
'July': 7, // January to July
'August': 8, // January to August
'September': 9, // January to September
'October': 10, // January to October
'November': 11, // January to November
'December': 12, // January to December
};
// The companies profit for the year
const profit: number = 880000;
// Data to indicate when there are additional shares added or retired
const changes: ChangesArray = [
{ month: 'January', change: 0 },
{ month: 'April', change: 40000 },
{ month: 'November', change: -30000 },
];
const sharesOutstanding: number = 500000; // Initial shares outstanding
const preferredDividends: number = 250000;
const calculateMonthsLeftInYear = (
currentMonth: number
) => {
const monthsInYear = 12;
return monthsInYear - currentMonth + 1; // +1 to include the current month
}
const calculateFractionOfYearOutstanding = (
changes: ChangesArray,
i,
): number => {
if (i < 0 || i >= changes.length) {
return 0; // Default value if i is out of range
}
const currentMonth: number = monthMappings[changes[i].month];
if (i === changes.length - 1) {
return calculateMonthsLeftInYear(currentMonth) / 12;
}
const monthOfNextStockChange: number = monthMappings[changes[i + 1].month];
// Months that passed between the current month and month of the next stock change
if (currentMonth !== undefined && monthOfNextStockChange !== undefined) {
const monthsBetween: number = monthOfNextStockChange - currentMonth;
return monthsBetween / 12; // Calculate fractionOfYearOutstanding
}
return 0; // Default value if no valid months are found
}
const calculateEndShares = (
changes: ChangesArray,
i: number,
startShares: number,
): number => {
let endShares = startShares;
const currentChange = changes[i];
if (currentChange && monthMappings[currentChange.month]) {
endShares += currentChange.change;
}
return endShares;
}
const calculateWeightedAverageShare = (
changes: ChangesArray,
sharesOutstanding: number
) => {
let weightedAverageSum: number = 0; // Initialize the sum of the weighted share
for (let i = 0; i < changes.length; i++) {
const startShares: number = sharesOutstanding;
// Calculate fraction of year Outstanding
const startFraction: number = calculateFractionOfYearOutstanding(changes, i);
// Calculate Month changes happened and endShares
const endShares: number = calculateEndShares(changes, i, startShares);
// Update weightedAverageSum and sharesOutstanding
weightedAverageSum += endShares * startFraction;
sharesOutstanding = endShares;
}
return weightedAverageSum;
}
const calculateEarningsPerShare = (
changes: ChangesArray,
sharesOutstanding: number,
profit: number,
preferredDividends: number
): number => {
const earnings: number = profit - preferredDividends; // Overall company earnings = profit - preferredDividends
const weightedAverageCommonShares: number = calculateWeightedAverageShare(changes, sharesOutstanding);
return earnings / weightedAverageCommonShares;
}
// Calculate earnings per share
const earningsPerShare: number = calculateEarningsPerShare(
changes,
sharesOutstanding,
profit,
preferredDividends
);
console.log(earningsPerShare);