/* * ------------------------------------------------------------------------- * $Id: ChartReturns.java,v 1.6 2004/05/26 18:54:54 estewart Exp $ * ------------------------------------------------------------------------- * Copyright (c) 1999 Visual Numerics Inc. All Rights Reserved. * * This software is confidential information which is proprietary to * and a trade secret of Visual Numerics, Inc. Use, duplication or * disclosure is subject to the terms of an appropriate license * agreement. * * VISUAL NUMERICS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. VISUAL * NUMERICS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR * ITS DERIVATIVES. *-------------------------------------------------------------------------- */ package com.imsl.demo.risk; import com.imsl.chart.*; import com.imsl.stat.Summary; import java.text.*; import javax.swing.*; /** * * @author brophy * @created January 31, 2002 */ public class ChartReturns extends JFrameChart { private Database db; private AxisXY axis; private Bar bar; private Statistics statistics; private Options options; /** Creates new ChartReturns */ public ChartReturns(Database db, String ticker) { this.db = db; setTitle("Risk Analysis"); Object l[] = getListeners(java.awt.event.WindowListener.class); for (int k = 0; k < l.length; k++) { removeWindowListener((java.awt.event.WindowListener)l[k]); } Chart chart = getChart(); chart.setTextColor("purple"); //chart.getBackground().setFillColor(new java.awt.Color(240,240,240)); axis = new AxisXY(chart); axis.getAxisX().getAxisTitle().setTitle("Daily Returns"); axis.getAxisY().getAxisTitle().setTitle("Probability"); axis.getAxisX().getAxisLabel().setTextFormat(new java.text.DecimalFormat("0.0%")); axis.getAxisY().getAxisLabel().setTextFormat(new java.text.DecimalFormat("0%")); statistics = new Statistics(); statistics.setLocation(100,300); statistics.show(); String tickers[] = db.getTickers(); compute(tickers[0]); options = new Options(tickers); options.setLocation(25,500); options.show(); } public void setVisible(boolean vis) { super.setVisible(vis); if (vis) { java.awt.Point chartLoc = getLocationOnScreen(); java.awt.Dimension optionsSize = options.getSize(); options.setLocation(chartLoc.x-optionsSize.width,chartLoc.y); java.awt.Dimension statSize = statistics.getSize(); statistics.setLocation(chartLoc.x-statSize.width,chartLoc.y+optionsSize.height); } statistics.setVisible(vis); options.setVisible(vis); } private void compute(String ticker) { if (bar != null) bar.remove(); Database.Series series = db.getSeries(ticker); int nSamples = series.close.length - 1; int nBins = 50; double max = 0.05; Summary s = new Summary(); // Setup the bins double bins[] = new double[nBins]; double dx = 2.*max/nBins; double x[] = new double[nBins]; for (int k = 0; k < nBins; k++) { x[k] = -max + (k+0.5)*dx; } for (int k = 0; k < nSamples; k++) { double t = (series.close[k+1]-series.close[k])/series.close[k]; s.update(t); int j = (int)Math.round((t+max-0.5*dx)/dx); if (j >= 0 && j < nBins) bins[j]++; } getChart().getChartTitle().setTitle("Returns for "+ticker.toUpperCase()); for (int k = 0; k < bins.length; k++) bins[k] /= nSamples; bar = new Bar(axis, x, bins); bar.setBarType(Data.BAR_TYPE_VERTICAL); bar.setFillColor("green"); bar.setBarWidth(0.5*dx); StringBuffer sb = new StringBuffer(2048); sb.append("<html><body>"); sb.append("<p align='center'><b>"+ticker+"</b></p>"); sb.append("<table>"); appendRow(sb, "Mean", s.getMean()); appendRow(sb, "StandardDeviation", s.getStandardDeviation()); appendRow(sb, "Variance", s.getVariance()); appendRow(sb, "Skewness", s.getSkewness()); appendRow(sb, "Kurtosis", s.getKurtosis()); sb.append("</table></body></html>"); statistics.setText(sb.toString()); statistics.repaint(); } static final DecimalFormat formatStat = new DecimalFormat("0.00E0"); private void appendRow(StringBuffer sb, String title, double value) { sb.append("<tr><td align='right'><i>"); sb.append(title); sb.append("</i></td><td align='left'>"); sb.append(formatStat.format(value)); sb.append("</td><tr>"); } private class Options extends JDialog { public Options(final String tickers[]) { super(ChartReturns.this, false); setTitle("Select"); getContentPane().setLayout(new java.awt.GridLayout(tickers.length, 0)); ButtonGroup group = new ButtonGroup(); for (int k = 0; k < tickers.length; k++) { final String ticker = tickers[k]; JRadioButton button = new JRadioButton(tickers[k]); group.add(button); button.setSelected(k == 0); button.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { compute(ticker); ChartReturns.this.repaint(); } }); getContentPane().add(button); } pack(); } } private class Statistics extends JDialog { private JEditorPane jEditorPane; public Statistics() { super(ChartReturns.this, false); setTitle("Statistics"); jEditorPane = new javax.swing.JEditorPane(); jEditorPane.setContentType("text/html"); jEditorPane.setPreferredSize(new java.awt.Dimension(250,250)); getContentPane().add(jEditorPane); pack(); } void setText(String text) { jEditorPane.setText(text); } } }