Maxima Bridge System: a Software Interface Between Stata and the Maxima Computer Algebra System

Maxima Bridge System: a Software Interface Between Stata and the Maxima Computer Algebra System

X Italian Stata User Group Meeting Florence, 14-15 November 2013 Maxima Bridge System: a Software Interface between Stata and the Maxima Computer Algebra System Giovanni Luca Lo Magno [email protected] Department of Economics, Business and Statistics University of Palermo What is a computer algebra system (CAS)? A CAS is a software program which enables the user to manipulate mathematical expression in a symbolic form Useful for dealing with: equations derivatives of functions integrals arbitrary numerical precision Maxima: a free and open-source CAS highly featured CAS programmable and extendible written in LISP runs in Windows, Mac, Unix and GNU/Linux downloadable from http://maxima.sourceforge.net Using Maxima from the terminal Maxima is at heart a command-line software program wxMaxima: a graphical user interface (GUI) for Maxima Downloadable for free from http://andrejv.github.io/wxmaxima Numerical computations vs. symbolic computations Calculating arcsin(1) Stata .. displaydisplay asin(1)asin(1) 1.57079631.5707963 Maxima (%i1)(%i1) asin(1);asin(1); %pi%pi (%o1)(%o1) ------ 22 Numerical computations vs. symbolic computations Obtaining the decimal expansion for π Stata .. displaydisplay %26.24f%26.24f _pi_pi 3.1415926535897931159979633.141592653589793115997963 Maxima (%i1)(%i1) %pi,%pi, bfloat,bfloat, fpprec:fpprec: 300;300; (%o1)(%o1) 3.141592653589793238462643383279502884197169399375105\3.141592653589793238462643383279502884197169399375105\ 82097494459230781640628620899862803482534211706798214808651\82097494459230781640628620899862803482534211706798214808651\ 32823066470938446095505822317253594081284811174502841027019\32823066470938446095505822317253594081284811174502841027019\ 38521105559644622948954930381964428810975665933446128475648\38521105559644622948954930381964428810975665933446128475648\ 23378678316527120190914564856692346034861045432664821339360\23378678316527120190914564856692346034861045432664821339360\ 726024914127b0726024914127b0 Numerical computations vs. symbolic computations Verifying the inverse of a Hilbert (1894) matrix Stata .. matrixmatrix AA == 1,1, 1/21/2 \\ 1/2,1/2, 1/31/3 .. matrixmatrix II == AA ** invsym(A)invsym(A) .. matrixmatrix listlist II symmetricsymmetric I[2,2]I[2,2] r1r1 r2r2 r1r1 11 r2r2 -3.708e-17-3.708e-17 11 .. displaydisplay C[2,1]C[2,1] ==== 00 00 Maxima (%i1)(%i1) A:A: matrix([1,matrix([1, 1/2],1/2], [1/2,[1/2, 1/3])1/3]) $$ (%i2)(%i2) I:I: AA .. invert(A);invert(A); [[ 11 00 ]] (%o2)(%o2) [[ ]] [[ 00 11 ]] (%i3) is(I[2,1]=0); (%i3) is(I[2,1]=0); (%o3)(%o3) truetrue What is Maxima Bridge System (MBS)? MBS is a software system which enables Stata to interface with Maxima MBS permits: the use of Maxima from Stata the writing of ado-program incorporating Maxima commands MBS comprises: a Stata plugin ado-programs (maxima, maximaget, maximaput) a stand-alone software program (Maxima Bridge) Maxima functions (MBS Utility Package) Calling Maxima from Stata MBS architecture Stata Maxima Bridge Maxima maximaget MBS Maxima Stata Maxima maxima Utility Plugin Server Server Package maximaput Maxima Plugin: Stata Server: MBS Utility manages the communicates with Package: communication with Stata Maxima functions Maxima Maxima Server: that facilitate the maxima: communicates with cooperation between a wrapper for the Maxima Stata and Maxima Stata Plugin maximaget: imports data from Maxima maximaput exports data to Maxima Maxima Bridge A bridge between Stata and Maxima Connects Stata and Stata by using a local TCP/IP network Essential for the communication between Stata and Maxima Stand-alone software program Written in C++ A screenshot of Maxima Bridge 0.1 beta Using Maxima from Stata: the maxima command Interactive mode .. maximamaxima ---- maximamaxima (type(type endend toto exit)exit) -------------------------------------------------------------- .. integrate(sqrt(x),integrate(sqrt(x), x,x, 0,0, 1);1); 22 (%o1)(%o1) -- 33 (%i2)(%i2) .. endend ------------------------------------------------------------------------------------------------------------------------ Inline mode .. maximamaxima integrate(sqrt(x),integrate(sqrt(x), x,x, 0,0, 1);1); 22 (%o2)(%o2) -- 33 (%i3)(%i3) Macro expansion when the maxima command is called Macro expansion only works in inline mode .. globalglobal testtest "mess""mess" .. maximamaxima ---- maximamaxima (type(type endend toto exit)exit) -------------------------------------------------------------- .. print("thisprint("this isis aa $test");$test"); thisthis isis aa $test$test (%o1)(%o1) thisthis isis aa $test$test (%i2)(%i2) .. endend ------------------------------------------------------------------------------------------------------------------------ .. maximamaxima print("thisprint("this isis aa $test");$test"); thisthis isis aa messmess (%o2)(%o2) thisthis isis aa messmess (%i3)(%i3) Data exchange between Stata and Maxima MBS provides two commands for data exchange: maximaget and maximaput maximaget 10101 Maxima data 10111 01101 10101 10111 Stata data 01101 maximaput Importing Maxima data: the maximaget command .. maximamaxima M:M: matrix([1,matrix([1, 2],2], [3,[3, 4]);4]); [[ 11 22 ]] (%o1)(%o1) [[ ]] [[ 33 44 ]] (%i2)(%i2) .. maximagetmaximaget MM (%o2)(%o2) donedone (%i3)(%i3) .. matrixmatrix listlist MM M[2,2]M[2,2] c1c1 c2c2 r1r1 11 22 r2r2 33 44 How Maxima data are imported into Stata The Maxima data to be imported is stored in a temporary binary file 1 Stata Maxima maximaget orders Maxima to write the temporary binary file maximaget 3 2 maximaget reads the Maxima writes the temporary binary file temporary binary file 1001010 1101111 1001101 1001001 1110101 temporary binary file Exporting Stata data: the maximaput command .. sysusesysuse autoauto (1978(1978 AutomobileAutomobile Data)Data) .. correlatecorrelate priceprice weightweight lengthlength (obs=74)(obs=74) || priceprice weightweight lengthlength -------------+----------------------------------------+--------------------------- priceprice || 1.00001.0000 weightweight || 0.53860.5386 1.00001.0000 lengthlength || 0.43180.4318 0.94600.9460 1.00001.0000 .. matrixmatrix CC == r(C)r(C) .. maximaputmaximaput CC (%o1)(%o1) [[ 1.01.0 0.538611462600480.53861146260048 0.431831244971590.43183124497159 ]] [[ ]] [[ 0.538611462600480.53861146260048 1.01.0 0.946008643417810.94600864341781 ]] [[ ]] [[ 0.431831244971590.43183124497159 0.946008643417810.94600864341781 1.01.0 ]] (%i2)(%i2) How Stata data are exported to Maxima The Stata data to be exported is stored in a temporary binary file 2 Stata Maxima maximaput orders Maxima to read the temporary binary file maximaput 1 3 maximaput writes the Maxima reads the temporary binary file temporary binary file 1001010 1101111 1001101 1001001 1110101 temporary binary file Syntax of maximaget and maximaput maximagetmaximaget maxima_namemaxima_name [[ifif][][inin],], fromscafromscalarlar fromlistfromlist frommatfrommatrixrix toscatoscalarlar tomatrixtomatrix tovartovar name(name(stata_namestata_name)) replacereplace Import facilities From Maxima To Stata scalar scalar list matrix matrix variable maximaputmaximaput statastata_name_name [[ifif][][inin],], fromscafromscalarlar fromlistfromlist fromvarfromvariableiable toscatoscalarlar tolisttolist tomattomatrixrix name(maximaname(maxima_name)_name) Export facilities From Stata To Maxima scalar scalar matrix list variable matrix MBS Utility Package Maxima functions to facilitate the cooperation beween Stata and Maxima statamissing stataf statafm killtemp wxmconnect written in Lisp defined in the maximabridge-init.lisp file, which is located under the Maxima Bridge installation folder automatically loaded by Maxima Bridge MBS Utility Package: the statamissing function Returns a Stata missing value Syntax statamissing(missing_type) missing_type is a string, which can be ., a, b, …, z: Example .. maximamaxima missvalue:missvalue: statamissing("g");statamissing("g"); (%o1)(%o1) 9.0038268217042019E+3079.0038268217042019E+307 (%i2)(%i2) .. quietlyquietly maximagetmaximaget missvaluemissvalue .. scalarscalar listlist missvaluemissvalue missvaluemissvalue == .g.g Missing values are internally stored as large positive value What if a number is divided by zero? Stata: a system missing value (.) is returned .. displaydisplay 3/03/0 .. Maxima: an error is returned .. maximamaxima 3/0;3/0; expt:expt: undefined:undefined: 00 toto aa negativenegative exponent.exponent. ---- anan error.error. ToTo debugdebug thisthis try:try: debugmode(true);debugmode(true); (%i2)(%i2) Stata functions and expressions generally return a missing value when an error occurs MBS Utility Package: the stataf function Given a Maxima function maxima_function with an argument list argument_list, stataf returns a wrapper function for maxima_function which returns a missing value when an error internally occurs Syntax stataf(maxima_function, argument_list) maxima_function must be a function argument_list must be a list error error message maxima_function no error ok error missing value wrapper_for_maxima_function no error ok MBS Utility Package: the stataf function Example .. maximamaxima divide(x,divide(x, y)y) :=:= x/yx/y $$ (%i2)(%i2) .. maximamaxima st_divide(x,st_divide(x, y)y) :=:= stataf(divide,stataf(divide, [x,[x, y])y]) $$ (%i3)(%i3) .. maximamaxima result:result: st_divide(3,st_divide(3, 0);0); (%o3)(%o3) 8.9884656743115795E+3078.9884656743115795E+307 (%i4)(%i4) .. maximagetmaximaget resultresult (%o4)(%o4) donedone (%i5)(%i5) .. scalarscalar listlist resultresult result = . result = . MBS Utility

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    83 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us