Skip to content

Commit

Permalink
MONDRIAN: Add unit tests (disabled) for LinRegXxx functions.
Browse files Browse the repository at this point in the history
 Fix a couple of unit tests

[git-p4: depot-paths = "//open/mondrian/": change = 3140]
  • Loading branch information
julianhyde committed Feb 4, 2005
1 parent a991d79 commit a21a727
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 37 deletions.
4 changes: 2 additions & 2 deletions demo/FoodMart.xml
Expand Up @@ -202,10 +202,10 @@ lname
<CalculatedMember
name="Profit last Period"
dimension="Measures"
formula="([Measures].[Profit], ParallelPeriod())"
formula="COALESCEEMPTY((Measures.[Profit], [Time].PREVMEMBER), Measures.[Profit])"
visible="false"/>
<CalculatedMember
name="Profit growth"
name="Profit Growth"
dimension="Measures"
formula="([Measures].[Profit] - [Measures].[Profit last Period]) / [Measures].[Profit last Period]"
visible="true">
Expand Down
228 changes: 197 additions & 31 deletions testsrc/main/mondrian/olap/fun/FunctionTest.java
Expand Up @@ -3306,28 +3306,6 @@ public void testStrToMember() {
Assert.assertEquals("4", s);
}

/*
LinRegPoint(
<<numeric exprssion>>,
<<set>>,
<<y numeric exprssion>>
[, <<x numeric exprssion>>]
)
With member
[Measures].[Test] as
'LinRegPoint(
Rank(Time.CurrentMember, Time.CurrentMember.Level.Members) ,
Descendants([Time].[All Periods].[2004], [Time].[Month]),
[Measures].[Wholesaler 867 Shipped Qty],
Rank(Time.CurrentMember, Time.CurrentMember.Level.Members)
)'
Select
{[Measures].[Test]} ON ROWS,
{[Time].[All Periods].[2004]} ON COLUMNS
from
[Wholesaler Shipments]
*/

/**
* Executes a scalar expression, and asserts that the result is as
* expected. For example, <code>assertExprReturns("1 + 2", "3")</code>
Expand Down Expand Up @@ -3402,14 +3380,12 @@ public void testRank() {
}

public void testLinRegPointQuarter() {
String query = "WITH MEMBER " + nl +
"[Measures].[Test] as " + nl +
"'LinRegPoint(" + nl +
"Rank(Time.CurrentMember, Time.CurrentMember.Level.Members) ," + nl +
"Descendants([Time].[1997], [Time].[Quarter]), " + nl +
"[Measures].[Store Sales], " + nl +
"Rank(Time.CurrentMember, Time.CurrentMember.Level.Members)" + nl +
")' " + nl +
String query = "WITH MEMBER [Measures].[Test] as " + nl +
" 'LinRegPoint(" + nl +
" Rank(Time.CurrentMember, Time.CurrentMember.Level.Members)," + nl +
" Descendants([Time].[1997], [Time].[Quarter]), " + nl +
"[Measures].[Store Sales], " + nl +
" Rank(Time.CurrentMember, Time.CurrentMember.Level.Members))' " + nl +
"SELECT " + nl +
"{[Measures].[Test],[Measures].[Store Sales]} ON ROWS, " + nl +
"{[Time].[1997].Children} ON COLUMNS " + nl +
Expand All @@ -3436,7 +3412,80 @@ public void testLinRegPointQuarter() {
runQueryCheckResult(query, expected);
}

public void testLinRegPointMonth() {
/**
* Tests all of the linear regression functions, as suggested by
* <a href="http://support.microsoft.com/kb/q307276/">a Microsoft knowledge
* base article</a>.
*/
public void _testLinRegAll() {
// We have not implemented the LastPeriods function, so we use
// [Time].CurrentMember.Lag(9) : [Time].CurrentMember
// is equivalent to
// LastPeriods(10)
String query = "WITH MEMBER " + nl +
"[Measures].[Intercept] AS " + nl +
" 'LinRegIntercept([Time].CurrentMember.Lag(10) : [Time].CurrentMember, [Measures].[Unit Sales], [Measures].[Store Sales])' " + nl +
"MEMBER [Measures].[Regression Slope] AS" + nl +
" 'LinRegSlope([Time].CurrentMember.Lag(9) : [Time].CurrentMember,[Measures].[Unit Sales],[Measures].[Store Sales]) '" + nl +
"MEMBER [Measures].[Predict] AS" + nl +
" 'LinRegPoint([Measures].[Unit Sales],[Time].CurrentMember.Lag(9) : [Time].CurrentMember,[Measures].[Unit Sales],[Measures].[Store Sales])'," + nl +
" FORMAT_STRING = 'Standard' " + nl +
"MEMBER [Measures].[Predict Formula] AS" + nl +
" '([Measures].[Regression Slope] * [Measures].[Unit Sales]) + [Measures].[Intercept]'," + nl +
" FORMAT_STRING='Standard'" + nl +
"MEMBER [Measures].[Good Fit] AS" + nl +
" 'LinRegR2([Time].CurrentMember.Lag(9) : [Time].CurrentMember, [Measures].[Unit Sales],[Measures].[Store Sales])'," + nl +
" FORMAT_STRING='#,#.00'" + nl +
"MEMBER [Measures].[Variance] AS" + nl +
" 'LinRegVariance([Time].CurrentMember.Lag(9) : [Time].CurrentMember,[Measures].[Unit Sales],[Measures].[Store Sales])'" + nl +
"SELECT " + nl +
" {[Measures].[Store Sales], " + nl +
" [Measures].[Intercept], " + nl +
" [Measures].[Regression Slope], " + nl +
" [Measures].[Predict], " + nl +
" [Measures].[Predict Formula], " + nl +
" [Measures].[Good Fit], " + nl +
" [Measures].[Variance] } ON COLUMNS, " + nl +
" Descendants([Time].[1997], [Time].[Month]) ON ROWS" + nl +
"FROM Sales";

String expected = "Axis #0:" + nl +
"{}" + nl +
"Axis #1:" + nl +
"{[Measures].[Store Sales]}" + nl +
"{[Measures].[Intercept]}" + nl +
"{[Measures].[Regression Slope]}" + nl +
"{[Measures].[Predict]}" + nl +
"{[Measures].[Predict Formula]}" + nl +
"{[Measures].[Good Fit]}" + nl +
"{[Measures].[Variance]}" + nl +
"Axis #2:" + nl +
"{[Time].[1997].[Q1].[1]}" + nl +
"{[Time].[1997].[Q1].[2]}" + nl +
"{[Time].[1997].[Q1].[3]}" + nl +
"{[Time].[1997].[Q2].[4]}" + nl +
"{[Time].[1997].[Q2].[5]}" + nl +
"{[Time].[1997].[Q2].[6]}" + nl +
"{[Time].[1997].[Q3].[7]}" + nl +
"{[Time].[1997].[Q3].[8]}" + nl +
"{[Time].[1997].[Q3].[9]}" + nl +
"{[Time].[1997].[Q4].[10]}" + nl +
"{[Time].[1997].[Q4].[11]}" + nl +
"{[Time].[1997].[Q4].[12]}" + nl +
"Row #0: 45,539.69" + nl +
"Row #0: 68711.40" + nl +
"Row #0: -1.033" + nl +
"Row #0: 46,350.26" + nl +
"Row #0: 46.350.26" + nl +
"Row #0: -1.#INF" + nl +
"Row #0: 5.17E-08" + nl +
"..." + nl +
"Row #11: 15343.67" + nl;

runQueryCheckResult(query, expected);
}

public void _testLinRegPointMonth() {
String query = "WITH MEMBER " + nl +
"[Measures].[Test] as " + nl +
" 'LinRegPoint(" + nl +
Expand Down Expand Up @@ -3495,4 +3544,121 @@ public void testLinRegPointMonth() {

runQueryCheckResult(query, expected);
}

public void _testLinRegIntercept() {
assertExprReturns("LinRegIntercept([Time].[Month].members," +
" [Measures].[Unit Sales], [Measures].[Store Sales])",
"-126.65");

// empty set
assertExprReturns("LinRegIntercept({[Time].Parent}," +
" [Measures].[Unit Sales], [Measures].[Store Sales])",
"-1.#IND"); // MSAS returns -1.#IND (whatever that means)

// first expr constant
assertExprReturns("LinRegIntercept([Time].[Month].members," +
" 7, [Measures].[Store Sales])",
"$7.00");

// second expr constant
assertExprReturns("LinRegIntercept([Time].[Month].members," +
" [Measures].[Unit Sales], 4)",
"-1.#IND"); // MSAS returns -1.#IND (whatever that means)
}

public void _testLinRegSlope() {
assertExprReturns("LinRegSlope([Time].[Month].members," +
" [Measures].[Unit Sales], [Measures].[Store Sales])",
"0.4746");

// empty set
assertExprReturns("LinRegSlope({[Time].Parent}," +
" [Measures].[Unit Sales], [Measures].[Store Sales])",
"-1.#IND"); // MSAS returns -1.#IND (whatever that means)

// first expr constant
assertExprReturns("LinRegSlope([Time].[Month].members," +
" 7, [Measures].[Store Sales])",
"$7.00");

// second expr constant
assertExprReturns("LinRegSlope([Time].[Month].members," +
" [Measures].[Unit Sales], 4)",
"-1.#IND"); // MSAS returns -1.#IND (whatever that means)
}

public void _testLinRegPoint() {
assertExprReturns("LinRegPoint([Measures].[Unit Sales]," +
" [Time].CurrentMember[Time].[Month].members," +
" [Measures].[Unit Sales], [Measures].[Store Sales])",
"0.4746");

// empty set
assertExprReturns("LinRegPoint([Measures].[Unit Sales]," +
" {[Time].Parent}," +
" [Measures].[Unit Sales], [Measures].[Store Sales])",
"-1.#IND"); // MSAS returns -1.#IND (whatever that means)

// zeroth expr constant
assertExprReturns("LinRegPoint(-1," +
" [Time].[Month].members," +
" 7, [Measures].[Store Sales])",
"-127.124");

// first expr constant
assertExprReturns("LinRegPoint([Measures].[Unit Sales]," +
" [Time].[Month].members," +
" 7, [Measures].[Store Sales])",
"$7.00");

// second expr constant
assertExprReturns("LinRegPoint([Measures].[Unit Sales]," +
" [Time].[Month].members," +
" [Measures].[Unit Sales], 4)",
"-1.#IND"); // MSAS returns -1.#IND (whatever that means)
}

public void _testLinRegR2() {
assertExprReturns("LinRegR2([Time].[Month].members," +
" [Measures].[Unit Sales], [Measures].[Store Sales])",
"0.4746");

// empty set
assertExprReturns("LinRegR2({[Time].Parent}," +
" [Measures].[Unit Sales], [Measures].[Store Sales])",
"-1.#IND"); // MSAS returns -1.#IND (whatever that means)

// first expr constant
assertExprReturns("LinRegR2([Time].[Month].members," +
" 7, [Measures].[Store Sales])",
"$7.00");

// second expr constant
assertExprReturns("LinRegR2([Time].[Month].members," +
" [Measures].[Unit Sales], 4)",
"-1.#IND"); // MSAS returns -1.#IND (whatever that means)
}

public void _testLinRegVariance() {
assertExprReturns("LinRegVariance([Time].[Month].members," +
" [Measures].[Unit Sales], [Measures].[Store Sales])",
"0.4746");

// empty set
assertExprReturns("LinRegVariance({[Time].Parent}," +
" [Measures].[Unit Sales], [Measures].[Store Sales])",
"-1.#IND"); // MSAS returns -1.#IND (whatever that means)

// first expr constant
assertExprReturns("LinRegVariance([Time].[Month].members," +
" 7, [Measures].[Store Sales])",
"$7.00");

// second expr constant
assertExprReturns("LinRegVariance([Time].[Month].members," +
" [Measures].[Unit Sales], 4)",
"-1.#IND"); // MSAS returns -1.#IND (whatever that means)
}
}

// End FunctionTest.java
10 changes: 6 additions & 4 deletions testsrc/main/mondrian/test/TestCalculatedMembers.java
Expand Up @@ -3,7 +3,7 @@
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// Copyright (C) 2002-2004 Kana Software, Inc. and others.
// Copyright (C) 2002-2005 Kana Software, Inc. and others.
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
//
Expand Down Expand Up @@ -152,16 +152,18 @@ private Cube getSalesCube(String cubeName) {
}

public void testCalculatedMemberInCubeAndQuery() {
runQueryCheckResult("WITH MEMBER [Measures].[Profit Growth]" + nl +
// Profit is defined in the cube.
// Profit Change is defined in the query.
runQueryCheckResult("WITH MEMBER [Measures].[Profit Change]" + nl +
" AS '[Measures].[Profit] - ([Measures].[Profit], [Time].PrevMember)'" + nl +
"SELECT {[Measures].[Profit], [Measures].[Profit Growth]} ON COLUMNS," + nl +
"SELECT {[Measures].[Profit], [Measures].[Profit Change]} ON COLUMNS," + nl +
" {[Time].[1997].[Q2].children} ON ROWS" + nl +
"FROM [Sales]",
"Axis #0:" + nl +
"{}" + nl +
"Axis #1:" + nl +
"{[Measures].[Profit]}" + nl +
"{[Measures].[Profit Growth]}" + nl +
"{[Measures].[Profit Change]}" + nl +
"Axis #2:" + nl +
"{[Time].[1997].[Q2].[4]}" + nl +
"{[Time].[1997].[Q2].[5]}" + nl +
Expand Down

0 comments on commit a21a727

Please sign in to comment.