|
| 1 | +""" |
| 2 | +FILE: sf-prediction.py |
| 3 | +DATE: 24 February 2022 |
| 4 | +
|
| 5 | +DESC: Predict the number of nodes for different scale factors. |
| 6 | + The known values for each scale factor are: |
| 7 | + _____________________________________________________________________ |
| 8 | + | Scale Factor | 1 | 3 | 10 | 30 | 100 | 300 | |
| 9 | + ---------------------------------------------------------------------- |
| 10 | + | Nr. of Persons | 10620 | 25870 | 70800 | 175950 | 487700 | 1230500 | |
| 11 | + ---------------------------------------------------------------------- |
| 12 | +
|
| 13 | + ___________________________________________________________ |
| 14 | + | Scale Factor | 1000 | 3000 | 10000 | 30000 | |
| 15 | + ------------------------------------------------------------- |
| 16 | + | Nr. of Persons | 3505000 | 9232000 | 27200000 | 77000000 | |
| 17 | + ------------------------------------------------------------ |
| 18 | +
|
| 19 | + The values are approximated using a polynomial with degree 3 for scale |
| 20 | + factors untill 3000 with the nr. of persons and scale factors scaled |
| 21 | + with natural logarithm. For scale factors larger than 3000, a 5th degree |
| 22 | + polynomial is used without log-scale. |
| 23 | +""" |
| 24 | + |
| 25 | +import numpy as np |
| 26 | + |
| 27 | +def approximate_small_sf(sf, num_persons, sf_new): |
| 28 | + """ |
| 29 | + :param: sf (List): List of scale factors |
| 30 | + :param: num_persons (List): List of number of persons aligned with the |
| 31 | + scale factor list |
| 32 | + :param: sf_new (List): The scale factors to predict |
| 33 | +
|
| 34 | + :return: List of predicted number of persons |
| 35 | + """ |
| 36 | + coeffs = np.polyfit(np.log(sf), np.log(num_persons), deg=3) |
| 37 | + poly = np.poly1d(coeffs) |
| 38 | + yfit = lambda x: np.exp(poly(np.log(x))) |
| 39 | + return np.array(yfit(sf_new)) |
| 40 | + |
| 41 | +def approximate_large_sf(sf, num_persons, sf_new): |
| 42 | + """ |
| 43 | + :param: sf (List): List of scale factors |
| 44 | + :param: num_persons (List): List of number of persons aligned with the |
| 45 | + scale factor list |
| 46 | + :param: sf_new (List): The scale factors to predict |
| 47 | +
|
| 48 | + :return: List of predicted number of persons |
| 49 | + """ |
| 50 | + coeffs = np.polyfit(sf, num_persons, deg=5) |
| 51 | + poly = np.poly1d(coeffs) |
| 52 | + return np.array(poly(sf_new)) |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + sf = [1, 3, 10, 30, 100, 300, 1000, 3000, 10000, 30000] |
| 56 | + num_persons = [ |
| 57 | + 10620, 25870, 70800, 175950, 487700, 1230500, 3505000, 9232000, |
| 58 | + 27200000, 77000000 |
| 59 | + ] |
| 60 | + sf_new = [1, 3, 10, 30, 100, 300, 1000, 3000, 10000, 30000, 100000] |
| 61 | + |
| 62 | + predicted_large_sf = approximate_large_sf(sf, num_persons, sf_new) |
| 63 | + predicted_small_sf = approximate_small_sf(sf, num_persons, sf_new) |
| 64 | + |
| 65 | + print("Number of Persons") |
| 66 | + print(num_persons) |
| 67 | + print("Polyfit Large") |
| 68 | + print(predicted_large_sf) |
| 69 | + print("Polyfit Small") |
| 70 | + print(predicted_small_sf) |
| 71 | + |
| 72 | + print("Error Polyfit Large") |
| 73 | + print(np.abs(predicted_large_sf[:10] - num_persons)) |
| 74 | + print("Error Polyfit Small") |
| 75 | + print(np.abs(predicted_small_sf[:10] - num_persons)) |
0 commit comments