# Grading functions

*Validation options*section). However, sometimes we will find that it's necessary to create our own rule for determining when an answer is correct. This can be done via

*grading functions*. A grading function is a user-defined formula that takes the student's answer and decides whether it is correct or not. This allows for even more specificity than with the options found on the Validation options section.

## Description

A grading function is a function of one or more variables (see grading functions and compound answers) defined in the *Define random variables and functions* section, that returns a number between 0 and 1. The number returned by the function in for each input corresponds to the mark out of 1.00 that will be received for that answer. In the first example, the values returned were either `true`

or `false`

, which is the same as 0 (no marks) or 1 (full marks). For an example using more values between 0 and 1, see custom marks.

Every time we wish to use a grading function, we must select it as the option in the *Validation options* section, and type the name of the function in the blank. Scroll down for more examples.

## Basic grading function

Let's suppose that we want to ask the question "*Give a prime number*". For this, we need to use a grading function.

First, let's create the grading function. This is done in the *Define random variables and functions* section. In this example, the grading function will be called .

The function we've defined, , returns `true`

if is prime, and `false`

otherwise. To use this as the validation method, select "custom grading function" in the *Validation options* section. Then, use the name of the grading function in the blank, in this case, "gf".

As for the *Correct answer* blank, anything will work. Since we've selected "grading function" as the validation method, the output of the grading function is the only thing that determines correct answers. In this case, we could input a prime number, for example.

## Custom marks

We'll show a simple example of a grading function with several outputs. The idea will be to award more marks for every level of correctness in the answer.

We want to ask the following question in basic arithmetic:

with partial credit awarded for each correct property of the answer the student gives. Say these are young children, and we consider the hardest part to be correctly giving a number whose remainder is 2 when divided by 7. We'll award 0.4 marks for that, and 0.3 for each of the other properties. Additionally, we want to only give half marks for the rest of the properties, if the answer falls in the first one (this is the purpose of `decrease_mark`

).

Observe that to make a longer grading function with multiple conditionals and calculations, we need to enclose it with the `begin..end`

statements and use `return`

to define the output of the function.

The question is completed now. Make sure to tick the "grading function" option in the *Validation options* section as described earlier, and give any particular correct value in the *Correct Answer* blank. We can see that both of these responses are correct:

We've used a fairly simple question as an example, but we can already see that grading functions can be made to have a very customized and complex design.

LIVE DEMO

## Compound answers

We can also create grading functions in the case of compound answer questions. All we need to do is use a separate argument for each answer blank. So, if there are three blanks to fill in, the grading function should look something like `gf(x,y,z)`

. The first argument corresponds to the first blank, the second argument corresponds to the second blank, and so on. Let's see a simple example.

The question will be a standard exercise in introductory calculus:

For this, the following grading function could be used:

The advantage in this case, as opposed to ordinary compound answers, is that we don't need to worry about the order in which the student answers. It also allows virtually infinite answers (here any answer of the form with an integer, was valid). However much more intricate behavior can be programmed, as is easily seen.

LIVE DEMO

## Empty answer from the student

Imagine that we are working with a compound answer like in compound answers. It may happen that the student leave a box empty and just answers one question. If you want to grade the answer he had submitted you need to modify the algorithm and test that the box is not empty with the command `not_null?`

. For instance, following the same example above

Recall that in the Correct Answer tab, we need to distribute the grade as we wish.

## Answering vectors

It is important to read this section if we wish to create questions involving vectors. The key point is that vectors are actually passed to the grading function as *matrices*, so we need to de-index them inside the function body to do normal vector operations with them. This should all be clear with the following basic example.

What this all essentially means is that if we pass `[...]`

to a grading function, it will be taken as `[ [...] ]`

(a list with the original vector as its only element). As a result, we cannot do vector operations with it. For example, the vector

will be converted to

How do we access the original vector? It is the first (and only) element of a list, so as with any list we may access the first element with the subindex "1", as so:

So if "v" was the original vector given by the student and passed to the grading function, to treat it as a vector we simply need to write "v_{1}" wherever we would like to use "v". Let's look at this with some examples.

#### Example 1

The matrix has rank 2, so there will be infinite solutions, meaning that we have to use a grading function if we want to allow any possible solution. The function itself is quite simple, but observe that we used the subindex as described above:

The other case we need to cover is allowing students to answer with a row or column vectors (until now we've only assumed row vectors). We'll illustrate what needs to be done with an example. Using the same question as before, let's say the student responds like so:

This is a column vector, and it is treated differently. All we need to do, though, is transpose it and we are back to the previous case. We most likely want to allow both types of answers, so at the beginning of the original grading function, we might add the following:

We've added an `if`

statement that simply detects if the input is a column vector (by seeing if the number of columns is 1), and transposes it in that case. If not, then nothing is done to "v". The result is that in all cases "v" will be of the form `[ [...] ]`

, and after that, we can use subindices exactly as described previously.

LIVE DEMO

#### Example 2

Imagine we want to ask the students the following question:

In this example, we clearly need a grading function since there are infinite correct answers.

The first answer is a point but it is understood as a matrix with one row and two columns instead: . Thus, if we want to work with the actual point, we have to retrieve the first row. Moreover, we have to tell CalcMe that it is a point (otherwise it will work with a vector). The first part of the grading function would be

The second answer is a vector, but as before it is understood as a matrix. Similarly, we have to access its first row with a grading function as the following:

## Keep arguments unevaluated

Suppose that, given an irreducible fraction, we want the student to answer with an equivalent fraction. We will also need a grading function to create this kind of question.

In this case, since we want the student's answer to be interpreted as it's written, we may select *Keep arguments unevaluated* so the evaluation system considers the fraction without simplifying. For instance, if we don't choose this option, it will understand as .

The corresponding grading function will be called *gf* and it will give the full grade if the student answers with a fraction mathematically equal to the statement's one, *f*, and their denominators are different.

`denominator(x)`

. It's very important to keep this notation in your mind. Thus, we will be able to answer with any equivalent fraction and it will be accepted as correct.