Saturday 14 October 2017

Dimensione Di Una Matrice In Forex Stata


AVVISO: Il gruppo di consulenza Idre statistica sarà la migrazione del sito web per il CMS WordPress nel mese di febbraio per facilitare la manutenzione e la creazione di nuovi contenuti. Alcune delle nostre pagine più vecchie verranno rimossi o archiviati in modo tale che essi non saranno più mantenuti. Cercheremo di mantenere i reindirizzamenti in modo che i vecchi URL continueranno a lavorare nel miglior modo possibile. Benvenuti al Istituto per la ricerca digitale e l'istruzione Aiuto Consulting Group Stat dando un regalo Stata FAQ Come posso accedere alle informazioni memorizzate dopo aver eseguito un comando nel Stata (risultati restituiti) Oltre all'uscita in mostrato nella finestra dei risultati, molti dei Statas comandi memorizzano le informazioni sul comando e dei suoi risultati in memoria. Questo consente all'utente, nonché altri comandi Stata, per rendere facilmente uso di queste informazioni. Stata chiama questi risultati restituiti. risultati restituiti può essere molto utile quando si desidera utilizzare le informazioni prodotte da un comando Stata di fare qualcos'altro in Stata. Ad esempio, se si vuole significare centro di una variabile, è possibile utilizzare riassumere per calcolare la media, quindi utilizzare il valore della media calcolata da riassumere per centrare la variabile. Utilizzando risultati restituiti eliminerà la necessità di riscrivere o tagliare e incollare il valore della media. Un altro esempio di come i risultati restituiti può essere utile è che se si desidera generare i valori attesi della variabile risultato quando le variabili predittive sono ad uno specifico insieme di valori, di nuovo qui, si potrebbe riscrivere i coefficienti o l'uso taglia e incolla, ma ha restituito risultati rendere il compito molto più facile. Il modo migliore per ottenere un senso di come i risultati restituiti lavoro è quello di saltare a destra e iniziare a guardare e li utilizzano. Il seguente codice apre un esempio di dati e utilizza riassumere (somma abbreviato) per generare statistiche descrittive per la lettura variabile. Questo produce l'output previsto, ma ancora più importante per i nostri scopi, Stata ora ha risultati del comando di riassumere in memoria. Ma come fai a sapere quali informazioni è stato memorizzato un elenco delle informazioni salvate da ogni comando è incluso nel file di aiuto Andor stampata manuale, così ho potuto guardare lì, ma posso anche solo tipo di elenco di ritorno. che elenca tutti i risultati restituiti in memoria. Sopra è un elenco dei risultati restituiti, come si può vedere ogni risultato è della forma R (.) Dove i puntini di sospensione (quot. Quot) è un breve etichetta. Abbiamo potuto vedere il file della Guida per il comando riassumere per scoprire ciò che ogni elemento della lista è, ma spesso è facile capire quale valore viene assegnato a quale risultato, ad esempio, R (media). Non contiene sorprendentemente la media di lettura (è possibile controllare questo contro l'uscita), ma altri non sono così evidenti, per esempio R (sumw). per questi, potrebbe essere necessario consultare il manuale se si pensa che si potrebbe desiderare di usarli. Il più delle volte il processo sarà relativamente facile perché youll sapere quale risultato si vuole accedere, si cercherà l'elenco per scoprire quale nome è memorizzato sotto, piuttosto che guardare la lista e cercando di capire ciò che ogni oggetto è. Come si può immaginare, diversi comandi, e anche lo stesso comando con diverse opzioni, memorizzare i risultati diversi. Di seguito riassumiamo la variabile leggere di nuovo, ma aggiungere l'opzione detail. Poi usiamo lista ritorno per ottenere l'elenco dei risultati restituiti. Così come l'opzione detail aggiunge informazioni all'uscita, risulta anche in ulteriori informazioni memorizzate nei risultati restituiti. Il nuovo elenco include tutte le informazioni restituite dal comando somma sopra, più curtosi asimmetria e una serie di percentili, tra cui il 1 ° (r (p25)) e 3 ° (r (p75)) quartili e la mediana (r (p50) ). Ora che abbiamo un po 'di senso di ciò che i risultati vengono restituiti dal comando riassumere, siamo in grado di fare uso dei risultati restituiti. In seguito attraverso con uno degli esempi di cui sopra, noi significa centrare la lettura variabile. Supponendo che l'ultimo comando abbiamo fatto è stato il comando summarize sopra, il codice qui sotto usi genera una nuova variabile, cread che contiene la media dei valori centrati di lettura. Si noti che invece di utilizzare il valore effettivo della media di lettura di questo comando, abbiamo usato il nome del risultato restituito (cioè R (media)), Stata sa quando vede R (media) che in realtà intende il valore memorizzato in che variabile di sistema. Nella riga successiva riassumiamo la nuova cread variabile. mentre la media non è esattamente uguale a zero, è all'interno di errore zero arrotondamento, quindi sappiamo che abbiamo correttamente intendiamo centrato lettura variabile. Come suggerisce il codice di cui sopra, possiamo usare tornati risultati più o meno allo stesso modo in cui si usa un numero effettivo. Questo perché Stata utilizza la r (.) Come segnaposto per un valore reale. Per un altro esempio di questo, diciamo che vogliamo calcolare la varianza di lettura dalla sua deviazione standard (ignorando il fatto che riassumono restituisce la varianza in R (Var)). Possiamo farlo al volo usando il comando del display come una calcolatrice. La seconda riga di codice sotto fa questo. Possiamo anche controllare il risultato tagliando e incollando il valore della deviazione standard della produzione, che viene fatto nel terzo comando di seguito. I risultati sono fondamentalmente le stesse, la differenza minima è errore di arrotondamento perché la stima memorizzata r (sd) contiene più cifre di accuratezza del valore della deviazione standard visualizzata nell'output. Tipi di risultati restituiti, Classe R e Classe E ora che sapete un po 'di risultati restituiti e come funzionano si è pronti per un po' di più informazioni su di loro. risultati restituiti sono di due tipi principali, R-classe, e l'e-classe (ci sono anche Classe S e Classe C resultsvariables, ma noi non discuterne qui). I comandi che eseguono la stima, per esempio regressioni di tutti i tipi, analisi fattoriale, e anova sono comandi e-classe. Altri comandi, ad esempio riassumono, in correlazione e comandi post-stima, sono comandi R-classe. La distinzione tra i comandi e-Classe R-classe ed è importante perché i negozi Stata risultati di comandi R-classe e classe e in diverse quotplaces. quot Questo ha due conseguenze per voi come un utente. In primo luogo, è necessario sapere se i risultati sono memorizzati in r () o () (così come il nome del risultato) per farne uso. Se non sei sicuro di quale classe di un comando youve Run è in, è possibile cercarlo nel file di aiuto, o quotlookquot in un unico luogo (utilizzando il comando appropriato per elencare i risultati), se i risultati non vengono memorizzati ci sono probabilmente in l'altro. Un potenzialmente più importante ramificazione della differenza nel modo in cui i risultati di comandi R-classe e di e-classe vengono restituiti è che i risultati restituiti sono tenuti in memoria solo fino a quando viene eseguito un altro comando della stessa classe. Cioè, restituiti i risultati di comandi precedenti sono sostituiti da successivi comandi della stessa classe. Al contrario, l'esecuzione di un comando di un'altra classe non influenzerà i risultati restituiti. Ad esempio, se corro una regressione, e poi una seconda regressione, i risultati della prima regressione (memorizzata in e ()) sono sostituiti da quelli per la seconda regressione (memorizzato anche in e ()). Tuttavia, se invece di un secondo di regressione, ho eseguito un comando post-stima, i risultati della regressione rimarrebbero in e (), mentre i risultati del comando post di stima sarebbero messi in R (). Mentre vi è una distinzione tra i due, l'uso effettivo dei risultati di comandi e-Classe R di classe ed è molto simile. Per cominciare, i comandi sono paralleli, per elencare i risultati di R-classe memorizzati nella memoria del comando è la lista tornare. a fare lo stesso per i risultati e di classe la lista eReturn comando. Inoltre, tranne per la differenza di convenzioni di denominazione (r () vs. e ()), i risultati sono accessibili allo stesso modo. L'esempio che segue dimostra questo, in primo luogo abbiamo regredire scrittura femminile e leggere. e quindi utilizzare la lista eReturn a guardare i risultati restituiti. L'elenco dei risultati restituiti per regresso comprende diversi tipi di risultati restituiti, compresi nelle voci scalari, macro, matrici e funzioni. Discuteremo i tipi di risultati restituiti sotto, ma per ora vi mostreremo come è possibile utilizzare lo scalare restituito risultati allo stesso modo in cui abbiamo usato i risultati restituiti da riassumere. Ad esempio, un modo per calcolare la varianza degli errori dopo una regressione è quello di dividere la somma dei quadrati dei residui dai gradi di libertà totali (cioè n-1). La somma dei quadrati dei residui è memorizzato in e (rss) e che la n per l'analisi viene memorizzato in e (N). Di seguito si usa il comando di visualizzazione come un calcolatore, insieme con i risultati restituiti per calcolare la varianza degli errori. Come vengono restituiti risultati: scalari, stringhe, matrici e funzioni Come accennato in precedenza, per entrambi i comandi R-classe e Classe E, ci sono diversi tipi di risultati restituiti, tra cui scalari, stringhe, matrici e funzioni. Negli elenchi dei risultati restituiti, ogni tipo è elencato sotto la propria voce. I risultati elencati sotto la voce quotscalarsquot sono proprio questo, un singolo valore numerico. Il loro utilizzo è discusso in precedenza, quindi ci voleva dire più su di loro in questa sezione. risultati restituiti elencati sotto quotmacrosquot sono generalmente stringhe che danno informazioni sul comando che è stato eseguito. Ad esempio, nei risultati restituiti di per la regressione mostrato sopra, e (cmdline) contiene il comando emesso dall'utente (senza abbreviazioni). Questi sono generalmente utilizzati in programmazione Stata. Risultati elencati sotto quotmatricesquot sono, come ci si aspetterebbe, matrici. Mentre la lista dei risultati restituiti dalla lista di ritorno e la lista erturn si mostrano i valori assunti dalla maggior parte dei risultati restituiti, questo non è pratico con le matrici, invece le dimensioni delle matrici sono elencati. Per visualizzare il contenuto delle matrici è necessario visualizzarli utilizzando i comandi matrice. Lo facciamo di seguito con la matrice dei coefficienti (e (b)) usando il comando lista matrice e (b). (Si noti che c'è un altro modo per accedere coefficienti e le loro errori standard dopo si forma un modello, questo è discusso qui di seguito.) Se si desidera eseguire operazioni di matrice su matrici restituiti, o che desiderano accedere a singoli elementi della matrice, possiamo spostare la matrice salvata come risultato tornato ad una matrice normale Stata. Questo viene fatto nella riga finale della sintassi di seguito. Infine, i risultati restituiti alla voce quotfunctionsquot contenere funzioni che possono essere utilizzati in modo simile ad altre funzioni Stata. La funzione più comune restituito da comandi di stima Stata è probabilmente e (campione). Questa funzione segna campione utilizzato nella stima ultima analisi, questo è utile come set di dati spesso contengono valori mancanti conseguente non tutti i casi dell'insieme di dati utilizzati in una data analisi. Supponendo che il comando di marcia ultima stima è stata la regressione della scrittura femminile e leggere mostrato sopra, la prima riga di codice riportato di seguito utilizza e (campione) per trovare la media di leggere tra quei casi utilizzati nel modello. La seconda riga di codice utilizza e (campione) per creare una nuova variabile denominata bandiera che è uguale a 1 per i casi che sono stati utilizzati per l'analisi, e zero altrimenti. (Nota dato l'esempio di dati non contiene dati mancanti, tutti i casi sono incluse nell'analisi, e la bandiera è una costante uguale a uno.) Coefficienti ed i loro errori standard Come discusso in precedenza, dopo che uno si adatta un modello, coefficienti e il loro standard errori vengono memorizzati in e () in forma matriciale. Queste matrici consentono l'accesso degli utenti ai coefficienti, ma Stata offre un modo ancora più semplice per accedere a queste informazioni da memorizzare nelle variabili di sistema B e SE. Per accedere al valore di un coefficiente di regressione dopo una regressione, tutto quello che bisogna fare è digitare bvarname dove varname è il nome della variabile predittore di cui si desidera esaminare coefficiente. Per accedere l'errore standard, è possibile digitare semplicemente sevarname. Per accedere il coefficiente e l'errore standard della costante usiamo rispettivamente bcons e secons. Di seguito si corre lo stesso modello di regressione abbiamo fatto sopra (omettendo l'uscita), utilizzando femminile e leggere da prevedere scrittura. Una volta che abbiamo stimato il modello, utilizziamo il comando display per mostrare che i valori di b sono uguali ai nostri coefficienti di regressione. Infine, calcolare il valore previsto di scrittura quando una femmina (femmina 1) studente ha un punteggio di lettura di 52. Il contenuto di questo sito web non deve essere interpretata come un'approvazione di un particolare sito web, il libro, o di un prodotto software da parte del Università di California. An Introduzione a Mata Mata è un linguaggio di matrice integrato in Stata, simile in molti modi per R, Matlab o GAUSS. Esso ha alcune caratteristiche uniche e interessanti comunque. Uno è che è un linguaggio compilato anziché interpretato, che migliora le prestazioni. Inoltre è stato parallelizzato a StataMP (disponibile su tutti i server Linux SSCC e Condor) che migliora notevolmente le prestazioni. D'altra parte Mata è abbastanza nuovo e ha onquot non ancora quotcaught al SSCC, in modo che non hanno paragoni nel mondo reale da offrire. Mata non è un sostituto per Stata, né è destinato ad essere un pacchetto statistico autonomo. E 'uno strumento che viene utilizzato al meglio come un supplemento per Stata (o SAS o SPSS), per fare quelle cose Stata non fa bene da solo. In particolare, Mata non funziona nel contesto di un unico insieme di dati, dando maggiore flessibilità. Ma non si dovrebbe cercare di imparare Mata a meno che non si ha già familiarità con Stata o un altro pacchetto statistico. Mata è un linguaggio relativamente levelquot quotlow. La maggior parte del vostro tempo in Stata (o SAS o SPSS) viene speso utilizzando i programmi integrati, trovando la giusta combinazione di opzioni per ottenere Stata a fare quello che vuoi. In Mata si avrà il controllo diretto, dicendo Mata ciò che si vuole fare step-by-step. (L'ottimizzatore Mata, di cui parleremo a lungo, è una notevole eccezione.) Ciò significa fare le cose semplici è di solito più ingombrante a Mata che in Stata, ma Mata ha meno vincoli. Questo articolo è stato scritto soprattutto per le persone che hanno una significativa esperienza utilizzando Stata, SAS o la sintassi SPSS, ma non altri linguaggi di programmazione. Così ci saranno un sacco di enfasi su come imparare a fare cose utili per matrici manipolare, e molti degli esempi sono progettati per dare un'esperienza così facendo oltre ad illustrare un concetto particolare. Matlab e Gauss veterani possono trovare possono sfogliare queste sezioni, concentrandosi su ciò che è nuovo per loro. Programmatori C troveranno che Mata imita C ogni volta che è possibile, in modo che possano probabilmente scremare sezioni su costrutti di programmazione standard come loop. Ma non importa quale sia il tuo background, si impara molto di più se leggete questo articolo al computer, con Stata in esecuzione, ed effettivamente digitare negli esempi. Mata viene eseguito all'interno Stata, in modo da poter utilizzare Mata youll bisogno di sapere come gestire un programma di Stata, denominato file fare. Se non hai mai usato Stata, si prega di leggere la sezione sul fare file in An Introduction to Stata. Interactive Stata (cioè avviarlo e digitare comandi) è un ottimo modo per imparare e questo è il modo youll fare gli esempi in questo articolo. Ma per il lavoro vero e proprio youll voglia di scrivere tutto in file fare. Ci sono diversi file di esempio associati a questo articolo. Ci sono collegamenti a loro nel testo in cui sono utilizzati. Tuttavia, se state usando un server Linux SSCC si consiglia di copiare tutti prima del tempo. Per farlo digitare quanto segue al prompt di Linux: mkdir mataclass cd mataclass cp usrglobalwebssccpubsfiles4-26. La maggior parte dei comandi digitare youll come si legge sono contenuti anche in mataclass. do. I due esempi più importanti si trovano in ex1.do e ex2.do. Mata Basics Per iniziare Mata, prima start up Stata e quindi digitare nella finestra di comando. Stata poi passare a quotMata mode. quot La sua facile dimenticare la modalità che sei in, ma se si guarda con attenzione alla parte inferiore della finestra di revisione, in cui normalmente si vede un periodo di youll ora vedere i due punti. Naturalmente quando si inizia Mata theres una linea orizzontale grande che dice Mata, ma che non sarà visibile a lungo. Questo bar si ricorda anche un altro comando utile: per uscire dalla modalità di Mata, tipo Mentre Stata è organizzato intorno comandi, Mata è organizzato intorno quotstatements. quot Ad esempio, è possibile digitare semplicemente e Mata tornerà Memorizzazione dei risultati in una variabile è altrettanto facile: si noti che non vi era alcuna uscita questa volta. Se volete vedere il valore di x, digitare semplicemente: e youll ottenere 4 indietro. Matrix Operatori Nei nostri esempi precedenti, il segno più ha agito come un operatore: Mata riconobbe come dire quottake la cosa prima che il segno più e la cosa dopo il segno più e riassumere them. quot Mata definisce anche gli operatori di matrice che svolgono le manipolazioni di matrice. Colonna e riga unirsi alla virgola è definito come l'operatore quotcolumn joinquot, o quottake le cose prima e dopo la virgola e metterli accanto a ogni other. quot Ciò significa che è interpretato da Stata come una matrice con una riga e due colonne. In realtà, digitarlo e l'uscita sarà: il backslash è l'operatore quotrow joinquot, o quottake la cosa prima che la barra rovesciata e collocarlo sulla parte superiore della cosa dopo it. quot Così è una matrice con (che non è la divisione.) due righe e una colonna: Le cose per essere operati non sono limitati a scalari, in modo da poter costruire matrici. Messaggi di errore Ci sono dei limiti: ltistmtgt: 3200 Errore di adattabilità R (3200) Non puoi fare una matrice la cui prima fila ha due colonne, ma la cui seconda e terza fila hanno una colonna. Stata definisce ciò come un errore conformabilità, una classe che comprende anche cose come cercare di moltiplicare le matrici cui il numero di colonne della prima matrice è diverso dal numero di righe della seconda. Questo è anche un errore di quotruntimequot. Quando si dà Mata un comunicato, la prima cosa che fa è compilare la dichiarazione in bytecode. A quel punto Mata isnt guardando le quantità specifiche da manipolare. Tutto ciò che vede è qualcosa quotput vicino a qualcos'altro, poi impilare che in cima altri due things. quot che ha senso, quindi la dichiarazione compila con successo. Solo quando viene eseguito il codice non preavviso Mata che le cose per essere impilati avere un diverso numero di colonne. Un errore di compilazione viene generato quando l'istruzione non rende senso non importa quale quantità si collega in esso. Per esempio, non valida espressione R (3000) errori di compilazione di tempo sono sempre il codice di errore 3000, e non lo comprendono ltistmtgt. Notando se un errore è un errore di compilazione o di un errore di runtime può aiutare a trovare il problema. Se fosse runtime, si sa Mata almeno compreso il codice (anche se non c'è nessun garanzia che capito per significare la stessa cosa si capisce che significhi) ed era qualcosa a proposito dei quantitativi specifici che stava lavorando con che ha causato il problema. Se la sua fase di compilazione, il codice doesnt anche senso Mata. Le parentesi Come sei mettendo insieme matrici sei invitati a includere le parentesi per contribuire a chiarire le cose. Il suo lo più per il vostro beneficio: sono tutti uguali a Mata, ma se uno di loro sembra più chiaro a voi con tutti i mezzi utilizzare tale stile in tutti i programmi. (Mi capita di essere un fan del terzo.) Gamma operatori Un'altra serie di strumenti utili per la creazione di matrici sono gli operatori gamma. Il. operatore crea una serie partendo dal numero sulla sinistra fino numero a destra e li rende in un vettore riga. Il. operatore fa lo stesso, ma li mette in un vettore colonna. La sua non è limitato a numeri interi, però: Si noti che il numero sulla destra, non è garantito per essere parte della serie risultante. I risultati di dichiarazioni di matrice possono naturalmente essere salvati in variabili, proprio come le altre dichiarazioni. Per esempio: Si noti come la definizione di z assomiglia molto la dichiarazione che abbiamo provato in precedenza che ha dato un errore di runtime. Si corre ora perché xey entrambi hanno due colonne. Ecco perché si trattava di un errore di runtime e non un errore di tempo di compilazione: dato gli input giusti la dichiarazione può lavorare. Operatori aritmetici Gli operatori aritmetici standard di riconoscere quando uno o entrambi i loro argomenti è una matrice, e agire di conseguenza. Questo include addizione e sottrazione, matrice volte scalare, e pieno moltiplicazione di matrici. Tuttavia, ci sono anche operatori quotcolonquot quale elemento di lavoro per elemento. Per addizione e sottrazione non fa differenza, poiché theyre elemento per elemento comunque. Ma fa una grande differenza con la moltiplicazione. Si consideri il seguente: Operatori logici Gli operatori logici come superiore, inferiore, e pari a vengono anche definiti in entrambe le versioni della matrice e del colon (si noti che l'operatore uguale è, al contrario di per l'assegnazione). Tuttavia, Mata non dispone di un tipo di variabile booleana. Così (come in Stata) operatori logici restituiscono uno per la vera e zero per falso. Se si confrontano due matrici utilizzando standard di maggiore, minore o uguale, Mata restituisce uno se e solo se la condizione è vera per tutti gli elementi delle due matrici. In caso contrario, restituisce zero Questi operatori possono lavorare solo su matrici della stessa dimensione. Nella loro forma colon, il risultato è una matrice contenente tutti i confronti elemento per elemento. La forma colon consente maggiore flessibilità riguardo le dimensioni delle matrici da confrontare. Se un argomento è uno scalare, vettore riga o vettore colonna, verrà duplicata come necessario in modo da corrispondere l'altra matrice. L'operatore di trasposizione è la citazione singolo a destra (). Funziona su un solo oggetto, quello alla sua sinistra, e restituisce la sua trasposta: Non c'è nessun operatore inversa, però. Inversion è effettuata da funzioni. Pedici fare riferimento a un particolare elemento di una matrice, che il numero di riga e colonna in parentesi quadre dopo il nome della matrice. Data la matrice di x sopra definito, x1,1 è 1. x1,2 è 2. ecc pedici possono essere utilizzati su entrambi i lati di un segno di uguale: se una matrice è un vettore riga o colonna, è possibile utilizzare un solo pedice. quotMissingquot per il numero di riga o colonna si intende tutte le righe o colonne. È possibile utilizzare il periodo standard mancanti o semplicemente lasciare il numero di fuori. Così È inoltre possibile sostituire o numero con un vettore riga o colonna (e Stata cura doesnt che quella che si usa). Tuttavia, sono necessari questa volta parentesi così Mata sa dove l'elenco di righe e le colonne finisce iniziare. È possibile ripetere righe o colonne, mescolare l'ordine, o addirittura creare matrici più grandi rispetto all'originale. vettori predefiniti sono anche bene: Si noti che l'utilizzo di indici vettoriali come questo sarà più veloce di moltiplicazione per una matrice di permutazione. Subscripting Ranges Mata si riferisce agli indici weve utilizzati finora come pedice quotlistquot. Ogni elemento che volevamo era indicato in modo esplicito (beh, tranne per la mancanza significato quotallquot). Ma youll spesso vogliono specificare un intervallo di righe e colonne. Theres non molto bisogno di gamme con piccole matrici, in modo da iniziare con la creazione di una matrice più grande: Prendetevi un momento per cercare di capire come questo ha funzionato prima di leggere oltre. La prima riga definisce x1 come un vettore riga utilizzando l'operatore gamma. Il secondo definisce un x2 vettore colonna, sempre utilizzando l'operatore di gamma, ma moltiplicando il risultato per dieci. La terza poi definisce x come dieci copie di x1 impilati uno sopra l'altro più dieci copie x2 posizionati uno accanto all'altro. Il risultato è un dieci per dieci matrice che va 11-110, che renderà più facile dire quali righe e colonne weve estratti nel prossimi esempi. (Sì, ci sono modi più semplici per fare questo usando le funzioni di loop Andor, ma noi havent ancora li coperti.) Un metodo di estrazione di una serie di righe e colonne di x è quello di utilizzare gli operatori nel raggio di indici di elenco. Così, per ottenere le righe e le colonne 3-7 4-8 si potrebbe fare: Questo è esattamente equivalente a digitando: indici True Range, però, sono diversi. Per prima cosa, sono contenuti tra parentesi quadre e il carattere pipe (shift-backslash). Ci sono due elementi, separati da una barra inversa: la riga e la colonna in alto a sinistra del campo desiderato, e in basso a destra del campo desiderato. Così l'equivalente di pensare a come sostituire quotrows quattro a sette e le colonne di tre attraverso eightquot con quoteverything tra quarta fila, terza colonna e riga sette, otto colonne, inclusive. quot mancante può significare diverse cose in pedice gamma. Se usato nello specificare alto a sinistra, significa o la prima riga o la prima colonna. Se utilizzato in specificando l'angolo in basso a destra, significa che sia l'ultima riga o l'ultima colonna. Così è semplicemente tutti x. C'è anche una regola che dice che gli angoli superiori a sinistra ea destra cant essere sulla stessa riga o colonna. Questo significa quotrow tre, dalla terza colonna a end. quot Ottenere i dati da Stata a Mata La maggior parte del tempo che non sarà la creazione di matrici a mano. Invece youll vuole fare matrici che contengono i dati che già avete in Stata. Youre anche probabile che vogliono prendere i risultati Mata e copiarli sul set di dati Stata. La prima domanda è necessario rispondere è se si vuole fare una nuova copia di dati, o se si vuole avere Mata lavorare con i dati Stata direttamente. Mata può definire matrici che sono in realtà quotviewsquot dei dati Stata. Pensate di una vista come solo dare i dati Stata un nome Mata può utilizzare. Vista utilizzano una quantità insignificante di memoria anche se i dati sono molto grandi. Se si modificano le tabelle di una matrice vista, i dati Stata vengono anche cambiati. Se si effettua una copia dei dati invece è possibile apportare modifiche e mantenere l'originale intatta, tuttavia youll usare il doppio della memoria. Per fare copie dei dati youll utilizzano la famiglia stdata di funzioni. Il più semplice e il più veloce è stdata. E 'usato per ottenere un unico numero da vostro set di dati. Prende due argomenti: un numero di osservazione e un numero variabile. Pensate di set di dati come matrice già, e il numero di variabili è semplicemente il numero di colonna della variabile che si desidera. Per vedere in azione, in primo luogo porre fine Mata digitando fine. caricare il set di dati ad esempio automobilistico, la lista di osservazione uno, e tornare in Mata: fine sysuse automatico l in 1 mata Ora conoscere il costo della prima vettura digitando: Il risultato sarà 4099, che corrisponde a quello che hai appena elencato. Provare alcune altre variabili per assicurarsi che avete ottenuto l'idea. Si noti che se si tenta di ottenere la prima colonna youll ottenere un valore mancante. Ecco perché stdata è solo per i dati numerici. Per le stringhe, utilizzare stsdata che opera in più o meno allo stesso modo. Mata può cercare i numeri di colonna per voi utilizzando la funzione stvarindex. Prende il nome della variabile che si desidera e restituisce l'indice. Tuttavia, questo processo sarà rallentare le cose un po '. Si noti che sei invitati a utilizzare l'uscita di una funzione come argomento per un'altra funzione: se si desidera più di un valore, utilizzare stdata. stdata può essere utilizzato proprio come stdata (anche se in quel caso stdata è più veloce). Tuttavia, stdata è più flessibile sugli argomenti che accetterà. Il numero di riga può essere mancante, nel qual caso vengono restituite tutte le osservazioni. Può anche essere un vettore colonna, nel qual caso vengono restituiti solo le osservazioni specificati. Può anche essere una matrice con due colonne. Ogni riga rappresenta quindi una serie di osservazioni. Un manca in questo caso viene interpretato come l'ultima osservazione. Il numero di colonna può anche mancare (tutte le variabili) o un vettore che elenca le variabili desiderate. Può anche accettare i nomi delle variabili anziché il loro numero, anche se questo sarà più lento. Non consente di specificare gli intervalli come si può per le righe. Come stdata. stdata non può gestire le stringhe. Ma vi è un stsdata equivalente. Provare quanto segue: (.) ​​Stdata stdata ((1,3), 2) stdata (1, (2,4)) stdata (1, (quotpricequot, quotrep78quot)) stsdata Naturalmente tutti questi risultati potrebbero essere (, 1). memorizzato in matrici per uso futuro. stdata può anche prendere un terzo argomento: una variabile di selezione. Questo può essere sia l'indice o il nome di una variabile, e se è specificato allora solo osservazioni quando tale variabile non è uguale a zero vengono restituiti. Scegliendo 0 come variabile di selezione ha un significato speciale: in questo caso le osservazioni saranno esclusi se hanno un valore mancante per qualsiasi variabile specificata. stview rende viste sui vostri dati piuttosto che la copia, ma è possibile selezionare le righe e le variabili usando gli stessi metodi esatti come stdata. Tuttavia, stview ti dà i risultati in un modo diverso. Con stdata. la matrice che si voleva era quello che la funzione stdata restituito. È quindi possibile memorizzare i risultati o semplicemente lasciarli essere visualizzati sullo schermo. Con stview. la funzione stessa non restituisce nulla. Invece è necessario passare in una matrice che sarà sostituito con la vista selezionata. Un problema è che la matrice deve esistere prima di passare a Non importa quanto è grande -., Infatti, sarà completamente sostituito - ma se si tenta di passare una matrice di stview che hasnt stato definito in qualche modo youll ottenere un errore di runtime. Un modo semplice è quello di impostare semplicemente la matrice che si desidera utilizzare a zero prima di passarlo in Si può anche farlo all'interno della vostra chiamata di funzione:. X e n ora apparire come un matrici regolari, ma di tenere presente che essi sono in vista infatti . Se il vostro obiettivo in utilizzando una visualizzazione è quello di salvare la memoria, bisogna stare attenti a non fare copie di esso. non crea una nuova vista. Si crea una nuova matrice contenente i valori di x una copia. Più sottilmente, crea una copia della colonna j-esima di x. Se avete bisogno di una colonna particolare di x, si può semplicemente creare una nuova vista: Theres alcun limite al numero di punti di vista diversi è possibile impostare per gli stessi dati. Ottenere i dati da Mata a Stata Se tutto quello che vuole fare è modificare i valori delle variabili esistenti, fare una vista e cambiare a piacimento. Ma se si desidera aggiungere nuove variabili youll bisogno di un altro paio di funzioni. staddvar aggiunge una nuova variabile. Prende due argomenti: il tipo di variabile e il nome della variabile. Esso restituisce l'indice della variabile, sebbene memorizzazione che per uso futuro è opzionale. Consente di creare una nuova variabile: weightmpg (così, libbra-miglia per gallone, una unità solo un ingegnere potrebbe amare). Si noti che la matrice chiamato mpg è ora una vista di tutte le righe della variabile mpg. stview (. weight0 quotweightquot) pmgweight: mpg staddvar (quotlongquot, quotpmgquot) ststore (., quotpmgquot, PMG) Per esplorare questa nuova variabile utilizzando gli strumenti Stata sei familiarità con, end Tipo. Naturalmente questo è un sostituto straordinariamente goffo per lungo pmgmpgweight Gen Questo porta ad un principio generale: utilizzare Mata per quello che il suo bravo, e Stata per quello che il suo bravo. Ci sono una vasta gamma di altre funzioni che è possibile utilizzare per comunicare tra Mata e Stata, tra cui l'aggiunta di osservazioni e ottenere o impostare le macro locali, e () e vettori R (), e molto altro ancora. Vedere il manuale per ulteriori dettagli. Salvataggio e caricamento Mata dati più spesso l'obiettivo di un programma di Mata è quello di prendere i dati Stata, trovare un risultato, e poi o visualizzarla o rimetterlo nella vostra dati Stata. Tuttavia, è possibile salvare Mata si matrici se è necessario. Per salvare una matrice, digitare il nome del file mata matsave matrixlist Sostituire il nome del file con il nome del file che si desidera creare. Stata aggiungerà. mmat al nome del file automaticamente. Sostituire matrixlist con una o più matrici che si desidera salvare (separandoli con spazi), o con un asterisco () per salvare tutte le matrici attualmente in memoria. Per caricare le matrici youve salvato in precedenza, tipo mata matuse filename Questo caricherà tutte le matrici memorizzate nel nome del file. Entrambi i comandi accetterà l'opzione di sostituzione. Per matsave. questo permette Mata per sovrascrivere il file esistente sul disco. Per matuse. questo permette di sovrascrivere Mata matrici esistenti in memoria con gli stessi nomi matrici nel file. Questi comandi sono destinati ad uso interattivo e non possono essere utilizzati nelle funzioni. Se è necessario salvare matrici all'interno di una funzione, controlla fopen. fputmatrix. fgetmatrix. e fclose. Hierarchical Data Hierarchical data has always been a bit awkward to work with in Stata, or any other statistical program that uses a single data matrix. A typical example would be individuals living in households: should each household be one observation, or each individual Either way there are inefficiencies. In Mata you can have it both ways: one matrix of individuals and one matrix of households. The key is linking them together, but subscripts make that easy. As an example, take a look at the hh Stata data set. which is in Stata format. It consists of six individuals living in three households. hh is the household ID, hhType is the type of household, and hhInc is the household income. age and female are individual variables. This data is in the long form, with one observation for each individual, which means that the household variables must be duplicated. end use hh, replace l Now enter mata and load the hh Mata data set . mata mata matuse hh hh ind It contains two matrices, ind and hh. hh contains the household level variables ( hhType and hhInc ). ind contains the individual variables ( age and female ) plus the household ID. Note however, that hh does not contain an ID: the row number is an implicit ID. For example, if you wanted to know what type of household person number two lives in, youd use: ind2,3 is the household ID of the second person, or the row in the hh matrix. Column one of hh is the household type. Of course a regression model may need a single matrix just like Stata does. You can easily recreate the matrix as Stata views it with: Here ind.,3 is a column vector listing which row we want from hh for each row of ind. The resulting rows from hh are then placed next to ind to create x . Matrix Functions Mata has a very large number of matrix functions built into it. This section will hit some of the most useful. Creating Matrices The I function can take one or two arguments. If it is given one argument, it will return an identity matrix of size equal to the argument it was given. If it is given two arguments, it will return a matrix with that number of rows and columns which is full of zeroes except for ones along the principal diagonal. The J function creates a matrix of constants. It takes three arguments: the number of rows of the matrix to be created, the number of columns, and what to put in the matrix. The last argument can be of any type. The e function creates unit vectors: row vectors with a one in one column and zeroes everywhere else. It takes two arguments: the location of the one, and the size of the row vector. Thus you could create an identity matrix by combining e s, though the I function would of course be much easier. The uniform function returns a matrix filled with random numbers distributed uniform(0,1). The size is specified in the same way as with the J function. If youre putting together a matrix which will be symmetric, the makesymmetric function can take care of half the work for you. You put together the lower triangle, and makesymmetric will copy it to the upper triangle (replacing what was there before). It takes one argument, a matrix, and returns the symmetric version. x(1,2)(3,4) ymakesymmetric(x) y Theres a second version, makesymmetric which returns nothing but changes the input matrix instead: The sort function returns a sorted matrix. It takes two arguments: the matrix to sort, and the column(s) to sort by. Note that sort is not a quotstablequot sort. If there are any ties, they will be resolved randomly. Repeat sort(x,1) enough times and you should see some cases when the original second row becomes the first row and others where it stays the second row. If the second argument is a row vector, the matrix will be sorted by the first column listed, then ties resolved by the second column, etc. sort does not change the matrix it is given--it returns a copy. If youd prefer to sort the original matrix without making a copy, use sort . jumble and jumble are the opposite of sort and sort. They take just one argument, a matrix, and put its rows in a random order. Sizes of Matrices rows. cols. and length all take one argument, a matrix. They return the number of rows, the number of columns, and the total number of elements (rowscolumns) in that matrix respectively. rows(x) cols(x) length(x) These can be very useful in for loops where you want to loop over your entire matrix but dont know ahead of time what size it will be. Of course matrices can have missing values just like ordinary Stata data sets. If you need to know the number of missing values in the whole matrix, a row, or a column, use missing. rowmissing or colmissing respectively. missing returns a scalar, rowmissing returns a column vector (one value for each row in the matrix), and colmissing returns a row vector (one value for each column in the matrix). y(1,2,.)(4. 6)(7,8,.) missing(y) rowmissing (y) colmissing(y) To get the number of non-missing values, using nonmissing. rownonmissing. or colnonmissing . Descriptive Statistics Mata has a set of functions for calculating descriptive statistics, though by design its not as rich as Statas. In their simplest and most commonly used forms, all these functions take a single matrix as their argument. Sums may be stretching the definition of quotdescriptive statisticquot but they are very useful. There are three main sum functions: sum. rowsum. and colsum. sum adds up the whole matrix, returning a single number. rowsum adds up each row, returning a column vector. colsum adds up each column, returning a row vector. sum(x) rowsum(x) colsum(x) max and min simply return the largest or smallest element of the matrix. rowmax and rowmin return a column vector containing the largest or smallest element of each row. Likewise, colmax and colmin return a row vector. You can get the largest and smallest elements with the minmax functions, including minmax. rowminmax. and colminmax. They return both the min and the max in either two columns or, in the case of colminmax. two rows. Sometimes youre more interested in where the min or max is than what it is. If so, check out minindex and maxindex. The mean function returns a row vector containing the column means of the matrix. If you need row means, youll need to construct them yourself: You can also get the variance matrix of your matrix with variance, and the correlation matrix with correlation. Matrix Characteristics Mata has functions for finding the most common matrix characteristics. diagonal returns the principal diagonal of a matrix as a row vector. trace returns the sum of the diagonal elements. det returns the determinant (with some round-off error). rank returns the rank of the matrix. As a general rule, using rank to check that a matrix is full rank because a subsequent calculation requires it is redundant--better to let the subsequent calculation fail and handle the failure. Also, when determining the rank of a matrix on a computer a certain tolerance is required for round-off error. Different functions can use different tolerances, so they may disagree with the rank function. Solvers, Inverters and Decomposition Matrix solvers are functions designed to solve the equation AXB for X. Since inverting a matrix A can be done by solving the equation AXI, inverters and solvers are closely related. In addition, the solvers and inverters generally work by doing some sort of decomposition, and the decomposition methods can also be accessed directly. Thus there are several families of three related functions. Which family youll choose depends on the properties of your matrix. Well focus on Cholesky methods, but the others work in a very similar way. The Cholesky Decomposition decomposes a symmetric, positive definite matrix into a lower triangular matrix times its transpose. The results can be used to solve matrix equations and find inverses much more quickly than more general methods. To get an example matrix we can work with, we can take advantage of the fact that if x is of full rank, xx is symmetric and positive definite--and random matrices are all but certain to be full rank. So: The cholesky function takes one argument, a matrix, and returns a lower triangular matrix which is its Cholesky Decomposition. To verify that it works, note that is the same as a. To illustrate solving a matrix equation, we need a right-hand side. The cholsolve function takes two arguments, both matrices. It them sets up and solves the equation AXB where A is the first matrix and B is the second, returning X. To verify that it worked, note that The cholinv function takes one argument, a matrix, and returns its inverse: Again, to verify it worked try The result should be an identity matrix. Youll notice that round-off error makes some of the zeroes not quite zeroes, but its as close as you can get using a computer. If your matrix is square but not symmetric, LU Decomposition can do similar things (though its slower than Cholesky). The lud function is equivalent to cholesky. but more complex because it has to give back three results. Thus the matrices to store the results have to be passed in, similar to stview . LU Decomposition breaks a matrix into a lower triangular matrix L, an upper triangular matrix U, and a permutation matrix P. But Mata, instead of using a matrix P, gives a column vector p which can be used with subscripting to do the same thing. Take look at L. U. and p. To verify that it worked, see that is a. Note how were using subscripts to pull rows from a result, not an existing matrix, and yet it works just fine. lusolve and luinv are much simpler--in fact the usage is identical to cholsolv and cholinv. Try Similar functions exist for QR decomposition ( qrd. qrsolve. qrinv ). Singular value decomposition has svd and svsolve. but the related inverter is pinv (which returns the Moore-Penrose pseudoinverse). invsym. for inverting symmetric matrices, has no related decomposition or solver functions. It is, however, what Stata suggests for linear regression. Example: Linear Regression Youre now prepared to do the most common matrix manipulation of all, at least at the SSCC. Since we have the automobile data set loaded, lets regress price on mpg, rep78, foreign and weight to see which characteristics American consumers were willing to pay for in 1978. One complication is that rep78 is missing for some observations, so youll need to drop them from your regression. Of course you could just drop them from the data set entirely, but lets assume that you want to keep them for later use. The first step is to mark the observations you can actually use. Exit Mata by typing end. then create a new variable touse which is one if all the variables are non-missing and zero otherwise. gen touse(price. amp mpg. amp rep78. amp weight. amp foreign.) Of course in reality the only variable that is ever missing is rep78. but well pretend we didnt know that ahead of time. If you wanted to run a regression on a subpopulation (say, just the foreign cars) you could add a condition to mark the subsample too. Now go back into Mata by typing mata. Next make x a view onto all the independant variables and y a view onto the dependant variable. touse is the selection variable for both views: observations will only be included if it is equal to one. stview(x0. (quotmpgquot, quotrep78quot, quotweightquot, quotforeignquot), quottousequot) stview(y0. (quotpricequot), quottousequot) To include a constant term in the regression you need to add a column of ones to x. Use the J function to create it, then add it to x with the comma (column join) operator. Now you can find the betas: Of course they dont mean anything without standard errors. Start by finding the residuals: Then the variance-covariance matrix is The standard errors for each beta can be extracted using the diagonal function, along with sqrt. which takes the (element by element) square root. The t-statistic is the beta divided by the standard error, but this is an element by element division so you need to use the colon operator. To find the p-values requires the ttail function, which takes as arguments the degrees of freedom and the t-statistic and returns the probability. It is a single-tailed test however, so you need to multiply by two. To put your results together in a readable form, try: Now exit Mata again, and check your results against reg price mpg rep78 weight foreign They should be identical. In regular Stata, if is almost always used to define which observations a command should act on. Its the equivalent to SQLs where clause. In Mata, if is only used to control program flow. (Though Stata can use if in this way as well--see the appropriate section of Programming in Stata .) The most basic form of if is simply if ( condition ) statement If condition is true, then statement is executed. Otherwise it is skipped entirely. Note that the condition must be in parentheses. If you want to do more than one thing if the condition is true, use the following syntax: if ( condition ) statements You can also use else to specify things that should happen if the condition is not true. Thus you can create fairly elaborate structures: if ( condition1 ) statements1 else if ( condition2 ) statements2 else statements3 Keep in mind that each condition has a single result, either true or false. For example, If x is a matrix you cant write if (xgt5) and then list things you want done for those elements of x which are greater than 5. You can write if (x1,1gt5) followed by if (x1,2gt5) etc. but of course the easy way to do that is using a loop. Mata has while. do - while. and for loops available (plus goto for easier conversion of FORTRAN code, but we dont want to endorse spaghetti logic). while looks very similar to if: while ( condition ) statements The difference is that the statements will be executed over and over as long as the condition is true. Thus your first concern should be to make sure that at some point the condition will become false, or the loop will run forever. Note that x is shorthand for xx1. Even though we said xlt5, the final value of x is six. Thats because the loop only decides to end when x becomes six. You can also put a while loop with a single statement in a single line. This loop is perfectly legal, but it didnt do anything. The reason is that the condition started out false, so the loop never executed at all. Sometimes you need to make sure that a loop runs at least once, and thats a job for do-while. A do-while loop starts with do and ends with the while condition. The statements are always executed at least once. If the condition is true, the loop starts over again from the top. Note that x is increased to seven even though it started out greater than five. One typical use for do-while loops is to do something until it converges: do complex mathematical stuff while (abs(newvalue-oldvalue)gttolerance) But unless youre absolutely certain your process will actually converge, youd better put in an escape clause that tells it to stop after a while whether it converges or not: do complex mathematical stuff while (abs(newValue-oldValue)gttolerance amp iterationltmaxIterations) Recall our first while loop: This is such a common structure that programmers wanted a quicker way to construct it. The answer was for loops. The for loop equivalent to this while loop is Note the three components: an initialization step, a rule for when the loop should end, and some sort of progression towards that ending. Strictly speaking you can skip the initialization and the progression--just leave the semi-colons as placeholders. All this really means though is that youre promising to take care of those steps yourself. In particular x must already be defined and you need to make sure the loop will in fact end. By far the most common use of for loops is to loop over the elements of a matrix. Writing Your Own Functions Mata allows you to write your own functions--in fact many of the standard functions weve used are written in Mata. As youve seen, calling a function is a matter of typing the function name and then giving a list of arguments in parentheses. Defining a function follows the same structure: first the word function to tell Mata what youre doing, then a name, and then a list of arguments in parentheses. The body of the function follows in curly brackets. If you want your function to return a result, one of the statements in the body needs to be return. followed by the thing to return in parentheses. function myfunction(x, y) statements return(z) As an example, consider the following (not terribly useful) function. function doubleAndSum(x) xx2 return(sum(x)) This function takes a matrix, multiplies it by the scalar two, and returns the sum of all the elements in the matrix. To test it, try the following: mI(3) doubleAndSum(m) m Note that you passed in a matrix called m even though the function calls it x. Thats fine: the doubleAndSum function refers to whatever it is given as x. In fact the input doesnt even have to have a name. In the first case, the argument passed in is the result of the I function. In the second, it is a row vector defined on the spot. On the other hand, in these cases we cant see how the input matrix was doubled as they arent stored anywhere. Function arguments in Mata are quotpassed by reference. quot If a function changes one of the arguments it is given, that change persists even after the function is completed. Example: Loops and Functions Our next example comes from basic physics, but the real point is using loops and functions to get useful results. The classic equation for the motion of a body under uniform acceleration is simply Depending on the level of mathematics used in your last physics course, you may or may not have worked with that as a vector equation, where x, x0,v0 and a are vectors with one number for each dimension. The vector component makes this a good problem for Mata. Lets track and plot the movement of a falling object for ten seconds. Begin by defining a function x which takes x0,v0,a and t as arguments and returns the position of the body at time t. This is a good point to pause and test what weve done so far before moving on. Try: This represents a body just moving at 10 meterssecond (not that Mata cares about the units) in the positive x direction, so it will be easy to tell if your function works properly or not. should give 100,0,0. But that doesnt guarantee our acceleration term is right, so try This should give 0,0,50. Now well set up the actual values we want to examine: This is an object starting 500 meters up, moving to the right at 100 meters per second, and falling under normal earth gravity (about 9.8 meters per second squared). Note how we suddenly switched from three dimensions to two. Your function wont care in the slightest since theyre all still matrices, but its a lot easier to plot on a two dimensional graph. Now you need somewhere to put the results. Mata doesnt do graphs, so youll have to use Stata to plot them. But first we have to think about what the results will be. Your function provides a snapshot of the object at any given time, so well take a thousand snapshots spread across the ten second span. Each snapshot will have two numbers, an x coordinate and a y coordinate, so those will be your variables (though to Mata theyre just column one and column two of the x row vector). Get out of Mata, then use standard Stata commands to set up the observations and variables youll need. Now get back into Mata, and set up a view of these variables. Call it r (as in results): Now youre ready to loop over your 1000 snapshots and get the results of the x function for each one: Note how the time for each snapshot, i100, spreads them evenly across 10 seconds as i goes from 1 to 1000. Finally exit Mata again, and create the graph. end scatter y x Its the classic parabola of projectile motion as you no doubt expected, but of course the real point was to practice using functions and loops to generate and work with data. As soon as we defined the x() function, Mata compiled it into quotobject code. quot This is not quite the same as machine-language code that can be run all by itself. But it is something Mata can understand and run very quickly. If you were planning to use this function in the future you could save the object code. Then future programs wouldnt need to spend time compiling it. If this is something youre interested in, see the mlib and mata mosave commands. This example is contained in ex1.do. A pointer is a variable which contains the memory address of another variable or matrix. Thus it quotpointsquot to that other variable. In principle a pointer is just a number, but youll never work with the number directly. Instead, youll use two operators, amp and . Computer scientists call these the quotreferencequot and quotdereferencequot operators, but I like to think of them as quotthe address ofquot and quotthe thing at. quot Consider the following: First we define a matrix x so we have something to work with. Then p is defined as quotthe address of x. quot Thus p or quotthe thing at p quot is just another name for x. You can use subscripts with p just like you would with x. but note how you have to put p in parenthesis first. (p)2,2 means quotfind the thing at the address contained in p and get the value at row two, column two. quot p2,2 would mean quotget the value at row two, column two of p. and then find the thing at that addressquot which wont work because p is a scalar. Note that the matrix you are pointing to doesnt need to have a name. For example: makes p point to a 5x5 matrix of zeroes (since thats what J returns) even though theres no direct name for that matrix. Its only accessible as p. One use for pointers is to construct data structures Mata doesnt handle automatically. For example, you can construct a three dimensional matrix by making a two dimensional matrix of pointers, each of which points to a column vector. The first step is to define the matrix that will contain the pointers. Matrices cant switch between containing numbers and containing pointers, so you need to make sure that the matrix is defined as containing pointers. On the other hand, we dont have any actual pointers to store yet. Thus well set the initial values of the matrix to NULL. a special pointer that doesnt point to anything. Now loop over all the elements of x, and make each one a pointer to a new (and unique) column vector. To work with an element i, j,k of your three dimensional matrix youd use the following: Yes, the parentheses are essential. Its rather awkward though, so if you were going to work with such matrices a lot consider defining the following functions: function put(val, x,i, j,k) Usage: value to put, matrix to put it in, i, j, k to put it at. ((xi, j))k,1val function get(x, i,j, k) Usage: matrix to get from, i, j, k of value to get. return(((xi, j))k,1) Then you can do things like: The fact that you cant mix pointers and regular data within a matrix does limit your flexibility. You cant, for example, have a matrix of individual data including pointers to other data relevant to the individual. You can, however, have two parallel matrices, one containing numbers and the other containing pointers. If row i represents the same individual in both matrices, you can pull information from either or both as needed. Its also possible to create a pointer to a function. Given your existing function doubleAndSum() try: Note how Mata distinguishes between x() the function and x the variable by the parentheses, even though youre not passing in any arguments when you define a pointer. The main reason youd want to create a pointer to a function is so that you can use that pointer as the argument of a function. For example, Matas optimizer functions have you pass in a pointer to the function which is to be optimized. The Mata Optimizer Many statistical operations involve maximizing or minimizing some quantity--maximum likelihood estimation being the obvious example. Mata includes an optimizer for these operations. Matas optimizer is actually a family of functions which define and then solve an optimization problem. Evaluator functions The first function must be written by you: the function to be maximized or minimized. This function can do whatever you want, it may be very complex or very simple, but it must accept a certain set of arguments and return the proper result in order for the optimizer to use it. You can name the arguments whatever you like, but there must be the right number and each must have the proper meaning. Optimization problems are described using a variety of notations, but if we consider the problem of maximizing yf(x), the Mata version of f() must take the following arguments: a number indicating whether the function is supposed to calculate (0) just f(x), (1) yf(x) and f(x) or (2) f(x), f(x) and f(x) x (the thing that changes as the optimizer looks for the max) y (where f(x) will be stored) A variable to store f(x) if calculated A variable to store f(x) if calculated (often H since it is the hessian in multivariate problems) If you can find an analytic solution for f(x) and f(x) and code them in, Matas optimizer will be much faster and more accurate. This is often impractical though, and Mata is perfectly willing to find numeric approximations to the derivatives it needs. Note, however, that even if your function never calculates derivatives it must still accept variables where they can be stored. So lets try a very simple function: y-x4. If youre willing to let Mata find all the derivatives for you, you can code this as the following: Note that todo (the number telling the function whether to calculate derivatives or not), g. and H are completely unused, but must still be accepted. Note also that we could change all the names. We could call x Fred and y George as long as Georgef(Fred). If were not quite so lazy we can also code the derivatives for this function quite easily: In this case, both x and y were scalars, but youre not limited to scalars. Consider y-x14 - x24 The two xs are stored in a row vector x, so x1 is x1 and x2 is x2. Making sure the optimizer sends in the right size of x is one of the steps in setting it up. In many cases the quantity youll want to maximize will be the sum of a column. For example, in maximum likelihood you will probably create a column where each row gives an observations contribution to the likelihood function. Matas optimizer will do this automatically with the proper settings, so the previous function could be recast as: Note how the function had to define y as a column vector of the proper size--otherwise it is a scalar. In statistical applications the quantity to be maximized will depend not just on parameters that can vary (x in our problems thus far) but on data that do not vary. Matas optimizer can be set up to pass up to nine additional arguments to your evaluator function, which can contain the data. They go after the first two arguments (and before the final three). This calls for a change of variable names. Consider maximizing sf(b). Well now use x for the data matrix. Then the function definition would be: function f(todo, b, x, s, g, H) x can contain both the independent and dependant variables, but if its easier to work with a separate matrix y, then the definition becomes: function f(todo, b, x, y, s, g, H) Well do an example using this kind of evaluator shortly. Setting Up and Running an Optimization Problem Once youve got your evaluator function defined, youre ready to set up the optimization problem. The first step is to call the optimizeinit function. optimizeinit takes no arguments and returns a variable containing a description of your optimization problem. Youll never look at this description, but you will pass it in to all the other optimization functions. Next tell it where to find the evaluator: The first argument is the problem description, and the second is a pointer to the evaluator function you already defined. Now give it a starting value for x--remember the f() function is in the form yf(x). Of course the correct answer is zero, but we want it to do some work Now youre ready to actually run the optimizer: This returns the value of x which maximizes f(x). You may want to store this in a variable: If we want to use the g(x) function instead, theres one additional step. Recall that in g(x) we coded the first and second derivatives ourselves so Mata doesnt have to approximate them. Mata refers to this as a quotd2quot evaluator. A quotd1quot evaluator codes just the first derivative, and a quotd0quot evaluator codes no derivatives at all (the f(x) function is a d0 evaluator). Mata will assume functions are d0 unless we say otherwise, so you need to add: Note that the optimizer doesnt care what order all the initialization functions are called in, as long as theyre before the actual optimize(). What about h(x1, x2) Its identical to f(x), except that we need to set an initial value for both variables: In doing so we also tell Mata that future xs must have two columns. Finally, i(x1,x2), where the function to be maximized is the column sum of y, is what Mata calls a quotv0quot evaluator. The quotvquot is for vector, and the quot0quot again means that we didnt code any derivatives. Thus we need To see the complete code to run the optimizer with each evaluator, see the the last parts of mataclass. do. There is one setup function we havent needed to call but you should know: changes the problem from maximizing the function (the default) to minimizing it. Example: Ranking Teams As a final, extended example, consider a problem familiar to any sports fan: determining how good a team is based on its wonloss record. Well assume that a team can be characterized by a single quotstrengthquot variable s. If team i plays team j, well assume that the probability of team i winning is given by exp(si-sj)(1exp(si-sj)), better known to Stata as invlogit(si-sj). Then, given a record of games played and who won, we can find a set of values for s that maximizes the probability of the given outcome. Well do a Monte Carlo study by first creating data which fits our assumptions, and then seeing how well the method works. Creating the Data The first step is to create the data. This is an exercise in matrix manipulation, so if you want to focus on the optimization part of the problem feel free to skip ahead. On the other hand, most readers will benefit from some practice in this area. First create a row vector str containing the real strengths of each team. For simplicity, use the uniform function, giving strengths distributed uniform(0,1). For our example well make 50 teams: Note that the column number within the str vector acts as a sort of team ID. Next we need a way to keep track of who played who. Well create a two-column matrix, where each row is a game and the two columns will contain the IDs of the two teams who played in that game. For brevity well call the team in column one the quothomequot team and the team in column two the quotvisitingquot team. Well assume that each team plays 20 games, 10 as the home team and 10 as the visiting team. Begin by creating a column vector teams containing the 50 teams using the range operator: Now create a column vector season which is just ten copies of teams stacked on top of each other: seasonteams for(i1ilt10i) seasonseasonteams This represents the home teams. Next assign the visiting teams by taking the same vector, putting it in a random order, and column joining it to the original: The only trouble is, its entirely possible for a team to be randomly assigned to play itself. This wouldnt really bother our estimator, but it does offend any claims that this represents the real world. More importantly, fixing it is good practice. Create a column vector same with the same number of rows as season which contains a 1 if the home and visiting teams are the same and a 0 if they are not: If a game has the a team playing itself, we will swap the visiting team with the visiting team of a randomly chosen game. Since its possible we might get the same team yet again, well keep checking and swapping until there are no more games between the same team. Heres the code: while (max(same)1) for(i1 iltrows(season) i) if (samei) swaptrunc(uniform(1,1)rows(season))1 tempseasonswap,2 seasonswap,2seasoni,2 seasoni,2temp same(season.,1:season.,2) Note how max(same) will be zero if there are no longer any games where the home and visiting teams are the same, so thats how we know when were done. We then loop over the rows, stopping to change those where samei is one (or true). In those cases, we pick a random row and swap visiting teams with it, using a temp variable to store its visiting teams ID as we do. We then recalculate same based on the new version of season before the while condition is reevaluated. Now we need to decide who won each game. Well create a column vector winner, which will contain a 1 if the home team won and a zero if the visiting team won. winnerJ(rows(season),1,.) for(i1 iltrows(season) i) winneriuniform(1,1):ltinvlogit(strseasoni,1-strseasoni,2) Note how indexing is used to pull up the strength ( str ) of the appropriate team--well be doing that a lot. The Maximum Likelihood Estimator Now were ready to construct the (log) likelihood function to be maximized. Well start with a version thats easy to understand, and try to make it efficient later. function llf(todo, strhat, season, winner, llf, g,H) llfJ(rows(season),1,.) for(i1iltrows(season)i) if (winneri) llfilog(invlogit(strhatseasoni,1-strhatseasoni,2)) else llfilog(invlogit(strhatseasoni,2-strhatseasoni,1)) This will be a v0 evaluator which takes season and winner as additional arguments. The estimated strengths are stored in strhat, and the column of log likelihoods (which will be summed automatically by virtue of being v0) is stored in llf . We have two possible outcomes, and the formula for finding the log likelihood is different in each outcome. For now well handle the two possibilities with an ifelse structure, but there are more efficient ways. Now to set up the optimization problem: soptimizeinit() optimizeinitevaluator(s, ampllf()) optimizeinitevaluatortype(s, quotv0quot) strhat0J(1,rows(teams),.5) optimizeinitparams(s, strhat0) optimizeinitargument(s,1,season) optimizeinitargument(s,2,winner) strhat1optimize(s) Most of these youve seen before. Note that strhat0 is the vector of starting values for our estimate of str. Since the actual strengths are distributed uniform(0,1) well start by setting them all to 0.5. What is new is optimizeinitarguments. This is where you tell the optimizer to pass in season and winner to your evaluator. As you see optimizeinitarguments takes three arguments: the optimization problem, the number of the argument youre setting, and what to pass in. Run the code. It will take a while but it should work. Efficiency So how can we make it faster It would be nice if we didnt have to figure out which formula to use for the likelihood. So lets rearrange the data a bit: instead of column one being the quothomequot team and column two the quotvisitingquot team, terms which have no real meaning in our model, lets make column one the winner and column two the loser. Create a new matrix season2 with the new arrangement: season2J(rows(season),2,.) for(i1iltrows(season)i) if (winneri) season2i,.seasoni,. else season2i,.seasoni,(2,1) Of course this loop takes time to run, but it only runs once. Its the evaluator that must be run over and over, so taking a bit more time to set things up so that the evaluator runs faster is well worth it. One general principle when it comes to writing fast Mata code is that matrix operations are faster than loops you write out. Theres no matrix operation that would allow you to look in one matrix for the estimated strength of a team identified in another matrix, but you can take calculating the log and invlogit functions out of the loop. Heres a second and more efficient version of the evaluator function: function llf2(todo, strhat, season, llf, g,H) xJ(rows(season),1,.) for(i1iltrows(season)i) xistrhatseasoni,1-strhatseasoni,2 llflog(invlogit(x)) Note how it doesnt need to have the winner matrix passed in anymore--it expects a version of season ( season2 ) that conveys that information by which team is in column one. Heres the setup needed to run this version: soptimizeinit() optimizeinitevaluator(s, ampllf2()) optimizeinitevaluatortype(s, quotv0quot) strhat0J(1,rows(teams),.5) optimizeinitparams(s, strhat0) optimizeinitargument(s,1,season2) strhat2optimize(s) Youll see that this runs in about half the time of the original. Most of the gain comes from moving the calculation of log and invlogit out of the loop. For those looking for the absolute best performance, consider turning on matastrict ( mata set matastrict on ). Matastrict requires that you declare the names and types of all variables before using them rather than letting Mata choose. Mata has more variable types than most languages, and they can be confusing. On the other hand, declaring your variables can help you avoid errors. More importantly, Matas compiler can use the additional information to create slightly more efficient object code. If you are interested in using matastrict, see the manuals and especially the section on declarations. Constraints One characteristic of this model is that only the difference between teams is identified. You could add 100 or -1,000,000 to all the strengths and the probability of each outcome would remain the same. Thus two different runs on the same data could give very different numbers and both be right. However, if we constrain just one strength to be a given number, then all the strengths are identified. Matas optimizer accepts constraints on the parameters in the form of two matrices C and c. The parameters p are then constrained such that Cpc . Lets constrain the strength of team one to be zero. To implement this, the C matrix needs to be a row vector with a column for each team. It will have a one in the first column and a zero in all other columns, which makes it a unit vector. c will be simply the scalar zero. You then pass in this constraint using the optimizeinitconstraints function. It takes two arguments: the problem s. as usual, and then a matrix which is the row join of C and c. Im not sure why it doesnt just take them as two separate arguments, but its easy to join them. Since youve made no other changes to the problem, you can simply run it again by calling optimize. This version will actually run significantly faster. As an exercise, consider constructing some sort of metric for how well your estimator does. (One easy one would be how often it correctly identifies the best team.) Then vary the number of games per season and see how many it takes to get reasonable accuracy. However, dont do this if you want to continue to take the ranking systems of actual sports seriously. This example (with additional commands for timing each method) is found in ex2.do. Learning More This article has just scratched the surface of whats possible in Mata. Theres obviously much more to learn, and even more to be looked up when you need it. As usual, Stata Corp. has included most of the Mata documentation in the online help. There is one trick though: to get help for Mata you need to type help mata topic rather than just help topic. This is especially important for functions and such that exist in both Mata and Stata. For example, compare the results of the following: help abs help mata abs A couple useful starting places: help mata help mata functions The Mata manuals are available in the CDE Library and the 4218 lab. There are two books, but they should be thought of as two volumes of the same manual. You are also welcome to ask the Help Desk for assistance. Mata is new to us as well, but well try to figure things out together. Last Revised: 10242007

No comments:

Post a Comment