-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStats.java
More file actions
207 lines (180 loc) · 4.69 KB
/
Copy pathStats.java
File metadata and controls
207 lines (180 loc) · 4.69 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package projectComplete;
import java.util.*;
public class Stats
{
static final double maxinvest=200000.0;
static double[][] data;
static int rows;
static int cols;
//COULDN'T GET THIS CONSTRUCTOR TO WORK SO JUST IGNORE IT INCLUDING SOME OF THE STATIC DECALARATIONS ABOVE
public Stats(double[][] data)
{
data = data;
rows = data.length;
cols =data[0].length;
}
/**
* takes in all the data and outputs an array containing the mean change%
* of every company with corresponding indexes
* @param double [][]data of change rates
* @return double[] means
*/
public static double[] getMeans(double [][] data)
{
int rows = data.length;
int cols =data[0].length;
double means[] = new double[cols];
double sums[]= new double[cols];
for(int c =1;c<cols;c++)
{
for(int r= 1 ; r<rows;r++)
{
sums[c]+= data[r][c];
}
means[c]=((sums[c])/(rows-1));
}
return means;
}
/**
*takes in all our change % per company and
* calculates the variance of each company
* storing it in a corresponding vars[]
*
*
* @param double[][]data, double[] means
* @return double[]vars
*/
public static double[] getVariance(double[][] data,double[] means)
{
int rows = data.length;
int cols =data[0].length;
double temp[] = new double[cols];
double vars[] = new double[cols];
for(int c=1;c<cols;c++)
{
for(int r=1;r<rows;r++)
{
temp[c] += (means[c]-data[r][c])*(means[c]-data[r][c]);
}
vars[c]=((temp[c])/(rows-1));
}
return vars;
}
/**
*calculates the variance for each company %change *
* @param double [][]data,double[]vars
* @return sds[]
*/
public static double[] getStdDev(double[][] data, double[] vars)
{
int rows = data.length;
int cols =data[0].length;
double sds[] = new double[cols];
for(int c=1;c<cols;c++)
{
sds[c] = Math.sqrt(vars[c]);
}
return sds;
}
/**
*calculates the x[] we need to later calculate our volatility
* @param double[][] data,int [] invests,double[]euros
* @return x[]
*/
public static double[] getSumProd(double[][] data,int [] invests,double[]euros)
{
int rows = data.length;
int cols =data[0].length;
double x[] =new double[rows];
double totalinvest=0;
for(int r = 1; r<rows;r++)
{ double temp=0;
totalinvest=0;
for(int c=1; c<cols;c++)
{
if(invests[c]!=0)
{
temp+=invests[c]*euros[c]*data[r][c];
totalinvest+=invests[c]*euros[c];
}
}
x[r]=temp/totalinvest;
}
return x;
}
/**
*calculate the mean of x
* @param x[]
* @return mean an double
*/
public static double getmean(double[]x)
{
double sum=0;
double mean=0;
for(int r= 1 ; r<rows;r++)
{
sum+= x[r];
}
mean=((sum)/(rows-1));
return mean;
}
/**
*calculates the variance of x[]
* @param double[] x
* @return var a double
*/
public static double getvar(double[]x)
{
int rows = x.length;
double temp=0;
double var=0;
double mean=getmean(x);
for(int r=1;r<rows;r++)
{
temp += (mean-x[r])*(mean-x[r]);
}
var=((temp)/(rows-1));
return var;
}
/**
*calculates the volatility of x[]
* @param double x[]
* @return vol
*/
public static double getvol(double[] x)
{
double vol=0;
vol=Math.sqrt(getvar(x));
return vol;
}
/**
*calculates how much we have invested
* @param double[] euro, double[] invests
* @return double investtotal
*/
public static double getcurrinvest(double[] euro,int[] invests)
{
int cols = invests.length;
double currinvest=0;
for(int c = 1;c<cols;c++)
{
if(invests[c]!=0)
{
currinvest+=euro[c]*invests[c];
}
}
return currinvest;
}
}
/* public double median()
{
Arrays.sort(data);
if (data.length % 2 == 0)
{
return (data[(data.length / 2) - 1] + data[data.length / 2]) / 2.0;
}
else
{
return data[data.length / 2];
}
}*/