Visualizzazione post con etichetta Machine Learning. Mostra tutti i post
Visualizzazione post con etichetta Machine Learning. Mostra tutti i post

lunedì 10 agosto 2015

Oracle:Linear regression through a SQL Query



I want to have a linear approximation directly in an SQL query.

To make it easy: having a generic function f, I want to approximate it with the function:


Being sure that I found the A and B which minimize the global error between f and f' (where f' is the linear approximation of f). 

Let's generate some data: 
 create table zzz_some_data as   
  SELECT    
    'X' groupid,   
    level x,    
    exp(level/10) + sin(level/10 *3.14) + dbms_random.value(0,1) y   
   FROM DUAL   
   CONNECT BY LEVEL <= 20   
 union all   
  SELECT    
    'Y' groupid,   
    level x,    
    exp(level/10) + Cos(level/10 *3.14) + dbms_random.value(0,1) y  
   FROM DUAL   
   CONNECT BY LEVEL <= 20;  

As you can see there is a random factor. At the bottom of the tutorial, you can find the exact data generated. 

Plotting these numbers the functions look like this: 



Running the following query, I can get the value of A and B for the two functions:

 WITH mean_estimates AS  
 (  SELECT GroupID  
      ,AVG(x) AS xmean  
      ,AVG(y) AS ymean  
      ,min(y) as first_seq  
      ,max(y) as last_seq  
   FROM zzz_some_data  
   GROUP BY GroupID  
 ),  
 stdev_estimates AS  
 (  SELECT pd.GroupID  
      ,CASE     
        WHEN SUM(POWER(x - xmean, 2)) = 0 THEN 1   
        when COUNT(*) = 1 then 10000000  
        ELSE SQRT(SUM(POWER(x - xmean, 2)) / (COUNT(*) - 1))   
       END AS xstdev  
      ,CASE  
        when count(*) = 1 then 10000000   
        else SQRT(SUM(POWER(y - ymean, 2)) / (COUNT(*) - 1))     
       end AS ystdev  
   FROM zzz_some_data pd  
   INNER JOIN mean_estimates pm ON pm.GroupID = pd.GroupID  
   GROUP BY pd.GroupID, pm.xmean, pm.ymean  
 ),  
 standardized_data AS          -- increases numerical stability  
 (  SELECT pd.GroupID  
      ,(x - xmean) / xstdev                  AS xstd  
      ,CASE ystdev WHEN 0 THEN 0 ELSE (y - ymean) / ystdev END AS ystd  
   FROM zzz_some_data pd  
   INNER JOIN stdev_estimates ps ON ps.GroupID = pd.GroupID  
   INNER JOIN mean_estimates pm ON pm.GroupID = pd.GroupID  
 ),  
 standardized_beta_estimates AS  
 (  SELECT GroupID  
      ,CASE WHEN SUM(xstd * xstd) = 0 THEN 0  
         ELSE SUM(xstd * ystd) / (COUNT(*) - 1) END     AS betastd  
   FROM standardized_data pd  
   GROUP BY GroupID  
 ),   
 linear_model as   
 (  
 SELECT   
    pb.GroupID,     
    ymean - xmean * betastd * ystdev / xstdev AS BETA,   
    betastd * ystdev / xstdev         AS ALPHA,   
    first_seq,   
    last_seq  
 FROM standardized_beta_estimates pb  
 INNER JOIN stdev_estimates ps ON ps.GroupID = pb.GroupID  
 INNER JOIN mean_estimates pm ON pm.GroupID = pb.GroupID  
 )  
 select   
  GroupID,   
  alpha,   
  Beta  
 from linear_model  
 order by GroupID desc;  


This query generate the following output: 

 GROUPID   ALPHA    BETA  
 ------- ---------- ----------  
 Y    ,341603277 ,27926809   
 X    ,221725267 1,66190364   



Let's see now the results: 


Here the plot of the approximation of X:


Here the plot of the approximation of Y:








 CREATE TABLE "ZZZ_SOME_DATA"  
  (  
   "GROUPID" CHAR(1 BYTE),  
   "X"    NUMBER,  
   "Y"    NUMBER  
  );  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('X','1','1,61932317662496064883408392432113402523');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('X','2','2,77694913213341470441940320202765814948');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('X','3','2,91090102987016066482064043800309804071');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('X','4','3,13678897398740391101470575963142792902');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('X','5','3,31605237538417176477155850638842190865');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('X','6','3,13667075089211953539069065645455345878');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('X','7','3,47488861159608290867160313703997232386');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('X','8','3,42251149078741711848240969510483689103');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('X','9','3,70603465587716541507204559098228342981');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('X','10','3,49314419776404379007856528151793653642');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('X','11','3,24172315449369167895615169724235970383');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('X','12','2,96881260394832309096782780931487864967');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('X','13','3,07513520161764733445750922412894102533');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('X','14','3,81479688098856311171457385615269516843');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('X','15','4,32475565056326473828436305142365381565');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('X','16','4,85233459222270714560038164481370508461');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('X','17','5,61989845355259006300115338181868398089');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('X','18','6,31700004318003808435939347557223237057');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('X','19','6,76689263318576069778969371993663187878');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('X','20','7,82576523994149918219695417536412769495');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('Y','1','2,53714598427679136859871205905510695503');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('Y','2','2,3110817502573158903868891890087776345');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('Y','3','1,94494243937675278618005281210530984129');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('Y','4','2,25982980116506374957923410951751088925');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('Y','5','1,86603427812480463944739874768807857366');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('Y','6','2,05505938632236899572636979590661767446');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('Y','7','2,3258999982985244735180126549118494278');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('Y','8','1,83188925742276377791489099175111599133');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('Y','9','2,41166969667358720655946060775919042819');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('Y','10','1,8104604598253185583746180090281519982');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('Y','11','2,87738150798966416515834453685956594055');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('Y','12','3,43069799388754748665140656518089681805');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('Y','13','3,54673238603787598200769925226968142282');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('Y','14','4,43614682007291136740320048898865794443');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('Y','15','4,87577441430844397983546298110499857896');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('Y','16','6,00804958270558010522626027655493241713');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('Y','17','6,3755271699704708032463599263767041073');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('Y','18','7,12510967938699587673395044064905861434');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('Y','19','8,17916759972655812255130821868093121265');  
 Insert into ZZZ_SOME_DATA (GROUPID,X,Y) values ('Y','20','9,11344975307640059368329002839752407251');  
 COMMIT;  

venerdì 7 agosto 2015

Tutorial: neural network in R by examples (using the package neuralnet)

Tutorial: Create a neural network in R

Let's start with the basics. 
If you want to start learning how does it work a NN in R, start searching for some packages supporting you. 

Googling a bit you will wee that there are two main packages: 
  • Neuralnet: specific for neural networks
  • Caret: generic machine learning package, containing a lot of machine learning algorithm, supporting very well the neural networks.
In this post I will show some investigation I made on the package "Neuralnet".
In this next post I show the investigation I made on the package "Caret".

Basic AND and OR functions


If you go on the help of R, typing: 
 ?neuralnet  

You can get the complete documentation.
Going quickly to the example, you find the following code:

 AND <- c(rep(0,7),1)  
 OR <- c(0,rep(1,7))  
 binary.data <- data.frame(expand.grid(c(0,1), c(0,1), c(0,1)), AND, OR)  
 net <- neuralnet(AND+OR~Var1+Var2+Var3, binary.data, hidden=0,   
             rep=10, err.fct="ce", linear.output=FALSE)  


Let's have a look inside to the returned object "net":
 > typeof(net)  
 [1] "list"  

 > summary(net)  
                   Length   Class      Mode    
 call                  7    -none-     call    
 response             16    -none-     numeric   
 covariate            24    -none-     numeric   
 model.list            2    -none-     list    
 err.fct               1    -none-     function  
 act.fct               1    -none-     function  
 linear.output         1    -none-     logical   
 data                  5    data.frame list    
 net.result           10    -none-     list    
 weights              10    -none-     list    
 startweights         10    -none-     list    
 generalized.weights  10    -none-     list    
 result.matrix       110    -none-     numeric   
 

> head(net)  
 $call  
 neuralnet(formula = AND + OR ~ Var1 + Var2 + Var3, data = binary.data,   
   hidden = 0, rep = 10, err.fct = "ce", linear.output = FALSE)  
 $response  
  AND OR  
 1  0 0  
 2  0 1  
 3  0 1  
 4  0 1  
 5  0 1  
 6  0 1  
 7  0 1  
 8  1 1  
 $covariate  
    [,1] [,2] [,3]  
 [1,]  0  0  0  
 [2,]  1  0  0  
 [3,]  0  1  0  
 [4,]  1  1  0  
 [5,]  0  0  1  
 [6,]  1  0  1  
 [7,]  0  1  1  
 [8,]  1  1  1  
 $model.list  
 $model.list$response  
 [1] "AND" "OR"   
 $model.list$variables  
 [1] "Var1" "Var2" "Var3"  
 $err.fct  
 function (x, y)   
 {  
   -(y * log(x) + (1 - y) * log(1 - x))  
 }  
 <environment: 0x00000000174034c8>  
 attr(,"type")  
 [1] "ce"  
 $act.fct  
 function (x)   
 {  
   1/(1 + exp(-x))  
 }  
 <environment: 0x00000000174034c8>  
 attr(,"type")  
 [1] "logistic"  

> print(net)
10 repetitions were calculated.

           Error Reached Threshold Steps
7  0.07270325501    0.007209205002   237
2  0.08361412743    0.008216337601   210
1  0.08425544114    0.008840366736   228
4  0.08667354432    0.009692218237   226
6  0.08736308608    0.009046804494   236
10 0.08767067498    0.009009176337   240
3  0.08817255652    0.009546552076   214
8  0.09165608530    0.009676470949   221
5  0.09170859687    0.008872336865   213
9  0.09517374836    0.009993935991   223




How can I use the NN once I have trained it?

 > test <- data.frame(expand.grid(c(0,1), c(0,1), c(0,1)))  
 > predicted <- compute(net, test) #Run them through the neural network  
 > print(predicted$net.result)  
            [,1]       [,2]  
 [1,] 0.000000005510800906 0.00003437586348  
 [2,] 0.000008322603907184 0.99999887725263  
 [3,] 0.000010232856460365 0.99999418995760  
 [4,] 0.015219103082846375 1.00000000000000  
 [5,] 0.000009115152595497 0.99998044588564  
 [6,] 0.013579325087889940 1.00000000000000  
 [7,] 0.016644285280624567 1.00000000000000  
 [8,] 0.962352882489281747 1.00000000000000  
 >   



What is very cool about this package is the graphical representation:

 > plot(net)

generate this plot:


Square root function

There is a great tutorial in this page: 


Let's have a look at the code: 

 install.packages('neuralnet')  
 library("neuralnet")  

 #Going to create a neural network to perform sqare rooting  
 #Type ?neuralnet for more information on the neuralnet library  
 #Generate 50 random numbers uniformly distributed between 0 and 100  
 #And store them as a dataframe  
 traininginput <- as.data.frame(runif(50, min=0, max=100))  
 trainingoutput <- sqrt(traininginput)  
 #Column bind the data into one variable  
 trainingdata <- cbind(traininginput,trainingoutput)  
 colnames(trainingdata) <- c("Input","Output")  
 #Train the neural network  
 #Going to have 10 hidden layers  
 #Threshold is a numeric value specifying the threshold for the partial  
 #derivatives of the error function as stopping criteria.  
 net.sqrt <- neuralnet(Output~Input,trainingdata, hidden=10, threshold=0.01)  
 #Test the neural network on some training data  
 testdata <- as.data.frame((1:10)^2) #Generate some squared numbers  
 net.results <- compute(net.sqrt, testdata) #Run them through the neural network  


Let's have a look at the reuslt:
 > cleanoutput <- cbind(testdata,sqrt(testdata),  
 +           as.data.frame(net.results$net.result))  
 > colnames(cleanoutput) <- c("Input","Expected Output","Neural Net Output")  
 > print(cleanoutput)  
   Input Expected Output Neural Net Output  
 1   1                 1       1.292537723  
 2   4                 2       2.004110690  
 3   9                 3       3.004860581  
 4   16                4       4.001313250  
 5   25                5       4.996608428  
 6   36                6       6.003918278  
 7   49                7       6.997395208  
 8   64                8       7.995504751  
 9   81                9       9.009677415  
 10  100              10       9.959406366  
 >   

Having a look at the graphical representation:


Square root function with more nodes in the hidden layer

And what if I wanted to increase the number of nodes in the hidden layer?
Please consider that this is only to see how to tune the network. Use an hidden layer for the Sqrt function is totally useless, and I will show it later with the results.

Anyway, to add more layers, it is simply needed to use the value:    "hidden = c(x, y, z)"
where:
- x: number of perceptrons in the first hidden layer
- y: number of perceptrons in the second hidden layer
- z: number of perceptrons in the third hidden layer

 traininginput <- as.data.frame(runif(50, min=0, max=100))  
 trainingoutput <- sqrt(traininginput)  
 trainingdata <- cbind(traininginput,trainingoutput)  
 colnames(trainingdata) <- c("Input","Output")  
 net.sqrt <- neuralnet(Output~Input,trainingdata, hidden=c(10, 10, 10), threshold=0.01)  

Let's plot now the network:

Let's check now the results (you will see, adding these hidden layer did not improve the results at all):

 > cleanoutput <- cbind(testdata,sqrt(testdata),  
 +           as.data.frame(net.results$net.result))  
 > colnames(cleanoutput) <- c("Input","Expected Output","Neural Net Output")  
 > print(cleanoutput)  
   Input Expected Output Neural Net Output  
 1   1                1    0.591036793  
 2   4                2    2.070495859  
 3   9                3    3.000115410  
 4   16               4    3.999594071  
 5   25               5    4.997643979  
 6   36               6    6.034835331  
 7   49               7    6.997600383  
 8   64               8    7.997306744  
 9   81               9    8.999801209  
 10  100             10    9.982023499  
 >   

sabato 15 marzo 2014

Octave Cheat Sheet

Octave Cheat Sheet:



I found this very interesting cheat sheet about Octave here:
Source http://altons.github.io/octave/2013/05/05/octave-commands-cheat-sheet/

I think it is very great, but there are couple of commands missing (like PS1, which in my opinion makes your life better!).
So I have taken it and extendend with some commands.

Management & Help

Task
Command
exits software
quit or exit
list variables in session
who
list variables in session with info about type
whos
deallocate variable(s)
clear varname
displays search path
path
Adds path to search
addpath
clear screen
clc
list .m files in dir
what
search for keyword
lookfor
displays help for function
help funname
Parse and execute the contents of FILE
source(".octaverc")
List installed packages
pkg list
Install packages
pkg install [-auto or -noauto] pkg_name.tar.gz
Describe packages
pkg describe pkg_name
Load/Unload package
pkg load/unload pkg_name
Uninstall package
pkg uninstall pkg_name



Shell Commands

Task
Command
Change Linde starter
PS1(‘desired Starter’); %PS1(‘>> ‘);
change working directory to dir
cd dirname
print working directory
Pwd
print directory listing
ls
return value of named environment variable
getenv (string)
execute arbitrary shell command string
system (cmd)
Load the file
 
You will load a file and make it available with a
Varible having same name of the file without extension
Load FILENAME_IN_PWD
Load(‘FILENAME_IN_PWD’);
Save variable into file
 
You will load a file and make it available with a
Varible having same name of the file without extension
save  FILENAME_IN_PWD VARIABLE_NAME;



Special Operators

Definition
Operator
Example
comment
%
% Initialising
wildcard
*
clear p*
array
[ ]
[1 2; 3 4]
Cell Arrays
{ }
p = { 10 , 15 , "rectangle" }; p{1} =10
ranges
start:step:stop (default step=1)
1:10 or 1:10:100
variable assignment
=
A=magic(3);
do not display
;
a=1;



Workflow

Task
Command
Suspend the execution of the program.
pause; or pause(5)
print string to the stout
fprintf("Hello World")



Vectors & Matrices

Rules

· Square brackets delimit literal matrices.
· Commas separate elements on the same row.
· Semicolons separate rows.
· Commas may be replaced by spaces, and
· semicolons may be replaced by one or more newlines.
· Elements of a matrix may be arbitrary expressions, provided that all the dimensions agree.
· Optional: Matrices are denoted by uppercase letters A,B,X etc while vectors by lowercase letters a,b,y etc.
 

Example
Expression
enter a row vector
[ x, y, ... ]
enter a column vector
[ x; y; ... ]
enter a 2×2 matrix
[ w, x; y, z ]
Return a row vector with N linearly spaced elements between BASE and LIMIT
linspace (BASE, LIMIT, N)
Similar to linspace except that the values are logarithmically spaced
logspace(BASE, LIMIT, N)
Higher Dimensional Arrays
B(z,y,z)=1
Sorting arrays
sort(A); or sort(A, 'descend');
Return true if any element in array is non-zero
any(A)
Return true if all element are non-zero
all(A)
Return a vector of indices of a matrix
[i , j] = find(A<0.5)
 
 

 

Array Operations

Task
Function
select elements of a vector
A(i)
select elements of a matrix
A(i,j)
select rows (columns) corresponding to the elements of a range
A(1:2,1); A(1:2,1:5);
select all rows (columns)
A(:,1); A(3,:);
Delete a row/column of a matrix
A(2,:)= [] or A(:,5) = []

 



Linear Algebra

Task
Function
dimensions of the array
size
returns larger dimension
length
allocates array with 0’s
zeros
allocates array with 1’s
ones
transpose array
'
Compute the determinant of A.
det(A)
inverse of matrix
inv
pseudo inverse of matrix
pinv
Calculate Eigenvalues / Eigenvectors of matrix A
eig(A) or [V,L] = eig(A)
Identity matrix
eye
Compute the rank of a Matrix
rank(A)
returns the lower triangular part of A
tril(A)
returns the upper triangular part of A
triu(A)
Create an N-by-N magic square
magic
Compute the dot product of two vectors
dot(A,B)
Compute the vector cross product of two 3-dimensional vectors X and Y
cross(X,Y)
interval range arrays
v = 1:0.1:2
%vector = STARTING_AT:DELTA:ENDING_AT



Plotting
Task
Command
Example
2-D plot
plot
plot(x,y); plot(x, a+b+c);
Surface plot
surf
surf(theta0, theta1, J);
Contour plot
contour
contour(theta0, theta1, J, logspace(-2, 3, 20))
Specify x-, y-, or z-axis labels for the current axis
[x,y,z]label
xlabel('k lags')
Set a plot window to plot window N
figure
figure;plot(x, a);
close figure window(s)
close
close [***(N),all,all hidden***]
new graphic objects are added to the plot
hold
hold on;plot(x, b);
Clear plot and restore default graphics settings
hold
hold off;
Display a legend for the axes
legend
plot(X,Y);legend('Temperature', 'Gas Demand');
give a title
title
title('myTitle');
export the chart
print
Print –dpng ‘filename.png’



Math Functions

Type
Function
Examples
Sum of elements along dimension DIM
sum
sum([1 2 3 4])
Product of elements along dimension DIM
prod
prod([1 2 3 4)]
Trigonometric
sin, cos, tan
floor((1+tan(1.2)) / 1.2)
Inverse Trigonometric
asin, acos, atan
Natural Logarithm
log
Base 10 Logarithm
log10
log10(100)/log10(10)
Exponentiation
exp
exp(0)
Absolute value
abs
abs(-3)
Square Root
sqrt
sqrt(3^2 + 4^2)
X raised to the Y power
power(X,Y)
power(3,2)
Real part of complex number
real
real(3+5I)
Imaginary part of complex number
imag
imag(3+5I)
Evaluate polynomials
polyval
polyval([2 10.1 0 6],0)
Write formatted polynomial
polyout
polyout([2 -3 1],"x")
Return the largest integer not greater than X
floor
floor(1.9)
Return the smallest integer not less than X
ceil
ceil(3.7)
Return the integer nearest to X
round
round(1.9)
Truncate fractional portion of X and return the integer portion
fix
fix(pi)



Stats Functions

Task
Example
Function
Uniform random numbers btw 0 and 1 (both excluded)
rand
rand(3,5)
Normal(0,1) random numbers
randn
randn(1,3)
Gamma distribution
randg
randg
Exponential distribution
rande
rande(1,10)
Poisson distribution
randp
randp(0.5,3,3)
Min value by column
min
min(A)
Max value by column
max
max(A)



Constants

Name
Expression
Default Variable
ans
Pi
pi
Euler's number
e
Imaginary number
i, j and I
Infinity
inf
Not a Number
NaN
machine precision
eps
true/false
logical 1/0



Logical Operators

Expression
Operator
is greater than
> 
is less than
< 
is greater than or equal to
>=
is less than or equal to
<=
is equal to
==
is not equal to
= or !=
AND with short circuiting
&&
with short circuiting
||
AND
&
OR
|
NOT



Auxiliary Functions

Task
Function
Check a scalar
isscalar(a)
Check a vector
isvector(b)
Check a matrix
ismatrix(b)
is func available
is TAB TAB
Type info
typeinfo(b)



String Functions

Task
Function
Example
Compare 2 strings
strcmp
strcmp("hello","Hello")



Import & Export Data

Task
Command
Example
Read the matrix DATA from a text file
dlmread (FILE, SEP, R0, C0)
dmlread("virus.dat",",",1,1);
Write data to a text file
dlmwrite (FILE, M, DELIM, R, C)
dlmwrite("out.txt",yhat,";",1,1);
Read the csv files
csvread (FILENAME, DLM_OPTS)
csvread("input.csv");
Write csv files
csvwrite (FILENAME, X, DLM_OPTS)
csvwrite("output.csv", yhat);

Defining Functions

Simplest Form

  function name
      body
  end

Example:

  function wakeup
        printf ("\a");
    end

Passing Params

  function name (arg-list)
        body
    end

Example:

  function wakeup (message)
        printf ("\a%s\n", message);
    end
 
  wakeup ("Rise and shine!");

Return Single Value

  function ret-var = name (arg-list)
        body
    end

Example:

  function retval = avg (v)
        retval = sum (v) / length (v);  
    end

Return Multiple Values

  function [ret-var1,ret-var2,…,ret-varn] = name (arg-list)
        body
    end

Example:

  function [mu,sigma] = basicStat(X)
    mu = mean(X);
    sigma = std(X);
  end



Statements

IF Statement

  if (condition)
       then-body
    elseif (condition)
       elseif-body
    else
       else-body
    end

Example:

   if (rem (x, 2) == 0)
       printf ("x is even\n");
     elseif (rem (x, 3) == 0)
       printf ("x is odd and divisible by 3\n");
     else
       printf ("x is odd\n");
     end

Note that the elseif keyword must not be spelled else if, as is allowed in Fortran. If it is, the space between the else and if will tell Octave to treat this as a new if statement within another if statement's else clause

SWITCH Statement

    switch (X)
       case 1
         do_something ();
       case 2
         do_something_else ();
       otherwise
         do_something_completely_different ();
     end

Example:

     A = 7;
     switch A
       case { 6, 7 }
         printf ("variable is either 6 or 7\n");
       otherwise
         printf ("variable is neither 6 nor 7\n");
     end

One advantage of using the switch statement compared to using if statements is that the labels can be strings

  switch (X)
       case "a string"
         do_something
       ...
     endswitch

WHILE Statement

  while (condition)
       body
     end

Example:

     fib = ones (1, 10);
     i = 3;
     while (i <= 10)
       fib (i) = fib (i-1) + fib (i-2);
       i++;
     end

DO-UNTIL Statement

     do
       body
     until (condition)

Example:

     fib = ones (1, 10);
     i = 2;
     do
       i++;
       fib (i) = fib (i-1) + fib (i-2);
     until (i == 10)

FOR Statement

     for var = expression
       body
     end

Example:

  fib = ones (1, 10);
     for i = 3:10
       fib (i) = fib (i-1) + fib (i-2);
     end