public static Value Variance(MatrixValue M) { if (M.Length == 0) { return(ScalarValue.Zero); } if (M.IsVector) { var variance = ScalarValue.Zero; var mean = M.Sum() / M.Length; for (int i = 1; i <= M.Length; i++) { variance += (M[i] - mean).Square(); } return(variance / M.Length); } var avg = (MatrixValue)YMath.Average(M); var scale = 1.0; var s = new MatrixValue(1, M.DimensionX); for (var i = 1; i <= M.DimensionY; i++) { for (int j = 1; j <= M.DimensionX; j++) { s[1, j] += (M[i, j] - avg[j]).Square(); } } scale /= M.DimensionY; for (var i = 1; i <= s.DimensionY; i++) { for (int j = 1; j <= s.DimensionX; j++) { s[i, j] *= scale; } } return(s); }
public Value Function(MatrixValue M) { return(YMath.Average(M)); }
public MatrixValue Function(MatrixValue cfgs, ScalarValue n, FunctionValue f, ArgumentsValue P) { var numberOfBootstrapSamples = n.GetIntegerOrThrowException("n", Name); var nConfigs = cfgs.DimensionY; var nData = cfgs.DimensionX; var distribution = new DiscreteUniformDistribution(Rng) { Beta = nConfigs, Alpha = 1 }; if (numberOfBootstrapSamples <= 1) { throw new YAMPException("Bootstrap: The number of bootstrap samples n is smaller or equal to 1!"); } var parameters = new ArgumentsValue(cfgs); foreach (var m in P.Values) { parameters.Insert(m); } var temp = f.Perform(Context, parameters); var nResult = 0;//dimension of the result if (temp is ScalarValue) { nResult = 1; } else if (temp is MatrixValue) { nResult = ((MatrixValue)temp).Length; } else { throw new YAMPException("Bootstrap: The observable f has to return either a scalar or a matrix!"); } var BootstrapObservable = new MatrixValue(numberOfBootstrapSamples, nResult); for (var i = 1; i <= numberOfBootstrapSamples; i++) { var BootstrapConfigs = new MatrixValue(nConfigs, nData); for (var j = 1; j <= nConfigs; j++) { var idx = distribution.Next(); for (var k = 1; k <= nData; k++) { BootstrapConfigs[j, k] = cfgs[idx, k]; } } parameters = new ArgumentsValue(BootstrapConfigs); foreach (var m in P.Values) { parameters.Insert(m); } temp = f.Perform(Context, parameters); if (temp is ScalarValue) { BootstrapObservable[i] = (ScalarValue)temp; } else { var m = (MatrixValue)temp; for (var k = 1; k <= nResult; k++) { BootstrapObservable[i, k] = m[k]; } } } temp = YMath.Average(BootstrapObservable); for (var i = 1; i <= numberOfBootstrapSamples; i++) { if (temp is ScalarValue) { BootstrapObservable[i] -= temp as ScalarValue; BootstrapObservable[i] *= BootstrapObservable[i]; } else { var T = temp as MatrixValue; for (var k = 1; k <= nResult; k++) { BootstrapObservable[i, k] -= T[k]; BootstrapObservable[i, k] *= BootstrapObservable[i, k]; } } } var error = YMath.Average(BootstrapObservable); var sqrt = new SqrtFunction(); error = sqrt.Perform(error); var result = new MatrixValue(2, nResult); if (temp is ScalarValue) { result[1] = (ScalarValue)temp; result[2] = (ScalarValue)error; } else { var T = (MatrixValue)temp; var E = (MatrixValue)error; for (var k = 1; k <= nResult; k++) { result[1, k] = T[k]; result[2, k] = E[k]; } } return(result); }
public MatrixValue Function(MatrixValue cfgs, ScalarValue n, FunctionValue f, ArgumentsValue P) { var numberOfBlocks = n.GetIntegerOrThrowException("n", Name); var nConfigs = cfgs.DimensionY; var nData = cfgs.DimensionX; if (numberOfBlocks > nConfigs) { throw new YAMPException("Jackknife: The number of measurements n is greater than the number of configurations cfgs!"); } if (numberOfBlocks <= 1) { throw new YAMPException("Jackknife: The number of measurements n <= 1!"); } var parameters = new ArgumentsValue(cfgs); foreach (var m in P.Values) { parameters.Insert(m); } var temp = f.Perform(Context, parameters); int nResult;//dimension of the result if (temp is ScalarValue) { nResult = 1; } else if (temp is MatrixValue) { nResult = ((MatrixValue)temp).Length; } else { throw new YAMPException("Jackknife: Observable f has to return either a scalar or a matrix!"); } var JackknifeObservable = new MatrixValue(numberOfBlocks, nResult); var BlockSize = nConfigs / numberOfBlocks; var nConfigsBlocked = BlockSize * numberOfBlocks; var residualConfigs = nConfigs - nConfigsBlocked; for (var i = 1; i <= numberOfBlocks; i++) { if (i <= numberOfBlocks - residualConfigs) { //the first (NumberOfBlocks - residualConfigs) blocks discard (BlockSize) elements ... var JackknifeConfigs = new MatrixValue(nConfigs - BlockSize, nData); var j = 1; for (; j <= (i - 1) * BlockSize; j++) { for (var k = 1; k <= nData; k++) { JackknifeConfigs[j, k] = cfgs[j, k]; } } j += BlockSize; for (; j <= nConfigs; j++) { for (var k = 1; k <= nData; k++) { JackknifeConfigs[j - BlockSize, k] = cfgs[j, k]; } } parameters = new ArgumentsValue(JackknifeConfigs); } else { //... whereas the residual (residualConfigs) blocks discard (BlockSize + 1) elements var JackknifeConfigs = new MatrixValue(nConfigs - BlockSize - 1, nData); var j = 1; for (; j <= nConfigs - (numberOfBlocks - (i - 1)) * (BlockSize + 1); j++) { for (var k = 1; k <= nData; k++) { JackknifeConfigs[j, k] = cfgs[j, k]; } } j += BlockSize + 1; for (; j <= nConfigs; j++) { for (var k = 1; k <= nData; k++) { JackknifeConfigs[j - BlockSize - 1, k] = cfgs[j, k]; } } parameters = new ArgumentsValue(JackknifeConfigs); } foreach (var m in P.Values) { parameters.Insert(m); } temp = f.Perform(Context, parameters); if (temp is ScalarValue) { JackknifeObservable[i] = (ScalarValue)temp; } else { var T = (MatrixValue)temp; for (var k = 1; k <= nResult; k++) { JackknifeObservable[i, k] = T[k]; } } } temp = YMath.Average(JackknifeObservable); for (var i = 1; i <= numberOfBlocks; i++) { if (temp is ScalarValue) { JackknifeObservable[i] -= temp as ScalarValue; JackknifeObservable[i] *= JackknifeObservable[i]; } else { var m = (MatrixValue)temp; for (var k = 1; k <= nResult; k++) { JackknifeObservable[i, k] -= m[k]; JackknifeObservable[i, k] *= JackknifeObservable[i, k]; } } } var error = YMath.Average(JackknifeObservable); var scale = numberOfBlocks - 1.0; if (error is ScalarValue) { error = ((ScalarValue)error) * scale; } else { var e = (MatrixValue)error; for (var i = 1; i <= e.DimensionY; i++) { for (var j = 1; j <= e.DimensionX; j++) { e[i, j] *= scale; } } } var sqrt = new SqrtFunction(); error = sqrt.Perform(error); var result = new MatrixValue(2, nResult); if (temp is ScalarValue) { result[1] = (ScalarValue)temp; result[2] = (ScalarValue)error; } else { var T = (MatrixValue)temp; var E = (MatrixValue)error; for (var k = 1; k <= nResult; k++) { result[1, k] = T[k]; result[2, k] = E[k]; } } return(result); }