Skip to content

Instantly share code, notes, and snippets.

@doraiso
Last active March 12, 2022 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doraiso/be8345fff10d0865999dfda69af17fea to your computer and use it in GitHub Desktop.
Save doraiso/be8345fff10d0865999dfda69af17fea to your computer and use it in GitHub Desktop.
/**
* 標本に対しての平均値・標準偏差・偏差値を返す
* Returns the mean, standard deviation, and deviation for a sample
* @author doraiso
*
*/
public class DiviasionValue {
/**
* 正規化した場合の標準偏差 Standard deviation when normalized
*/
private int STANDARD_SCALE = 10;
/**
* 正規化した場合の平均 Normalized mean
*/
private int STANDARD_MEAN = 50;
public static void main(String[] args) {
// 標本のすべての値 All values of the sample
int[] scores = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
// 入力値(得点) Input value (score)
int score = 20;
DiviasionValue dv = new DiviasionValue();
System.out.println("平均:" + dv.getMean(scores)); // Mean
System.out.println("標準偏差:"+ dv.getSD(scores)); // SD
System.out.println("得点:" + score); // Score
System.out.println("偏差値:" + dv.getStandardScore(dv.getMean(scores),dv.getSD(scores),score)); //Standard Score
}
/**
* 平均を求める return mean
* @param scores 標本のすべての値 All values of the sample
* @return 標本の平均値を返す return mean
*/
private float getMean(int[] scores) {
// 合計 Σx
// scoresがx1,x2,..,xi,..,xn(scoresの数はn個)とした場合の合計
int sum = 0;
for (int i : scores) {
sum += i;
}
// 平均 μ = Σx / n
float mean = sum / scores.length;
return mean;
}
/**
* 標準偏差を求める return SD
* @param scores 標本のすべての値 All values of the sample
* @return 標本の標準偏差を返す return SD
*/
private float getSD(int[] scores) {
// 分散 σ^2 = Σ(xi - μx) ^ 2 distribution
float variance = 0F;
for (int i : scores) {
variance += Math.pow(i - getMean(scores), 2);
}
// 標準偏差 σ = sqrt((σ^2) / n) standard deviation
float standardDiviasion = (float) Math.sqrt(variance / scores.length);
return standardDiviasion;
}
/**
* 偏差値を求める return Deviation
* @param mean 標本の平均値(μ)
* @param sd 標本の標準偏差(σ)
* @param x 値(得点)
* @return 値(得点)に対しての偏差値を返す return Deviation
*/
private float getStandardScore(float mean,float sd,float x) {
// 標本の平均値μ、標準偏差σを正規化する Normalize the mean μ and standard deviation σ of the sample
// 正規化では平均値を50,標準偏差を10とする For normalization, the mean is 50 and the standard deviation is 10.
// T = 10(x - μ) / σ + 50
return (STANDARD_SCALE * (x - mean) / sd) + STANDARD_MEAN;
}
}
@doraiso
Copy link
Author

doraiso commented Mar 12, 2022

久しぶりに見ただけ

@doraiso
Copy link
Author

doraiso commented Mar 12, 2022

コメント直した

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment