Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Inverse Chi Squared Function
Old 06-12-2011, 04:21 AM Inverse Chi Squared Function
evans123's Avatar
Ultra Talker

Posts: 468
Trades: 0
Im working on a script that uses the bayesian theorem and the inverse chi squared function to retrieve the probability that any given text belongs in a specific category.

I've read up on the inverse chi squared function and i know it shouldnt give a negative value, but for some reason im getting negative values from the function.

PHP Code:
        $N 0;
        
$H $S 1;
        foreach(
$ngrams as $k => $v) {
            if ( !isset(
$knowledge[$k]) ) continue;

            
$N++;
            
$value $knowledge[$k] * $v
            
$H *= $value;
            
$S *= (float)( - ( ($value>=1) ? 0.99 $value) );
        } 
So after calculating the above the values are as follows:

H: 4
S: 1.0E-20
N = 10

These values are now passed into the inverse chi squared function with 2 degress of freedom.

PHP Code:
        $H $this->chi2Q( -log$N *  $H), $N);
        
$S = (float)$this->chi2Q( -log$N *  $S), $N); 
Inverse Chi Squared Function:
PHP Code:
    function chi2Q($x$v) {
        
$m $x 2;
        
        
$sum $term exp(-$m);

        for(
$i=1$i < ($v/2);$i++) {
            
$term *= $m/$i;
            
$sum += $term;
        }
        echo 
'Sum: '.$sum;
        return (
$sum 1.0) ? $sum 1.0;
    } 
The results from the function are as follows:

H: -2.8299816393601
S: 2.0228764578537E-10

Then going back to the original function we can calculate the probability that its in H or S.

PHP Code:
        $I = (( + ($H $S)) / 2) * 100;
        return 
is_finite($I) ? $I 100
Because H is alot smaller that S it causes the final probability to be -91.499081978121, obviously you shouldnt get an inverse number from a chi square distribution, so im not sure where its gone wrong. Maybe i haven't got enough information, and if so how would i go about detecting this?

Last edited by evans123; 06-12-2011 at 09:36 AM..
evans123 is offline
Reply With Quote
View Public Profile Visit evans123's homepage!
 
 
Register now for full access!
Old 06-12-2011, 09:18 AM Re: Inverse Chi Squared Function
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
S: 2.0228764578537E-10
isn't a negative number, just a tiny positive number ( 1<>0 )

0.000 000 000 20228764578537



<pedantic>BTW it should be Bayesian theorem </pedantic>
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 06-12-2011, 09:40 AM Re: Inverse Chi Squared Function
evans123's Avatar
Ultra Talker

Posts: 468
Trades: 0
Spelling mistake... Sorry.

The S is a postive number but because its so small when calculating the probability using the code below, the probability will be negative as H+S isn't enough to generate a postive number:

PHP Code:
        $I = (( + ($H $S)) / 2) * 100;
        return 
is_finite($I) ? $I 100


I know you can't get a negative number from the inverse chi squared function, but as it stands i do. I was just wondering if there were any limitations to using the function and whether any one has prior knowledge who might be able to sport any mistakes etc...

Last edited by evans123; 06-12-2011 at 09:42 AM..
evans123 is offline
Reply With Quote
View Public Profile Visit evans123's homepage!
 
Old 06-12-2011, 09:43 AM Re: Inverse Chi Squared Function
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Yes, but you do have $H - $S in your formulae.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 06-12-2011, 09:56 AM Re: Inverse Chi Squared Function
evans123's Avatar
Ultra Talker

Posts: 468
Trades: 0
Yeah, but surely both $H and $S should be positive numbers first.
evans123 is offline
Reply With Quote
View Public Profile Visit evans123's homepage!
 
Old 06-14-2011, 06:11 AM Re: Inverse Chi Squared Function
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
Originally Posted by evans123 View Post
Yeah, but surely both $H and $S should be positive numbers first.
Trying (and failing) to cast my mind back to college for statistical analysis lessons.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Inverse Chi Squared Function
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.23943 seconds with 12 queries