Theil’s Inequality Algorithm using C#

TheilsInequality

public class TheilInEqualityAlgorithm
{
private readonly int _holdOutPeriod;
private readonly IEnumerable _actualAndPredictedValues;
public TheilInEqualityAlgorithm(int holdOutPeriod, IEnumerable actualAndPredictedValues)
{
_holdOutPeriod = holdOutPeriod;
_actualAndPredictedValues = actualAndPredictedValues;
}
private double CalculateRootMeanSquareDeviation()
{
double sum = 0;
foreach (ActualAndPredictedValue actualAndPredictedValue in _actualAndPredictedValues)
{
double difference = actualAndPredictedValue.Actual - actualAndPredictedValue.Predicted;
double squareOfdifference = Math.Pow(difference, 2);
sum = sum + squareOfdifference;
}

double rootMeanSquareDeviation = Math.Sqrt(sum / _holdOutPeriod);
return rootMeanSquareDeviation;
}
public Dictionary<string,double> CalculateTheilInEquality()
{
var results=new Dictionary<string, double>();
double calculatedRootMeanSquareDeviation = CalculateRootMeanSquareDeviation();
results.Add("RMSD",calculatedRootMeanSquareDeviation);
double actualSquareSum = 0;
double predictedSquareSum = 0;
foreach (ActualAndPredictedValue actualAndPredictedValue in _actualAndPredictedValues)
{
double actualSquare = Math.Pow(actualAndPredictedValue.Actual, 2);
double predictedSquare = Math.Pow(actualAndPredictedValue.Predicted, 2);
actualSquareSum = actualSquareSum + actualSquare;
predictedSquareSum = predictedSquareSum + predictedSquare;
}
double sqrtOfActualSum = Math.Sqrt(actualSquareSum / _holdOutPeriod);
double sqrtOfPredictedSum = Math.Sqrt(predictedSquareSum / _holdOutPeriod);
double theilInEquality = (calculatedRootMeanSquareDeviation) / (sqrtOfActualSum + sqrtOfPredictedSum);
results.Add("TheilInEquality", theilInEquality);
return results;
}
}
public class ActualAndPredictedValue
{
public double Actual { get; set; }
public double Predicted { get; set; }
}

Pass actual and predicted values for the periods

var actualPredictedCollection = new List
{
new ActualAndPredictedValue
{
Actual =-659505.150000,
Predicted =-900587.246911
}};
var results = new TheilInEqualityAlgorithm(12,actualPredictedCollection).CalculateTheilInEquality();
double theilInEquality=results[“TheilInEquality”];
Enjoy coding…

Alternative string concatenation of a two string in Javascript

function GetString(string1,string2) {
var str1 = string1.split('');
var str2 = string2.split('');

var finalString = "";
var length = str1.length > str2.length ? str2.length : str1.length;
for (var i = 0; i str2.length) {
var difference = str1.length - str2.length;
for (var i = 0; i < difference; i++) {
var rem = str1[str2.length + i];
finalString = finalString + rem;
}

} else {
var difference = str2.length - str1.length;
for (var i = 0; i < difference; i++) {
var rem = str2[str1.length + i];
finalString = finalString + rem;
}
}
return finalString;
}

var result= GetString(“INDIA”, “USA”);
Output : IUNSDAIA

Alternative string concatenation of a two string in C#

public static string GetString(string string1,string string2)
{
char[] str1 = string1.ToCharArray();
char[] str2 = string2.ToCharArray();

string finalString = “”;
int length = str1.Length > str2.Length ? str2.Length : str1.Length;
for (int i = 0; i < length ; i++)
{
finalString = finalString + str1[i] + str2[i];
}
if (str1.Length > str2.Length)
{
int difference = str1.Length – str2.Length;
for (int i = 0; i < difference; i++)
{
char rem = str1[str2.Length + i];
finalString = finalString + rem;
}

}
else
{
int difference = str2.Length – str1.Length;
for (int i = 0; i < difference; i++)
{
char rem = str2[str1.Length + i];
finalString = finalString + rem;
}
}
return finalString;
}

string finalString = GetString(“USA”, “INDIA”);

Ouput : UISNADIA

Split numbers at nth occurrence in C#

class Program
{
static void Main(string[] args)
{

var finalResult = GetSplittedNumbers("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",10);
}
public static List<string> GetSplittedNumbers(string stringSeparatedByComma,int n)
{
var listOfSeparatedValues = new List<string>();
List<int> splittedIntArrays =
stringSeparatedByComma.Split(',').Select(
x => Convert.ToInt32(x)).ToList();
var splittedList = new List<int>();
foreach (int item in splittedIntArrays)
{

int status = item % n;
if (splittedList == null)
{
splittedList = new List<int>();
}

splittedList.Add(item);

if (status == 0)
{

string res = AgainCommaSeparated(splittedList);
listOfSeparatedValues.Add(res);
splittedList = null;
}
}
return listOfSeparatedValues;
}
private static string AgainCommaSeparated(IEnumerable<int> numbers )
{
var result= numbers.Aggregate("", (current, number) => current + number.ToString() + ",");
return result.Substring(0, result.Length - 1);
}
}