1
$\begingroup$

Is it possible to write this function in mathematica exactly as shown, and then run the expression on a dataset?

enter image description here

Please note I know that there are several other ways to do this. I am only interested in knowing how to compute it(if its possible) using notation that you would find in a text book.

$\endgroup$
1
  • $\begingroup$ This question would benefit from some attempt to actually do this in Mathematica and Mr. Wizard's answer raises important issues. Still, I've up voted it, because trying this kind of thing helped my entry into the Mathematica universe. I think it could also help others. Not the most efficient way to use Mathematica, but interesting none the less. $\endgroup$
    – Jagra
    Sep 26, 2013 at 14:34

2 Answers 2

7
$\begingroup$

The meaning of the subscript is not pre-defined in Mathematica. In this instance it is used to grab an element of a dataset, but it could be used as an adjective like Subscript[F, gravity]. To use the subscript notation you could use

<< Notation`
Notation[ParsedBoxWrapper[SubscriptBox["x_", "i_"]] ⟺ ParsedBoxWrapper[RowBox[{"x_", "[[", "i_", "]]"}]]]

Notation package makes subscripts equivalent to part

Then you could write a block of code which implements the procedure with whichever dataset is assigned to x at the moment of evaluation.

x = RandomReal[{0, 1}, 30];

Note N is a built-in function, so we will use [ScriptCapitalN] instead

\[ScriptCapitalN] = Length@x;
μ = 1/\[ScriptCapitalN] \!\(\*SubsuperscriptBox[\(∑\), \(i = 1\), \(\[ScriptCapitalN]\)]\*SubscriptBox[\(x\), \(i\)]\);
σ = Sqrt[1/\[ScriptCapitalN] \!\(\*SubsuperscriptBox[\(∑\), \(i = 1\), \(\[ScriptCapitalN]\)]\*SuperscriptBox[\((\*SubscriptBox[\(x\), \(i\)] - μ)\), \(2\)]\)];

immediate assignments

A little less "exactly as written", you could reinforce the idea that each of these quantities depends on the dataset by making them functions.

ClearAll[\[ScriptCapitalN], μ, σ];
\[ScriptCapitalN][x_] := Length@x;
μ[x_] := 1/\[ScriptCapitalN][x] \!\(\*SubsuperscriptBox[\(∑\), \(i = 1\), \(\[ScriptCapitalN][x]\)]\*SubscriptBox[\(x\), \(i\)]\);
σ[x_] := Sqrt[1/\[ScriptCapitalN][x] \!\(\*SubsuperscriptBox[\(∑\), \(i = 1\), \(\[ScriptCapitalN][x]\)]\*SuperscriptBox[\((\*SubscriptBox[\(x\), \(i\)] - μ[x])\), \(2\)]\)];
σ@x

delayed assignments

$\endgroup$
2
  • 1
    $\begingroup$ In a teaching setting, I would use this for small data sets so students see how the computer is just doing tedious hand calculations very quickly. Then I would introduce large data sets so the computer hangs. This is because functions are being called over and over unnecessarily. Ie, our implementation is inefficient. Then I would introduce the built-in functions, as efficient implementations for the same calculations. $\endgroup$ Sep 26, 2013 at 8:03
  • $\begingroup$ This is great! Thank you! $\endgroup$
    – ALEXANDER
    Sep 27, 2013 at 6:27
5
$\begingroup$

It matters if this about entering the expression (function), or displaying the expression. (Please clarify.)

If it is about entering the expression you will find that while you can get close in many cases it will not be exact, and also that there are ambiguities that must be resolved with more definite coding. Generally speaking I would recommend that you do not attempt this and instead enter functions in the most native Mathematica format.

If it is about display, e.g. TraditionalForm, then you can use things like Interpretation, and your own MakeBoxes or Format definitions.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.