Programming Test QR Team Developed Suite Analytical Models

Programming Test QR Team Developed Suite Analytical Models

QR team has developed a suite of analytical models that take 1-n variables as input. Say alpha (a), beta (b), gamma (g), delta (d), rho (‘r). All are double values.

Example:

  • QR_model_2(double a, double b)
  • QR_model_3(double a, double b, double c)
  • QR_model_4(double a, double b, double c, double d) … etc…

In order to run a particular model through a series of regressions, we are supplied with a test vector of values for each of the input parameters.
Example:

  • alpha = 1.1 1.2 1.3
  • beta = 2.1, 2.2, 2.3, 2.4, 2.5
  • gamma = 3.1, 3.2, 3.3
  • delta = 0.1, 0.2
  • rho = 5, 6, 7, 8, 9

Test vectors have the same cardinality as the model under test. Example: when testing QR_model_2, the test vector will comprise 2 items (alpha and beta), when testing QR_model_4, 4 test vectors will be provided.

We need a helper function that can take these vectors and create inputs that can be passed into the function such that the function is tested with each of the variations of the inputs. For example, given the above i.e a 5 argument model, the output of the helper function would be:
1.1, 2.1, 3.1, 0.1, 5
1.1, 2.1, 3.1, 0.2, 5 … etc …

Assume that you can be supplied the input arguments in a data structure of your choice. The results need to be some sort of data structure that can be used to extract the parameters for each run of QR_model_N. You can use Java, Python or C++ to implement your solution. Please include modest build instructions and ideally the results of a test run such as a simple main program.

This assignment typically takes 1.5 hours

Answer: Above problem looks very complex when you read first time but if you read four five times I believe you will understand what to do. Below is java class solution of above problem:

I have used Consumer interface from Java 8 which accept only parameter which list of double array value as below with main logic of iterating list which has double array inside it:

  • QRTeamModelSimpleConsumer.java:
package com.test;

import java.util.List;
import java.util.function.Consumer;

public class QRTeamModelSimpleConsumer implements Consumer<List<double []>>{

	@Override
	public void accept(List<double[]> qrModelList) {
		
		System.out.println("QR_Model_"+qrModelList.size()+" cardinality");
		
		int[] positions = new int[qrModelList.size()];
	    boolean another = true;	    
	    
	    while (another) {
	        for (int n = 0; n < qrModelList.size(); n++) {
	            System.out.print(qrModelList.get(n)[positions[n]]+" ");
	        }
	        System.out.println();

	        another = false;
	        
	        for (int c = 0; c < qrModelList.size(); c++) {
	            positions[c]++;
	            if (positions[c] < qrModelList.get(c).length) {
	                another = true;
	                break;
	            }
	            positions[c] = 0;
	        }
	    }
		
	}

}
  • Now write main java program which will use this consumer where we could pass values dynamically:

QRTeamModelSimple.java:

package com.test;

import java.util.ArrayList;
import java.util.List;

public class QRTeamModelSimple {

	public static void main(String[] args) {
		
		double [] alphaArray = {1.1, 1.2, 1.3};
		double [] betaArray = {2.1, 2.2, 2.3, 2.4, 2.5};
		double [] gammaArray = {3.1, 3.2, 3.3};
		double [] deltaArray = {0.1, 0.2};
		double [] rhoArray = {5, 6, 7, 8, 9};
		double [] rhoArray2= {5, 6, 7, 8, 9};
		
		List<double []> qrModelList = new ArrayList<double[]>();
		
		qrModelList.add(alphaArray);
		qrModelList.add(betaArray);
		qrModelList.add(gammaArray);
		qrModelList.add(deltaArray);
		qrModelList.add(rhoArray);
		qrModelList.add(rhoArray2);
		
		QRTeamModelSimpleConsumer qrTeamModelSimpleConsumer = new QRTeamModelSimpleConsumer();
		qrTeamModelSimpleConsumer.accept(qrModelList);
		
	}	

}

Note: In above program you could add or delete as many as double array value you want and as its asked in question. I passed six double array values to consumer and below is output:

  • Output:
QR_Model_6 cardinality
1.1 2.1 3.1 0.1 5.0 5.0 
1.2 2.1 3.1 0.1 5.0 5.0 
1.3 2.1 3.1 0.1 5.0 5.0 
1.1 2.2 3.1 0.1 5.0 5.0 
1.2 2.2 3.1 0.1 5.0 5.0 
1.3 2.2 3.1 0.1 5.0 5.0 
1.1 2.3 3.1 0.1 5.0 5.0 
1.2 2.3 3.1 0.1 5.0 5.0 
1.3 2.3 3.1 0.1 5.0 5.0 
1.1 2.4 3.1 0.1 5.0 5.0 
1.2 2.4 3.1 0.1 5.0 5.0 
1.3 2.4 3.1 0.1 5.0 5.0 
1.1 2.5 3.1 0.1 5.0 5.0 
1.2 2.5 3.1 0.1 5.0 5.0 
1.3 2.5 3.1 0.1 5.0 5.0 
1.1 2.1 3.2 0.1 5.0 5.0 ..............

Reference:

Leave a Reply

Your email address will not be published. Required fields are marked *