WeBWorK Main Forum

using n choose k

using n choose k

by Joel Trussell -
Number of replies: 4
I"m writing a series of probability problems and would like to use the n choose k function C(n,k) but I get an error. the problem works if I use the factorial n!, k! etc.
can I use Cn,k) in this case?

DOCUMENT();

loadMacros(
"PGstandard.pl", # Standard macros for PG language
"MathObjects.pl",
#"source.pl", # allows code to be displayed on certain sites.
"PGcourse.pl", # Customization file for the course
"parserPopUp.pl",
"AnswerFormatHelp.pl",
"PGauxiliaryFunctions.pl",
);

# sampling rate problem with aliasing

TEXT(beginproblem());

# Show which answers are correct and which ones are incorrect
$showPartialCorrectAnswers = 1;

##############################################################
#
# Setup
#
#

Context("Numeric");
Context()->variables->are(m=>'Real',n=>'Real',k=>'Real');

#$test = Formula("1-C(k,n)/C(k,m+n)");
#$test = Formula("(fact(n)/(fact(n-k)*fact(k))");
$test = Formula("1-(n!/((n-k)!*k!))/((m+n)!/((m+n-k)!*k!))")->reduce;
$test->{test_points} = [[4,3,1],[5,4,3],[6,4,3]]; # remember set all 3 variables



##############################################################
#
# Text
#
#

Context()->texStrings;
BEGIN_TEXT
This problem is related to ECE514, HW1 Problem 4.
$PAR
There are \(m\) white balls and \(n\) black balls in a box. We draw \(k\) balls from the box. What is the probability of drawing at least one white ball?
$BR
P(at least one white ball) = \{ans_rule(40)\} \{ AnswerFormatHelp("formulas") \}



END_TEXT
Context()->normalStrings;

##############################################################
#
# Answers
#
#

ANS($test->cmp());


In reply to Joel Trussell

Re: using n choose k

by Alex Jordan -
You would like to add a function with two inputs to the context.
See here:
http://webwork.maa.org/wiki/Modifying_contexts_(advanced)#Adding_New_Functions

There is an example for adding a function of one variable, but note that you want to work with numeric2.

C(n,k) should work at the perl level, so your subroutine defining C the MathObject function should be able to use C the perl function.

I haven't tested any of this, just hoping that a quickly rattled off reply helps some.
In reply to Alex Jordan

Re: using n choose k

by Davide Cervone -
You are correct about what needs to be done. There is, in fact, an example in the pg/docs/MathObjects/extensions">pg/docs/MathObjects/extensions directory the implements C(n,k). There is also and example the defined "n choose k" as an operator (it uses #, but you could use choose or C or anything else you wanted.
In reply to Davide Cervone

Re: using n choose k

by Joel Trussell -
Thanks a bunch! - this helps - There was one problem that I encountered - see code below. After defining the function C(n,k) as in the examples, the line
$test1 = Real(1 - C($n1,$k1)/C($m1+$n1,$k1));
works fine, but the line
$test1 = Real(1-C($k1,$n1)/C($k1,$m1+$n1));
produces an error
the only difference is the spaces before and after the minus sign.

A second question: I used the webpage
http://webwork.maa.org/wiki/Available_Functions#.W-i1HzNRfD4
when I started coding this problem. It seems to indicate that C(n,k) is included in the standard math and does not require special definition. Is this correct? or should the webpage be updated?

Thanks
Joel


##DESCRIPTION
## Algebra problem: true or false for inequality
##ENDDESCRIPTION

##KEYWORDS('algebra', 'inequality', 'fraction')

## DBsubject('Algebra')
## DBchapter('Fundamentals')
## DBsection('Real Numbers')
## Date('6/3/2002')
## Author('')
## Institution('')
## TitleText1('Precalculus')
## EditionText1('3')
## AuthorText1('Stewart, Redlin, Watson')
## Section1('1.1')
## Problem1('22')

########################################################################

DOCUMENT();

loadMacros(
"PGstandard.pl", # Standard macros for PG language
"MathObjects.pl",
#"source.pl", # allows code to be displayed on certain sites.
"PGcourse.pl", # Customization file for the course
"parserPopUp.pl",
"AnswerFormatHelp.pl",
"PGauxiliaryFunctions.pl",
);

# sampling rate problem with aliasing

TEXT(beginproblem());

# Show which answers are correct and which ones are incorrect
$showPartialCorrectAnswers = 1;

##############################################################
#
# Setup
#
#

Context("Numeric");
Context()->variables->are(m=>'Real',n=>'Real',k=>'Real');

package MyFunction2;
our @ISA = qw(Parser::Function::numeric2); # this is what makes it R^2 -> R

sub C {
shift; my ($n,$r) = @_; my $C = 1;
$r = $n-$r if ($r > $n-$r); # find the smaller of the two
for (1..$r) {$C = $C*($n-$_+1)/$_}
return $C
}

package main;

#
# Make it work on formulas as well as numbers
#
sub C {Parser::Function->call('C',@_)}

#
# Add the new function into the Context
#
Context()->functions->add(C => {class => 'MyFunction2'});
$m1 = 5;
$n1 = 7;
$k1 = 4;
#$test1 = Real(1-C($k1,$n1)/C($k1,$m1+$n1));
$test1 = Real(1 - C($n1,$k1)/C($m1+$n1,$k1));
$test = Formula("1-C(n,k)/C(m+n,k)");
#$test = Formula("(fact(n)/(fact(n-k)*fact(k))");
#$test = Formula("1-(n!/((n-k)!*k!))/((m+n)!/((m+n-k)!*k!))")->reduce;
$test->{test_points} = [[4,3,1],[5,4,3],[6,4,3]]; # remember set all 3 variables



##############################################################
#
# Text
#
#

Context()->texStrings;
BEGIN_TEXT
This problem is related to ECE514, HW1 Problem 4.
$PAR
There are \(m\) white balls and \(n\) black balls in a box. We draw \(k\) balls from the box. What is the probability of drawing at least one white ball? You may use the N Choose K function defined by \( C(N,K) = \) number of distinct (unordered) ways that \(N\) items can be drawn \(K\) at a time.
$BR
P(at least one white ball) = \{ans_rule(30)\} \{ AnswerFormatHelp("formula") \}
$BR
Compute the answer for \(m=5\), \(n=7\), \(k= 4\)
$BR
\{ans_rule(30)\} \{ AnswerFormatHelp("number") \}

END_TEXT
Context()->normalStrings;

##############################################################
#
# Answers
#
#

ANS($test->cmp());
ANS($test1->cmp());





ENDDOCUMENT();

In reply to Joel Trussell

Re: using n choose k

by Davide Cervone -
The issue is that Perl has a number of "file test flags" (see this list) that are modeled on unix command-line arguments for checking if certain files match certain conditions. For example, -d $filename returns true if the file is a directory and false otherwise. It turns out that -C is such a flag.

In Perl, these build-in flags take precedence over user-defined functions, so in your expression 1-C($n1,$k1), the -C is being interpreted as a file test flag, not a minus with a function call. Personally, I think this is a bad choice, but it is the way it is, and we have to live with that.

There are a number of ways to work around it. One is to put a space between the minus and the "C"; this is why your second format works. Another would be to put parentheses around the function calls, as in 1-(C($n1,$k1)).

It is actually good practice to pus spaces around operators (for readability), so if you get in the habit of that, you will not run into this issue. But I understand how it can be confusing.