I'm having an issue with the following code. The code works with no errors but I'm receiving different output values when using a parallel for loop vs a regular for loop. I need to get the parallel for loop working properly because I run this code thousands of times. Does anyone know why my parallel for loop is returning different outputs?
private object _lock = new object();
public double CalculatePredictedRSquared()
{
double press = 0, tss = 0, press2 = 0, press1 = 0;
Vector<double> output = CreateVector.Dense(Enumerable.Range(0, 400).Select(i => Convert.ToDouble(i)).ToArray());
List<double> input1 = new List<double>(Enumerable.Range(0, 400).Select(i => Convert.ToDouble(i)));
List<double> input2 = new List<double>(Enumerable.Range(200, 400).Select(i => Convert.ToDouble(i)));
Parallel.For(0, output.Count, i =>
{
ConcurrentBag<MultipleRegressionInfo> listMRInfoBag = new ConcurrentBag<MultipleRegressionInfo>(listMRInfo);
ConcurrentBag<double> vectorArrayBag = new ConcurrentBag<double>(output);
ConcurrentBag<double[]> matrixList = new ConcurrentBag<double[]>();
lock (_lock)
{
matrixList.Add(input1.Where((v, k) => k != i).ToArray());
matrixList.Add(input2.Where((v, k) => k != i).ToArray());
}
var matrixArray2 = CreateMatrix.DenseOfColumnArrays(matrixList);
var actualResult = vectorArrayBag.ElementAt(i);
var newVectorArray = CreateVector.Dense(vectorArrayBag.Where((v, j) => j != i).ToArray());
var items = FindBestMRSolution(matrixArray2, newVectorArray);
double estimate1 = 0;
if (items != null)
{
lock (_lock)
{
var y = 0d;
var independentCount = matrixArray2.RowCount;
var dependentCount = newVectorArray.Count;
if (independentCount == dependentCount)
{
var populationCount = independentCount;
y = newVectorArray.Average();
for (int l = 0; l < matrixArray2.ColumnCount; l++)
{
var avg = matrixArray2.Column(l).Average();
y -= avg * items[l];
}
}
for (int m = 0; m < 2; m++)
{
var coefficient = items[m];
if (m == 0)
{
estimate1 += input1.ElementAt(i) * coefficient;
}
else
{
estimate1 += input2.ElementAt(i) * coefficient;
}
}
estimate1 += y;
}
}
else
{
lock (_lock)
{
estimate1 = 0;
}
}
lock (_lock)
{
press1 += Math.Pow(actualResult - estimate1, 2);
}
});
for (int i = 0; i < output.Count; i++)
{
List<double[]> matrixList = new List<double[]>();
matrixList.Add(input1.Where((v, k) => k != i).ToArray());
matrixList.Add(input2.Where((v, k) => k != i).ToArray());
var matrixArray = CreateMatrix.DenseOfColumnArrays(matrixList);
var actualResult = output.ElementAt(i);
var newVectorArray = CreateVector.Dense(output.Where((v, j) => j != i).ToArray());
var items = FindBestMRSolution(matrixArray, newVectorArray);
double estimate = 0;
if (items != null)
{
var y = CalculateYIntercept(matrixArray, newVectorArray, items);
for (int m = 0; m < 2; m++)
{
var coefficient = items[m];
if (m == 0)
{
estimate += input1.ElementAt(i) * coefficient;
}
else
{
estimate += input2.ElementAt(i) * coefficient;
}
}
}
else
{
estimate = 0;
}
press2 += Math.Pow(actualResult - estimate, 2);
}
tss = CalculateTotalSumOfSquares(vectorArray.ToList());
var test1 = 1 - (press1 / tss);
var test2 = 1 - (press2 / tss);
}
public Vector<double> CalculateWithQR(Matrix<double> x, Vector<double> y)
{
Vector<double> result = null;
result = MultipleRegression.QR(x, y);
for (int i = 0; i < result.Count; i++)
{
var value = result.ElementAt(i);
if (Double.IsNaN(value) || Double.IsInfinity(value))
{
return null;
}
}
return result;
}
public Vector<double> CalculateWithNormal(Matrix<double> x, Vector<double> y)
{
Vector<double> result = null;
result = MultipleRegression.NormalEquations(x, y);
for (int i = 0; i < result.Count; i++)
{
var value = result.ElementAt(i);
if (Double.IsNaN(value) || Double.IsInfinity(value))
{
return null;
}
}
return result;
}
public Vector<double> CalculateWithSVD(Matrix<double> x, Vector<double> y)
{
Vector<double> result = null;
result = MultipleRegression.Svd(x, y);
for (int i = 0; i < result.Count; i++)
{
var value = result.ElementAt(i);
if (Double.IsNaN(value) || Double.IsInfinity(value))
{
return null;
}
}
return result;
}
public Vector<double> FindBestMRSolution(Matrix<double> x, Vector<double> y)
{
Vector<double> result = null;
result = CalculateWithNormal(x, y);
if (result != null)
{
return result;
}
else
{
result = CalculateWithSVD(x, y);
if (result != null)
{
return result;
}
else
{
result = CalculateWithQR(x, y);
if (result != null)
{
return result;
}
}
}
return result;
}
public double CalculateTotalSumOfSquares(List<double> dependentVariables)
{
double tts = 0;
for (int i = 0; i < dependentVariables.Count; i++)
{
tts += Math.Pow(dependentVariables.ElementAt(i) - dependentVariables.Average(), 2);
}
return tts;
}
Actual Output (Updated results):
test1 = 137431.12889999992 (parallel for loop)
test2 = 7.3770258447689254E- (regular for loop)
Epilogue: How to setup an MCVE-compliant testing
This may be a fair way to prepare an indeed fully reproducible setup of an MCVE-code + A/B/C/... DataSET
-s, put inside a ready-to-run [IDE & Testing Sandbox, hyperlinked here][1], so that Community members can click a re-run button and focus on root-cause analysis, not on decoding and re-engineering the heaps of incomplete SLOCs.
If this runs for the O/P, it will run for other Community Members, whom the O/P has asked for an answer or help.
My new version of the code:
public double CalculatePredictedRSquared()
{
Vector<double> output = CreateVector.Dense(Enumerable.Range(0, 400).Select(i => Convert.ToDouble(i)).ToArray());
List<double> input1 = new List<double>(Enumerable.Range(0, 400).Select(i => Convert.ToDouble(i)));
List<double> input2 = new List<double>(Enumerable.Range(200, 400).Select(i => Convert.ToDouble(i)));
double tss = CalculateTotalSumOfSquares(output.ToList());
IEnumerable<int> range = Enumerable.Range(0, output.Count);
var query = range.Select(i => DoIt(i, output, input1, input2));
var result = 1 - (query.Sum() / tss);
return result;
}
public double DoIt(int i, Vector<double> output, List<double> input1, List<double> input2)
{
List<double[]> matrixList = new List<double[]>
{
input1.Where((v, k) => k != i).ToArray(),
input2.Where((v, k) => k != i).ToArray()
};
var matrixArray = CreateMatrix.DenseOfColumnArrays(matrixList);
var actualResult = output.ElementAt(i);
var newVectorArray = CreateVector.Dense(output.Where((v, j) => j != i).ToArray());
var items = FindBestMRSolution(matrixArray, newVectorArray);
double estimate = 0;
if (items != null)
{
var y = CalculateYIntercept(matrixArray, newVectorArray, items);
for (int m = 0; m < 2; m++)
{
var coefficient = items[m];
if (m == 0)
{
estimate += input1.ElementAt(i) * coefficient;
}
else
{
estimate += input2.ElementAt(i) * coefficient;
}
}
}
else
{
estimate = 0;
}
return Math.Pow(actualResult - estimate, 2);
}