|
1 | 1 | package com.thealgorithms.maths; |
2 | 2 |
|
3 | 3 | /** |
4 | | - * Return on Investment (ROI) measures the profitability of an investment |
5 | | - * relative to its cost, expressed as a percentage. |
| 4 | + * Return on Investment (ROI) calculations for evaluating investment profitability. |
6 | 5 | * |
7 | | - * <p>Formula: ROI = (Gain - Cost) / Cost × 100 |
| 6 | + * <p>This class provides two related computations: |
| 7 | + * <ul> |
| 8 | + * <li><b>Simple ROI</b> – measures total gain relative to cost: |
| 9 | + * {@code ROI = (Gain - Cost) / Cost × 100}</li> |
| 10 | + * <li><b>Annualized ROI</b> – converts a total ROI over multiple years into |
| 11 | + * an equivalent annual rate using the geometric mean: |
| 12 | + * {@code Annualized ROI = ((1 + ROI/100)^(1/n) - 1) × 100}</li> |
| 13 | + * </ul> |
8 | 14 | * |
9 | | - * @see <a href="https://www.investopedia.com/terms/r/returnoninvestment.asp">Investopedia</a> |
| 15 | + * @see <a href="https://www.investopedia.com/terms/r/returnoninvestment.asp">Investopedia – ROI</a> |
| 16 | + * @see <a href="https://www.investopedia.com/terms/a/annualized-total-return.asp">Investopedia – Annualized Return</a> |
10 | 17 | */ |
11 | 18 | public final class ReturnOnInvestment { |
| 19 | + |
12 | 20 | private ReturnOnInvestment() { |
13 | 21 | } |
14 | 22 |
|
15 | 23 | /** |
16 | | - * Calculates the return on investment as a percentage. |
| 24 | + * Calculates the simple return on investment as a percentage. |
17 | 25 | * |
18 | | - * @param gainFromInvestment the total value gained from the investment |
19 | | - * @param costOfInvestment the total cost of the investment |
20 | | - * @return ROI as a percentage |
21 | | - * @throws IllegalArgumentException if costOfInvestment is not positive |
| 26 | + * @param gainFromInvestment the total value received from the investment |
| 27 | + * @param costOfInvestment the total cost of the investment (must be positive) |
| 28 | + * @return ROI as a percentage; negative when a loss occurred |
| 29 | + * @throws IllegalArgumentException if {@code costOfInvestment} is not positive |
22 | 30 | */ |
23 | | - public static double returnOnInvestment(double gainFromInvestment, double costOfInvestment) { |
| 31 | + public static double returnOnInvestment(final double gainFromInvestment, final double costOfInvestment) { |
24 | 32 | if (costOfInvestment <= 0) { |
25 | 33 | throw new IllegalArgumentException("costOfInvestment must be greater than 0"); |
26 | 34 | } |
27 | 35 | return (gainFromInvestment - costOfInvestment) / costOfInvestment * 100.0; |
28 | 36 | } |
| 37 | + |
| 38 | + /** |
| 39 | + * Calculates the annualized (per-year) return on investment. |
| 40 | + * |
| 41 | + * <p>While simple ROI tells you the total gain over an entire holding period, |
| 42 | + * annualized ROI normalizes that gain to a yearly rate so that investments |
| 43 | + * held for different lengths of time can be compared on equal footing. |
| 44 | + * It applies the geometric-mean formula: |
| 45 | + * |
| 46 | + * <pre> |
| 47 | + * Annualized ROI = ((1 + simpleROI / 100) ^ (1 / years) - 1) × 100 |
| 48 | + * </pre> |
| 49 | + * |
| 50 | + * @param gainFromInvestment the total value received from the investment |
| 51 | + * @param costOfInvestment the total cost of the investment (must be positive) |
| 52 | + * @param years the number of years the investment was held (must be positive) |
| 53 | + * @return annualized ROI as a percentage |
| 54 | + * @throws IllegalArgumentException if {@code costOfInvestment} or {@code years} is not positive |
| 55 | + */ |
| 56 | + public static double annualizedReturnOnInvestment(final double gainFromInvestment, final double costOfInvestment, final double years) { |
| 57 | + if (costOfInvestment <= 0) { |
| 58 | + throw new IllegalArgumentException("costOfInvestment must be greater than 0"); |
| 59 | + } |
| 60 | + if (years <= 0) { |
| 61 | + throw new IllegalArgumentException("years must be greater than 0"); |
| 62 | + } |
| 63 | + final double simpleRoi = returnOnInvestment(gainFromInvestment, costOfInvestment); |
| 64 | + return (Math.pow(1.0 + simpleRoi / 100.0, 1.0 / years) - 1.0) * 100.0; |
| 65 | + } |
29 | 66 | } |
0 commit comments