The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Origin C
 Inverse Student T
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

Jwelch1324

USA
Posts

Posted - 06/18/2007 :  5:47:47 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Version (Select Help-->About Origin): 7.5
Operating System: Windows

Ok, so i asked tech support if origin had an inverse student t function to which i was told use the invt(double x, double y) function.

However upon using the function i got a weird answer, so i attempted to implement an algorithm i found in a book called "C/C++ Mathematical Algorithms For Scientists and Engineers"

Needless to say, the answers were different, here is the output from origin for the two methods.

TInvTest(0.05,5);
TInv: 2.023695
invt: 0.518971


EDIT: I realized the invt function in origin takes an actual T value not a probability... still it doesn't explain the vast difference in my algorithm and the excel one...

Also is there a function in origin that takes a probability and degrees of freedom to give me the T value associated with that given probability.


Were TInv is My algorithm and invt is the origin one. Both are calculated with p = 0.05 and df = 5.

further more, becuase these were so far off i consulted excel which has a built in TInv function, and with the same parameters i got
2.5705


So... Here is my source code for the algorithm i put in there, if anyone sees any huge flaws please let me know. but given that i have gotten three very different answers from three different algorithms, i am just curious to know if anyone has any ideas as to what is going on ?

Thanks =).
Jon Welch

StudentT.c
/*------------------------------------------------------------------------------*
* File Name: *
* Creation: *
* Purpose: OriginC Source C file *
* Copyright (c) ABCD Corp. 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 *
* All Rights Reserved *
* *
* Modification Log: *
*------------------------------------------------------------------------------*/

////////////////////////////////////////////////////////////////////////////////////
// Including the system header file Origin.h should be sufficient for most Origin
// applications and is recommended. Origin.h includes many of the most common system
// header files and is automatically pre-compiled when Origin runs the first time.
// Programs including Origin.h subsequently compile much more quickly as long as
// the size and number of other included header files is minimized. All NAG header
// files are now included in Origin.h and no longer need be separately included.
//
// Right-click on the line below and select 'Open "Origin.h"' to open the Origin.h
// system header file.
#include <Origin.h>
#include <math.h>
#define SQR(x) ((x) * (x))
////////////////////////////////////////////////////////////////////////////////////
// Start your functions here.
double QInv(double x)
{
double result;
double sum1, sum2;
double tempo, xp;
int i;

double c[4] = {2.515517,0.802853,0.010328,0.0};
double d[4] = {1.0, 1.432788, 0.189269, 0.001308};

if(x<=0.0)
x = 0.0001;
else if(x>=1.0)
x = 0.9999;

if (x<0.5)
tempo = sqrt(log(1.0/SQR(x)));
else
tempo = sqrt(log(1.0/SQR(1.0-x)));


sum1 = 0.0;
sum2 = 0.0;
xp = 1.0;

for(i = 0;i<4;i++)
{
sum1 += c[i] * xp;
sum2 += d[i] * xp;
xp *= tempo;
}
result = tempo - sum1 / sum2;
result = (x > 0.5)? -result : result;
return result;
}

double TInv(double x, double df)
{
double sum, xp, xq;
double Pwr[10];
double term[5];
int i;

if(x <= 0.0)
x = 0.0001;
else if (x >= 1.0)
x = 0.9999;

xq = QInv(x);
Pwr[1] = xq;
for (i = 2;i<=9;i++)
Pwr[i] = Pwr[i-1] * xq;

term[1] = 0.25 * ( Pwr[3] + Pwr[1] );
term[2] = (5*Pwr[5] + 16*Pwr[3] + 3*Pwr[1])/96;
term[3] = (3*Pwr[7] + 19*Pwr[7] + 17*Pwr[3] - 15*Pwr[1])/384;
term[4] = (79*Pwr[9] + 776*Pwr[7] + 1482*Pwr[5] - 1920*Pwr[3] - 945*Pwr[1])/92160.0;

sum=xq;
xp = 1;
for(i = 1;i<=4;i++)
{
xp *=df;
sum += term[i] / xp;
}
return sum
}

/*
@param p = probability
@param df = degress of freedom
*/
void TInvTest(double p, double df)
{
printf("TInv: %f\n",TInv(p,df));
printf("invt: %f\n",invt(p,df));
}


Edited by - Jwelch1324 on 06/18/2007 5:53:50 PM
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000