-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountryAndStatePredictions.java
More file actions
109 lines (103 loc) · 3.48 KB
/
CountryAndStatePredictions.java
File metadata and controls
109 lines (103 loc) · 3.48 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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
class CountryAndStatePredictions {
static double mean = 0;
static double x_not = 0;
static int n = 0;
static int total[];
/**
* calls all functions and displays results
* @param args the number of days for which the prediction is to be given
*/
public static void main (String args[]) {
input();
analyse();
System.out.println(mean + " " + x_not);
long prediction[] = prediction(Integer.parseInt(args[0]));
for (long i: prediction) {
System.out.println(i);
}
}
/**
* inputs the values from the data source and stores it in an array
*/
public static void input() {
try {
File file = new File ("data.csv");
Scanner sc = new Scanner(file);
Scanner sc1 = new Scanner(file);
int count = 0;
while(sc.hasNextInt()) {
count ++;
sc.nextLine();
}
n = count;
total = new int[n];
for (int i = 0; i < n; i++) {
total[i] = sc1.nextInt();
}
} catch (FileNotFoundException e) {
System.out.println("File Not Found");
} catch (Exception e) {
System.out.println("Exception");
}
}
/**
* Calculates the Gaussian mean and x_not value using historic data
*/
public static void analyse() {
double[] mean1 = new double[n - 2];
int pointer = 0;
double x = 0;
double mean_calc = 0;
for(int i = 1; i < n - 1; i++) {
int current = total[i];
int previous = total[i - 1];
int next = total[i + 1];
double fx = (next - previous) / 2;
double fpp = (next + previous - 2 * current);
if(current * fpp - fx * fx != 0) {
mean1[pointer++] = getMean(i, fx, current, fpp) / (current * fpp - fx * fx);
x += (mean1[pointer - 1] * current) / i;
}
}
for(int i = 0; i < pointer; i++) {
x_not += (mean1[i]) / (pointer);
}
mean = x / pointer;
}
/**
* Calculates the value of mu for each iteration (see Appendix A)
* @param x the current day (x-coordinate)
* @param fx the number of reported cases on day x
* @param fm the slope of the graph at x
* @param fpp the rate of change of slope of the graph
* @return the value mu for the given parameters
*/
public static double getMean(int x, double fm, int fx, double fpp) {
return - fx * fm + x * fx * fpp - x * fm * fm;
}
/**
* returns the prediction for the total cases of Covid-19 for specified number of days
* @param days the number of days including and after 25/5 for which prediction is required
*
* @return an array with the prediction for the given number of days.
*/
public static long[] prediction(int days) {
long y[] = new long[days];
for (int i = 0; i < days; i++) {
double x = (n + 0.5 * i - 70) / (x_not);
y[i] = Math.round (9 * mean * sigmoid(x)) + 94400;
}
return y;
}
/**
* Returns the value of sigmoid(x)
* @param x value for which sigmoid needs to be given.
* @return the value of sigmoid(x)
*/
public static double sigmoid(double x) {
return 1 / (1 + Math.exp(-x));
}
}