forked from nmcl/JavaSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariance.java
More file actions
178 lines (144 loc) · 4.44 KB
/
Variance.java
File metadata and controls
178 lines (144 loc) · 4.44 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
/*
* Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne
* and others contributors as indicated
* by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
* (C) 1990-2008,
*/
package org.javasim.stats;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Used to obtain the variance of the samples given.
*/
public class Variance extends Mean
{
public Variance()
{
reset();
}
/**
* @param Add to the instance, updating the variance.
*/
public void setValue (double value) throws IllegalArgumentException
{
_sqr += value * value;
super.setValue(value);
}
/**
* Zero the statistics.
*/
public void reset ()
{
_sqr = 0.0;
super.reset();
}
/**
* @return the variance.
*/
public double variance ()
{
if (_Number > 1)
return ((_sqr - ((_Sum * _Sum) / _Number)) / (_Number - 1));
else
return 0.0;
}
/**
* @return the standard deviation of the samples.
*/
public double stdDev ()
{
if (_Number == 0 || variance() <= 0)
return 0.0;
else
return Math.sqrt(variance());
}
/**
* @return the confidence.
*
* @param value the confidence range should be between 0 and 0.9999
*/
public double confidence (double value) throws IllegalArgumentException
{
if ((value > 1) || (value < 0))
throw new IllegalArgumentException();
return mean() + (1+value)*stdDev();
}
/**
* Prints out the statistics information.
*/
public void print ()
{
System.out.println("Variance : " + variance());
System.out.println("Standard Deviation: " + stdDev());
super.print();
}
/**
* Save the state of the histogram to the file named 'fileName'.
*
* @param fileName the file to use.
* @return <code>true</code> if it succeeds, <code>false</code> otherwise.
*/
public boolean saveState (String fileName) throws IOException
{
FileOutputStream f = new FileOutputStream(fileName);
DataOutputStream oFile = new DataOutputStream(f);
boolean res = saveState(oFile);
f.close();
return res;
}
/**
* Save the state of the histogram to the stream 'oFile'.
*
* @param oFile the stream to use.
* @return <code>true</code> if it succeeds, <code>false</code> otherwise.
*/
public boolean saveState (DataOutputStream oFile) throws IOException
{
oFile.writeDouble(_sqr);
return super.saveState(oFile);
}
/**
* Restore the histogram state from the file 'fileName'.
*
* @param fileName the file to use.
* @return <code>true</code> if it succeeds, <code>false</code> otherwise.
*/
public boolean restoreState (String fileName) throws FileNotFoundException,
IOException
{
FileInputStream f = new FileInputStream(fileName);
DataInputStream iFile = new DataInputStream(f);
boolean res = restoreState(iFile);
f.close();
return res;
}
/**
* Restore the histogram state from the stream 'iFile'.
*
* @param iFile the stream to use.
* @return <code>true</code> if it succeeds, <code>false</code> otherwise.
*/
public boolean restoreState (DataInputStream iFile) throws IOException
{
_sqr = iFile.readDouble();
return super.restoreState(iFile);
}
private double _sqr;
}