Tuesday, November 28, 2017

Java Swing Ejercicio (Renta Fija 2)

Java Swing Ejercicio (Renta Fija 2)

Capitalización Continua

En el ejercicio Java Swing (Renta Fija 1) se identificaron varias expresiones para encontrar el valor futuro de una cantidad monetaria. La capitalización continua surge como concepto, cuando se piensa en lo siguiente: ¿Qué valor futuro se obtiene si la capitalización de los intereses es cada segundo - continua -.?

Para dar respuesta a ésta pregunta, la expresión del Valor Futuro Capitalización No Anual

$$ \begin{gather} FV &= PV * (1 + \frac{i}{m})^{n*m} \text{ ........(1)}\\ \end{gather}$$

donde

FV = Valor futuro.
PV = Valor presente.
i = Interés Anual.
n = Número de periodos.
m = Número de pagos en el año.

Reemplazando $$m = \infty$$

surge el concepto de límite. Reescribiendo (1) utilizando el concepto de limite se tiene:

$$ \begin{gather} FV &= \lim_{m \to \infty} PV * (1 + \frac{i}{m})^{n*m} \text{ ........(2)}\\ \end{gather}$$ La solución del limite se expone a continuación: $$ \begin{align} FV &= \lim_{m \to \infty} PV * (1 + \frac{i}{m})^{n*m} \\ Ln [FV] &= Ln [PV] + n * \lim_{m \to \infty} m * LN[(1 + \frac{i}{m})] \\ Ln [FV] &= Ln [PV] + n * \lim_{m \to \infty} \frac{LN[(1 + \frac{i}{m})]}{\frac{1}{m}}\\ \text{(Aplico L´Hopital en el límite)} \\ Ln [FV] &= Ln [PV] + n * \lim_{m \to \infty} \frac{\frac{\frac{-i}{m}}{(1 + \frac{i}{m})}}{\frac{-1}{m}} \\ Ln [FV] &= Ln [PV] + n * i * \lim_{m \to \infty} \frac{1}{(1 + \frac{1}{m})} \\ Ln [FV] &= Ln [PV] + n * i \\ e^{Ln [FV]} &= e^{Ln [PV] + n * i} \\ e^{Ln [FV]} &= e^{Ln [PV]} * e^{n * i} \\ FV &= PV * e^{n * i} \text{ ........(3)} \\ \end{align}$$

Las expresiones (1) y (3) están relacionadas para un valor de m grande.

Ejercicio en Java

Sea \( \theta = \) Error de aproximación tal que

$$ \theta = PV * e^{n * i} - PV * (1 + \frac{i}{m})^{n*m}$$ Para que valor de m se obtiene un error de aproximación igual 0.00001;

Solución

Para una correcta compilación es necesario anexar los códigos en un paquete de nombre: deltaerror.

Delta Class

/* Delta: Measure aproximation error as the difference between 
;continuous compounding; and discrete compounding.
 */

package deltaerror;
import java.util.*;

public class Delta {
    private double m, n, interes;
    private List mValues = new ArrayList<>();
    private List deltaValues = new ArrayList<>();
    
    public Delta(double m, double n, double interes){
        this.m = m;
        this.n = n;
        this.interes = interes;
    }
    public void getMDelta(double errorDeAproximacion){
        double error = 10;
        while(error > errorDeAproximacion){
            // Add values to ListArrays nvalues
            mValues.add(m);
            this.m++;
            
            error = Math.exp(this.n * this.interes)-Math.pow((1+(this.interes/this.m)),this.n*this.m);
            // Add values to ListArrays deltaValues
            deltaValues.add(error);
        }
    }
    
    public List getMValues(){return mValues;} // X value
    public List getDeltaValues(){return deltaValues;} // Y value
}


DeltaPlot Class (main)

package deltaerror;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/** Steps to implement the Form with Swing Fx
 * 1) Define variables and Controls
 * 2) Initialize Variables and Controls
 * 3) Create a LayOut (HBox) that holds: eltaLabel, deltaText and Plot Button
 * 4) Create a Layout (VBox) that holds: HBox, and XYChart
 * 5) Set the Scene
 * 6) Set the Stage
 * 7) Implements Class Delta that calculate approximation error (Delta.java)
 * 8) Add action to Plot Button.
*/



public class DeltaPlot extends Application {
    
    // Step 1:Start
    // ********* Variables & Controls ************
    private final NumberAxis xAxis, yAxis;  // XY axis
    private final TextField deltaText;
    private final Label deltaLabel;
    private final Button plotButton;
    private final LineChart lineChart;
    private final XYChart.Series series;
    
    private double deltaValue;
    // Step 1: End
    
    {
        // Step 2: Start
        
        //Initialize Axis
        xAxis = new NumberAxis();
        yAxis = new NumberAxis();
        xAxis.setLabel("m Values");
        
        //Initialize XY_Chart
        lineChart = new LineChart<>(xAxis,yAxis);
        lineChart.setTitle("Aproximation Error");
        
        //XY_Chart serie
         series = new XYChart.Series();
        
        //Initialize controls
        deltaLabel = new Label("Delta: ");
        plotButton = new Button("Plot");
        deltaText = new TextField("0.0001");
        deltaText.setMaxWidth(90);
        deltaText.setMinWidth(90);
        
        // Step 2: End
        
    }
    
    @Override public void start(Stage stage) {
        // Step 3: Start
        // Set Layout to hold: deltaLabel, deltaText and plotButton
        HBox hbox = new HBox(5);
        hbox.getChildren().addAll(deltaLabel, deltaText,plotButton);
        // Step 3: End
        
        // Step 4: Start
        // Set Layout to hold: hbox and lineChart
        VBox pane = new VBox();
        pane.getChildren().addAll(hbox,lineChart);
        // Step 4: End
               
        // Step 5: Start
        // Set the Scene
        Scene scene  = new Scene(pane,800,450);
        // Step 5: End
       
        // Step 6: Start
        // Set the Stage
        stage.setScene(scene);
        stage.show();
        // Step 6: End
        
        // Step 8: Start
        // PlotButton
        plotButton.setOnAction(e->{
            lineChart.getData().clear();
            deltaValue = Double.parseDouble(deltaText.getText());
            
            lineChart.getData().add(getSeries(series,"Continuous Compoundint Vs Discret Compounding ($1 / 1 Year)", deltaValue));
        });
        //Step 8: End
            
    }
    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        launch(args);
    }
    
    // Helper function that retrieves the aproximation error (delta error)
    private XYChart.Series getSeries(XYChart.Series x, String title, double error){
        // Set the XYChart
        XYChart.Series mySerie = new XYChart.Series<>();
        mySerie.setName(title);
        
        // Set the object to retrieve the data
        Delta myDelta = new Delta(1,1,0.05);
        myDelta.getMDelta(error);
        
        // Set the data series and add them
        for(int i = 0; i < myDelta.getMValues().size(); i++){
            mySerie.getData().add(new XYChart.Data(myDelta.getMValues().get(i),myDelta.getDeltaValues().get(i)));
        }
        // Return serie
        return mySerie;
    }
    
}

No comments:

Post a Comment

Earn free bitcoin